87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
import { createServerHead as createServerHead$1, createHead as createHead$1, getActiveHead } from 'unhead';
|
|
import { version, unref, nextTick, inject } from 'vue';
|
|
import { defineHeadPlugin } from '@unhead/shared';
|
|
|
|
const Vue3 = version[0] === "3";
|
|
|
|
function resolveUnref(r) {
|
|
return typeof r === "function" ? r() : unref(r);
|
|
}
|
|
function resolveUnrefHeadInput(ref) {
|
|
if (ref instanceof Promise || ref instanceof Date || ref instanceof RegExp)
|
|
return ref;
|
|
const root = resolveUnref(ref);
|
|
if (!ref || !root)
|
|
return root;
|
|
if (Array.isArray(root))
|
|
return root.map((r) => resolveUnrefHeadInput(r));
|
|
if (typeof root === "object") {
|
|
const resolved = {};
|
|
for (const k in root) {
|
|
if (!Object.prototype.hasOwnProperty.call(root, k)) {
|
|
continue;
|
|
}
|
|
if (k === "titleTemplate" || k[0] === "o" && k[1] === "n") {
|
|
resolved[k] = unref(root[k]);
|
|
continue;
|
|
}
|
|
resolved[k] = resolveUnrefHeadInput(root[k]);
|
|
}
|
|
return resolved;
|
|
}
|
|
return root;
|
|
}
|
|
|
|
const VueReactivityPlugin = defineHeadPlugin({
|
|
hooks: {
|
|
"entries:resolve": (ctx) => {
|
|
for (const entry of ctx.entries)
|
|
entry.resolvedInput = resolveUnrefHeadInput(entry.input);
|
|
}
|
|
}
|
|
});
|
|
|
|
const headSymbol = "usehead";
|
|
function vueInstall(head) {
|
|
const plugin = {
|
|
install(app) {
|
|
if (Vue3) {
|
|
app.config.globalProperties.$unhead = head;
|
|
app.config.globalProperties.$head = head;
|
|
app.provide(headSymbol, head);
|
|
}
|
|
}
|
|
};
|
|
return plugin.install;
|
|
}
|
|
function createServerHead(options = {}) {
|
|
const head = createServerHead$1(options);
|
|
head.use(VueReactivityPlugin);
|
|
head.install = vueInstall(head);
|
|
return head;
|
|
}
|
|
function createHead(options = {}) {
|
|
options.domDelayFn = options.domDelayFn || ((fn) => nextTick(() => setTimeout(() => fn(), 0)));
|
|
const head = createHead$1(options);
|
|
head.use(VueReactivityPlugin);
|
|
head.install = vueInstall(head);
|
|
return head;
|
|
}
|
|
|
|
const _global = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
const globalKey = "__unhead_injection_handler__";
|
|
function setHeadInjectionHandler(handler) {
|
|
_global[globalKey] = handler;
|
|
}
|
|
function injectHead() {
|
|
if (globalKey in _global) {
|
|
return _global[globalKey]();
|
|
}
|
|
const head = inject(headSymbol);
|
|
if (!head && process.env.NODE_ENV !== "production")
|
|
console.warn("Unhead is missing Vue context, falling back to shared context. This may have unexpected results.");
|
|
return head || getActiveHead();
|
|
}
|
|
|
|
export { Vue3 as V, createServerHead as a, createHead as c, headSymbol as h, injectHead as i, resolveUnrefHeadInput as r, setHeadInjectionHandler as s };
|