@tanstack/vue-router
Version:
Modern and scalable routing for Vue applications
96 lines (95 loc) • 2.94 kB
JavaScript
import { useRouter } from "./useRouter.js";
import { _getAssetMatches, appendUniqueUserTags, escapeHtml, getAssetCrossOrigin, getScriptPreloadAttrs, resolveManifestCssLink } from "@tanstack/router-core";
import * as Vue from "vue";
import { useStore } from "@tanstack/vue-store";
//#region src/headContentUtils.tsx
var useTags = (assetCrossOrigin) => {
const router = useRouter();
const matches = useStore(router.stores.matches, _getAssetMatches);
const tags = Vue.computed(() => {
const currentMatches = matches.value;
const tags = [];
const manifest = router.ssr?.manifest;
for (const match of currentMatches) for (const link of manifest?.routes[match.routeId]?.css ?? []) {
const resolvedLink = resolveManifestCssLink(link);
tags.push({
tag: "link",
attrs: {
rel: "stylesheet",
...resolvedLink,
crossOrigin: getAssetCrossOrigin(assetCrossOrigin, "stylesheet") ?? resolvedLink.crossOrigin
}
});
}
if (manifest?.inlineStyle) tags.push({
tag: "style",
attrs: manifest.inlineStyle.attrs,
children: manifest.inlineStyle.children,
inlineCss: true
});
const resultMeta = [];
const metaByAttribute = {};
let title;
for (let i = currentMatches.length - 1; i >= 0; i--) {
const metas = currentMatches[i].meta;
if (!metas) continue;
for (let j = metas.length - 1; j >= 0; j--) {
const m = metas[j];
if (!m) continue;
if (m.title) {
if (!title) title = {
tag: "title",
children: m.title
};
} else if ("script:ld+json" in m) try {
resultMeta.push({
tag: "script",
attrs: { type: "application/ld+json" },
children: escapeHtml(JSON.stringify(m["script:ld+json"]))
});
} catch {}
else {
const attribute = m.name ?? m.property;
if (attribute) if (metaByAttribute[attribute]) continue;
else metaByAttribute[attribute] = true;
resultMeta.push({
tag: "meta",
attrs: { ...m }
});
}
}
}
if (title) resultMeta.push(title);
resultMeta.reverse();
appendUniqueUserTags(tags, resultMeta);
const preloads = [];
const links = [];
const headScripts = [];
for (const match of currentMatches) {
for (const preload of manifest?.routes[match.routeId]?.preloads ?? []) if (preload) preloads.push({
tag: "link",
attrs: { ...getScriptPreloadAttrs(manifest, preload, assetCrossOrigin) }
});
for (const link of match.links ?? []) if (link) links.push({
tag: "link",
attrs: { ...link }
});
for (const script of match.headScripts ?? []) if (script) {
const { children, ...attrs } = script;
headScripts.push({
tag: "script",
attrs: { ...attrs },
children
});
}
}
tags.push(...preloads);
appendUniqueUserTags(tags, links);
appendUniqueUserTags(tags, headScripts);
return tags;
});
return () => tags.value;
};
//#endregion
export { useTags };
//# sourceMappingURL=headContentUtils.js.map