@unhead/vue 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,199 @@
1
+ 'use strict';
2
+
3
+ const shared = require('@vueuse/shared');
4
+ const vue = require('vue');
5
+ const unhead = require('unhead');
6
+ const dom = require('@unhead/dom');
7
+
8
+ function resolveUnrefHeadInput(ref) {
9
+ const root = shared.resolveUnref(ref);
10
+ if (!ref || !root)
11
+ return root;
12
+ if (Array.isArray(root))
13
+ return root.map(resolveUnrefHeadInput);
14
+ if (typeof root === "object") {
15
+ return Object.fromEntries(
16
+ Object.entries(root).map(([key, value]) => {
17
+ if (key === "titleTemplate")
18
+ return [key, vue.unref(value)];
19
+ return [key, resolveUnrefHeadInput(value)];
20
+ })
21
+ );
22
+ }
23
+ return root;
24
+ }
25
+ function asArray(value) {
26
+ return Array.isArray(value) ? value : [value];
27
+ }
28
+
29
+ const VueTriggerDomPatchingOnUpdatesPlugin = () => {
30
+ return unhead.defineHeadPlugin({
31
+ hooks: {
32
+ "entries:updated": function(head) {
33
+ dom.debouncedRenderDOMHead(vue.nextTick, head);
34
+ }
35
+ }
36
+ });
37
+ };
38
+
39
+ const vueTriggerDomPatchingOnUpdatesPlugin = {
40
+ __proto__: null,
41
+ VueTriggerDomPatchingOnUpdatesPlugin: VueTriggerDomPatchingOnUpdatesPlugin
42
+ };
43
+
44
+ const VueReactiveInputPlugin = () => {
45
+ return unhead.defineHeadPlugin({
46
+ hooks: {
47
+ "entries:resolve": function(ctx) {
48
+ for (const entry of ctx.entries)
49
+ entry.input = resolveUnrefHeadInput(entry.input);
50
+ }
51
+ }
52
+ });
53
+ };
54
+
55
+ const Vue3 = vue.version.startsWith("3");
56
+ vue.version.startsWith("2.");
57
+ const IsBrowser = typeof window !== "undefined";
58
+
59
+ function useHead$2(input, options = {}) {
60
+ const head = injectHead();
61
+ head.push(input, options);
62
+ }
63
+
64
+ function useServerHead$1(input, options = {}) {
65
+ useHead$2(input, { ...options, mode: "server" });
66
+ }
67
+
68
+ function useHead$1(input, options = {}) {
69
+ const head = injectHead();
70
+ const vm = vue.getCurrentInstance();
71
+ if (!vm) {
72
+ head.push(input, options);
73
+ return;
74
+ }
75
+ const resolvedInput = vue.ref({});
76
+ vue.watchEffect(() => {
77
+ resolvedInput.value = resolveUnrefHeadInput(input);
78
+ });
79
+ let entry;
80
+ vue.watch(resolvedInput, (e) => {
81
+ if (!entry)
82
+ entry = head.push(e, options);
83
+ else
84
+ entry.patch(e);
85
+ }, { immediate: true });
86
+ vue.onBeforeUnmount(() => {
87
+ entry?.dispose();
88
+ });
89
+ }
90
+
91
+ function useServerHead(input, options = {}) {
92
+ if (!IsBrowser)
93
+ useServerHead$1(input, options);
94
+ }
95
+ function useHead(input, options = {}) {
96
+ if (options.mode === "server" && IsBrowser || options.mode === "client" && !IsBrowser)
97
+ return;
98
+ IsBrowser ? useHead$1(input, options) : useHead$2(input, options);
99
+ }
100
+ const useTitle = (title) => useHead({ title });
101
+ const useTitleTemplate = (titleTemplate) => useHead({ titleTemplate });
102
+ const useMeta = (meta) => useHead({ meta: asArray(meta) });
103
+ const useLink = (link) => useHead({ link: asArray(link) });
104
+ const useScript = (script) => useHead({ script: asArray(script) });
105
+ const useStyle = (style) => useHead({ style: asArray(style) });
106
+ const useNoscript = (noscript) => useHead({ noscript: asArray(noscript) });
107
+ const useBase = (base) => useHead({ base });
108
+ const useHtmlAttrs = (attrs) => useHead({ htmlAttrs: attrs });
109
+ const useBodyAttrs = (attrs) => useHead({ bodyAttrs: attrs });
110
+
111
+ const headSymbol = Symbol("unhead");
112
+ function injectHead() {
113
+ return vue.getCurrentInstance() && vue.inject(headSymbol) || unhead.getActiveHead();
114
+ }
115
+ async function createHead(options = {}) {
116
+ const plugins = [
117
+ unhead.HydratesStatePlugin(),
118
+ VueReactiveInputPlugin(),
119
+ ...options?.plugins || []
120
+ ];
121
+ if (IsBrowser) {
122
+ const { VueTriggerDomPatchingOnUpdatesPlugin } = await Promise.resolve().then(function () { return vueTriggerDomPatchingOnUpdatesPlugin; });
123
+ plugins.push(VueTriggerDomPatchingOnUpdatesPlugin());
124
+ }
125
+ const head = await unhead.createHead({
126
+ ...options,
127
+ plugins
128
+ });
129
+ const vuePlugin = {
130
+ install(app) {
131
+ if (Vue3)
132
+ app.config.globalProperties.$head = head;
133
+ app.provide(headSymbol, head);
134
+ app.mixin({
135
+ created() {
136
+ const instance = vue.getCurrentInstance();
137
+ if (!instance)
138
+ return;
139
+ const options2 = instance.type;
140
+ if (!options2 || !("head" in options2))
141
+ return;
142
+ const source = typeof options2.head === "function" ? () => options2.head() : options2.head;
143
+ useHead(source);
144
+ }
145
+ });
146
+ }
147
+ };
148
+ return { ...vuePlugin, ...head };
149
+ }
150
+
151
+ const HeadVuePlugin = function(_Vue) {
152
+ _Vue.mixin({
153
+ beforeCreate() {
154
+ const options = this.$options;
155
+ if (options.head) {
156
+ const origProvide = options.provide;
157
+ options.provide = function() {
158
+ let origProvideResult;
159
+ if (typeof origProvide === "function")
160
+ origProvideResult = origProvide.call(this);
161
+ else
162
+ origProvideResult = origProvide || {};
163
+ return {
164
+ ...origProvideResult,
165
+ [headSymbol]: options.head
166
+ };
167
+ };
168
+ if (!this.$head)
169
+ this.$head = options.head;
170
+ } else if (!this.$head && options.parent && options.parent.$head) {
171
+ this.$head = options.parent.$head;
172
+ }
173
+ }
174
+ });
175
+ };
176
+
177
+ exports.HeadVuePlugin = HeadVuePlugin;
178
+ exports.VueReactiveInputPlugin = VueReactiveInputPlugin;
179
+ exports.VueTriggerDomPatchingOnUpdatesPlugin = VueTriggerDomPatchingOnUpdatesPlugin;
180
+ exports.asArray = asArray;
181
+ exports.createHead = createHead;
182
+ exports.headSymbol = headSymbol;
183
+ exports.injectHead = injectHead;
184
+ exports.resolveUnrefHeadInput = resolveUnrefHeadInput;
185
+ exports.useBase = useBase;
186
+ exports.useBodyAttrs = useBodyAttrs;
187
+ exports.useHead = useHead;
188
+ exports.useHtmlAttrs = useHtmlAttrs;
189
+ exports.useLink = useLink;
190
+ exports.useMeta = useMeta;
191
+ exports.useNoscript = useNoscript;
192
+ exports.useScript = useScript;
193
+ exports.useServerHead = useServerHead;
194
+ exports.useStyle = useStyle;
195
+ exports.useTitle = useTitle;
196
+ exports.useTitleTemplate = useTitleTemplate;
197
+ for (const k in dom) {
198
+ if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = dom[k];
199
+ }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { MaybeComputedRef, MaybeRef } from '@vueuse/shared';
2
+ export { MaybeComputedRef } from '@vueuse/shared';
2
3
  import * as _unhead_schema from '@unhead/schema';
