UNPKG

@tanstack/vue-router

Version:

Modern and scalable routing for Vue applications

97 lines (96 loc) 2.9 kB
import { useRouter } from "./useRouter.js"; import { useRouterState } from "./useRouterState.js"; import { escapeHtml } from "@tanstack/router-core"; import * as Vue from "vue"; //#region src/headContentUtils.tsx var useTags = () => { const router = useRouter(); const routeMeta = useRouterState({ select: (state) => { return state.matches.map((match) => match.meta).filter(Boolean); } }); const meta = Vue.computed(() => { const resultMeta = []; const metaByAttribute = {}; let title; [...routeMeta.value].reverse().forEach((metas) => { [...metas].reverse().forEach((m) => { if (!m) return; if (m.title) { if (!title) title = { tag: "title", children: m.title }; } else if ("script:ld+json" in m) try { const json = JSON.stringify(m["script:ld+json"]); resultMeta.push({ tag: "script", attrs: { type: "application/ld+json" }, children: escapeHtml(json) }); } catch {} else { const attribute = m.name ?? m.property; if (attribute) if (metaByAttribute[attribute]) return; else metaByAttribute[attribute] = true; resultMeta.push({ tag: "meta", attrs: { ...m } }); } }); }); if (title) resultMeta.push(title); resultMeta.reverse(); return resultMeta; }); const links = useRouterState({ select: (state) => state.matches.map((match) => match.links).filter(Boolean).flat(1).map((link) => ({ tag: "link", attrs: { ...link } })) }); const preloadMeta = useRouterState({ select: (state) => { const preloadMeta = []; state.matches.map((match) => router.looseRoutesById[match.routeId]).forEach((route) => router.ssr?.manifest?.routes[route.id]?.preloads?.filter(Boolean).forEach((preload) => { preloadMeta.push({ tag: "link", attrs: { rel: "modulepreload", href: preload } }); })); return preloadMeta; } }); const headScripts = useRouterState({ select: (state) => state.matches.map((match) => match.headScripts).flat(1).filter(Boolean).map(({ children, ...script }) => ({ tag: "script", attrs: { ...script }, children })) }); const manifestAssets = useRouterState({ select: (state) => { const manifest = router.ssr?.manifest; return state.matches.map((match) => manifest?.routes[match.routeId]?.assets ?? []).filter(Boolean).flat(1).filter((asset) => asset.tag === "link").map((asset) => ({ tag: "link", attrs: { ...asset.attrs } })); } }); return () => uniqBy([ ...manifestAssets.value, ...meta.value, ...preloadMeta.value, ...links.value, ...headScripts.value ], (d) => { return JSON.stringify(d); }); }; function uniqBy(arr, fn) { const seen = /* @__PURE__ */ new Set(); return arr.filter((item) => { const key = fn(item); if (seen.has(key)) return false; seen.add(key); return true; }); } //#endregion export { useTags }; //# sourceMappingURL=headContentUtils.js.map