3
4
  import { Title as Title$1, TitleTemplate as TitleTemplate$1, EntryAugmentation, Base as Base$1, Link as Link$1, Meta as Meta$1, Style as Style$1, Script as Script$1, Noscript as Noscript$1, DataKeys, SchemaAugmentations, DefinedValueOrEmptyObject, MergeHead, BaseHtmlAttr, MaybeArray, BaseBodyAttr, HeadEntryOptions, HeadClient, CreateHeadOptions } from '@unhead/schema';
4
- export { HeadTag, MergeHead } from '@unhead/schema';
5
- import { Plugin, InjectionKey } from 'vue';
5
+ export { ActiveHeadEntry, Head, HeadClient, HeadEntryOptions, HeadTag, MergeHead } from '@unhead/schema';
6
+ import { Plugin } from 'vue';
6
7
  export * from '@unhead/dom';
7
8
 
8
9
  declare type MaybeComputedRefEntries<T> = MaybeComputedRef<T> | {
@@ -107,6 +108,8 @@ declare type UseHeadInput = MaybeComputedRef<ReactiveHead>;
107
108
  declare function resolveUnrefHeadInput(ref: any): any;
108
109
  declare function asArray<T>(value: Arrayable<T>): T[];
109
110
 
111
+ declare const VueTriggerDomPatchingOnUpdatesPlugin: () => _unhead_schema.HeadPlugin;
112
+
110
113
  declare const VueReactiveInputPlugin: () => _unhead_schema.HeadPlugin;
111
114
 
112
115
  declare function useServerHead(input: ReactiveHead, options?: HeadEntryOptions): void;
@@ -122,11 +125,11 @@ declare const useBase: (base: ReactiveHead['base']) => void;
122
125
  declare const useHtmlAttrs: (attrs: ReactiveHead['htmlAttrs']) => void;
123
126
  declare const useBodyAttrs: (attrs: ReactiveHead['bodyAttrs']) => void;
124
127
 
125
- declare type VueHeadClient = HeadClient<UseHeadInput> & Plugin;
126
- declare const headSymbol: InjectionKey<VueHeadClient>;
127
- declare function injectHead(): VueHeadClient;
128
- declare function createHead(options?: CreateHeadOptions): VueHeadClient;
128
+ declare type VueHeadClient<T extends MergeHead> = HeadClient<MaybeComputedRef<ReactiveHead<T>>> & Plugin;
129
+ declare const headSymbol: unique symbol;
130
+ declare function injectHead<T extends MergeHead>(): VueHeadClient<T>;
131
+ declare function createHead<T extends MergeHead>(options?: CreateHeadOptions): Promise<VueHeadClient<T>>;
129
132
 
130
133
  declare const HeadVuePlugin: Plugin;
131
134
 
132
- export { Arrayable, Base, BodyAttributes, HeadVuePlugin, HtmlAttributes, Link, MaybeComputedRefEntries, Meta, Noscript, ReactiveHead, Script, Style, Title, TitleTemplate, UseHeadInput, VueHeadClient, VueReactiveInputPlugin, asArray, createHead, headSymbol, injectHead, resolveUnrefHeadInput, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
135
+ export { Arrayable, Base, BodyAttributes, HeadVuePlugin, HtmlAttributes, Link, MaybeComputedRefEntries, Meta, Noscript, ReactiveHead, Script, Style, Title, TitleTemplate, UseHeadInput, VueHeadClient, VueReactiveInputPlugin, VueTriggerDomPatchingOnUpdatesPlugin, asArray, createHead, headSymbol, injectHead, resolveUnrefHeadInput, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { resolveUnref } from '@vueuse/shared';
2
- import { unref, version, getCurrentInstance, ref, watchEffect, watch, nextTick, onBeforeUnmount, inject } from 'vue';
3
- import { defineHeadPlugin, getActiveHead, createHead as createHead$1, HydratesStatePlugin } from 'unhead';
2
+ import { unref, nextTick, version, getCurrentInstance, ref, watchEffect, watch, onBeforeUnmount, inject } from 'vue';
3
+ import { defineHeadPlugin, getActiveHead, HydratesStatePlugin, createHead as createHead$1 } from 'unhead';
4
4
  import { debouncedRenderDOMHead } from '@unhead/dom';
5
5
  export * from '@unhead/dom';
6
6
 
@@ -25,6 +25,21 @@ function asArray(value) {
25
25
  return Array.isArray(value) ? value : [value];
26
26
  }
27
27
 
28
+ const VueTriggerDomPatchingOnUpdatesPlugin = () => {
29
+ return defineHeadPlugin({
30
+ hooks: {
31
+ "entries:updated": function(head) {
32
+ debouncedRenderDOMHead(nextTick, head);
33
+ }
34
+ }
35
+ });
36
+ };
37
+
38
+ const vueTriggerDomPatchingOnUpdatesPlugin = {
39
+ __proto__: null,
40
+ VueTriggerDomPatchingOnUpdatesPlugin: VueTriggerDomPatchingOnUpdatesPlugin
41
+ };
42
+
28
43
  const VueReactiveInputPlugin = () => {
29
44
  return defineHeadPlugin({
30
45
  hooks: {
@@ -38,7 +53,7 @@ const VueReactiveInputPlugin = () => {
38
53
 
39
54
  const Vue3 = version.startsWith("3");
40
55
  version.startsWith("2.");
41
- const IsClient = typeof window !== "undefined";
56
+ const IsBrowser = typeof window !== "undefined";
42
57
 
43
58
  function useHead$2(input, options = {}) {
44
59
  const head = injectHead();
@@ -66,22 +81,20 @@ function useHead$1(input, options = {}) {
66
81
  entry = head.push(e, options);
67
82
  else
68
83
  entry.patch(e);
69
- debouncedRenderDOMHead(nextTick, head);
70
84
  }, { immediate: true });
71
85
  onBeforeUnmount(() => {
72
86
  entry?.dispose();
73
- debouncedRenderDOMHead(nextTick, head);
74
87
  });
75
88
  }
76
89
 
77
90
  function useServerHead(input, options = {}) {
78
- if (!IsClient)
91
+ if (!IsBrowser)
79
92
  useServerHead$1(input, options);
80
93
  }
81
94
  function useHead(input, options = {}) {
82
- if (options.mode === "server" && IsClient || options.mode === "client" && !IsClient)
95
+ if (options.mode === "server" && IsBrowser || options.mode === "client" && !IsBrowser)
83
96
  return;
84
- return IsClient ? useHead$1(input, options) : useHead$2(input, options);
97
+ IsBrowser ? useHead$1(input, options) : useHead$2(input, options);
85
98
  }
86
99
  const useTitle = (title) => useHead({ title });
87
100
  const useTitleTemplate = (titleTemplate) => useHead({ titleTemplate });
@@ -94,18 +107,23 @@ const useBase = (base) => useHead({ base });
94
107
  const useHtmlAttrs = (attrs) => useHead({ htmlAttrs: attrs });
95
108
  const useBodyAttrs = (attrs) => useHead({ bodyAttrs: attrs });
96
109
 
97
- const headSymbol = Symbol("head");
110
+ const headSymbol = Symbol("unhead");
98
111
  function injectHead() {
99
112
  return getCurrentInstance() && inject(headSymbol) || getActiveHead();
100
113
  }
101
- function createHead(options = {}) {
102
- const head = createHead$1({
114
+ async function createHead(options = {}) {
115
+ const plugins = [
116
+ HydratesStatePlugin(),
117
+ VueReactiveInputPlugin(),
118
+ ...options?.plugins || []
119
+ ];
120
+ if (IsBrowser) {
121
+ const { VueTriggerDomPatchingOnUpdatesPlugin } = await Promise.resolve().then(function () { return vueTriggerDomPatchingOnUpdatesPlugin; });
122
+ plugins.push(VueTriggerDomPatchingOnUpdatesPlugin());
123
+ }
124
+ const head = await createHead$1({
103
125
  ...options,
104
- plugins: [
105
- HydratesStatePlugin(),
106
- VueReactiveInputPlugin(),
107
- ...options?.plugins || []
108
- ]
126
+ plugins
109
127
  });
110
128
  const vuePlugin = {
111
129
  install(app) {
@@ -121,7 +139,7 @@ function createHead(options = {}) {
121
139
  if (!options2 || !("head" in options2))
122
140
  return;
123
141
  const source = typeof options2.head === "function" ? () => options2.head() : options2.head;
124
- head.push(source);
142
+ useHead(source);
125
143
  }
126
144
  });
127
145
  }
@@ -155,4 +173,4 @@ const HeadVuePlugin = function(_Vue) {
155
173
  });
156
174
  };
157
175
 
158
- export { HeadVuePlugin, VueReactiveInputPlugin, asArray, createHead, headSymbol, injectHead, resolveUnrefHeadInput, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
176
+ export { HeadVuePlugin, VueReactiveInputPlugin, VueTriggerDomPatchingOnUpdatesPlugin, asArray, createHead, headSymbol, injectHead, resolveUnrefHeadInput, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
@@ -1,5 +1,4 @@
1
- import { getCurrentInstance, nextTick, onBeforeUnmount, ref, watch, watchEffect } from "vue";
2
- import { debouncedRenderDOMHead } from "@unhead/dom";
1
+ import { getCurrentInstance, onBeforeUnmount, ref, watch, watchEffect } from "vue";
3
2
  import { injectHead, resolveUnrefHeadInput } from "../../index";
4
3
  export function useHead(input, options = {}) {
5
4
  const head = injectHead();
@@ -18,10 +17,8 @@ export function useHead(input, options = {}) {
18
17
  entry = head.push(e, options);
19
18
  else
20
19
  entry.patch(e);
21
- debouncedRenderDOMHead(nextTick, head);
22
20
  }, { immediate: true });
23
21
  onBeforeUnmount(() => {
24
22
  entry?.dispose();
25
- debouncedRenderDOMHead(nextTick, head);
26
23
  });
27
24
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/vue",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "packageManager": "pnpm@7.14.0",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -19,9 +19,11 @@
19
19
  "exports": {
20
20
  ".": {
21
21
  "types": "./dist/index.d.ts",
22
+ "require": "./dist/index.cjs",
22
23
  "import": "./dist/index.mjs"
23
24
  }
24
25
  },
26
+ "main": "dist/index.cjs",
25
27
  "module": "dist/index.mjs",
26
28
  "types": "dist/index.d.ts",
27
29
  "files": [
@@ -31,10 +33,10 @@
31
33
  "vue": ">=2.7 || >=3"
32
34
  },
33
35
  "dependencies": {
34
- "@unhead/dom": "0.1.1",
35
- "@unhead/schema": "0.1.1",
36
+ "@unhead/dom": "0.1.3",
37
+ "@unhead/schema": "0.1.3",
36
38
  "@vueuse/shared": "latest",
37
- "unhead": "0.1.1"
39
+ "unhead": "0.1.3"
38
40
  },
39
41
  "devDependencies": {
40
42
  "vue": "latest"