Server: Phase 2 (REVIEW not respected yet)
This commit is contained in:
1 parent
5d16ded553
commit
142b47674d
4271 files changed
+1203269
No files matched your search
+144
@@ -0,0 +1,144 @@
|
||||
import { useRouter } from "./useRouter.js";
|
||||
import { _getAssetMatches, appendUniqueUserTags, deepEqual, escapeHtml, getAssetCrossOrigin, getScriptPreloadAttrs, resolveManifestCssLink } from "@tanstack/router-core";
|
||||
import * as React$1 from "react";
|
||||
import { isServer } from "@tanstack/router-core/isServer";
|
||||
import { useSelector } from "@tanstack/react-store";
|
||||
//#region src/headContentUtils.tsx
|
||||
function buildTagsFromMatches(router, nonce, matches, assetCrossOrigin) {
|
||||
matches = _getAssetMatches(matches);
|
||||
const routeMeta = matches.map((match) => match.meta).filter((meta) => meta !== void 0);
|
||||
const resultMeta = [];
|
||||
const metaByAttribute = {};
|
||||
let title;
|
||||
for (let i = routeMeta.length - 1; i >= 0; i--) {
|
||||
const metas = routeMeta[i];
|
||||
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 {
|
||||
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]) continue;
|
||||
else metaByAttribute[attribute] = true;
|
||||
resultMeta.push({
|
||||
tag: "meta",
|
||||
attrs: {
|
||||
...m,
|
||||
nonce
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (title) resultMeta.push(title);
|
||||
if (nonce) resultMeta.push({
|
||||
tag: "meta",
|
||||
attrs: {
|
||||
property: "csp-nonce",
|
||||
content: nonce
|
||||
}
|
||||
});
|
||||
resultMeta.reverse();
|
||||
const constructedLinks = matches.flatMap((match) => match.links ?? []).filter((link) => link !== void 0).map((link) => ({
|
||||
tag: "link",
|
||||
attrs: {
|
||||
...link,
|
||||
nonce
|
||||
}
|
||||
}));
|
||||
const manifest = router.ssr?.manifest;
|
||||
const manifestCssTags = [];
|
||||
if (manifest) {
|
||||
matches.forEach((match) => {
|
||||
(manifest.routes[match.routeId]?.css)?.forEach((link) => {
|
||||
const resolvedLink = resolveManifestCssLink(link);
|
||||
manifestCssTags.push({
|
||||
tag: "link",
|
||||
attrs: {
|
||||
rel: "stylesheet",
|
||||
...resolvedLink,
|
||||
crossOrigin: getAssetCrossOrigin(assetCrossOrigin, "stylesheet") ?? resolvedLink.crossOrigin,
|
||||
suppressHydrationWarning: true,
|
||||
nonce
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
if (manifest.inlineStyle) manifestCssTags.push({
|
||||
tag: "style",
|
||||
attrs: {
|
||||
...manifest.inlineStyle.attrs,
|
||||
nonce
|
||||
},
|
||||
children: manifest.inlineStyle.children,
|
||||
inlineCss: true
|
||||
});
|
||||
}
|
||||
const preloadLinks = [];
|
||||
if (manifest) matches.forEach((match) => {
|
||||
manifest.routes[match.routeId]?.preloads?.forEach((preload) => {
|
||||
preloadLinks.push({
|
||||
tag: "link",
|
||||
attrs: {
|
||||
...getScriptPreloadAttrs(manifest, preload, assetCrossOrigin),
|
||||
nonce
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
const styles = matches.flatMap((match) => match.styles ?? []).filter((style) => style !== void 0).map(({ children, ...attrs }) => ({
|
||||
tag: "style",
|
||||
attrs: {
|
||||
...attrs,
|
||||
nonce
|
||||
},
|
||||
children
|
||||
}));
|
||||
const headScripts = matches.flatMap((match) => match.headScripts ?? []).filter((script) => script !== void 0).map(({ children, ...script }) => ({
|
||||
tag: "script",
|
||||
attrs: {
|
||||
...script,
|
||||
nonce
|
||||
},
|
||||
children
|
||||
}));
|
||||
const tags = [];
|
||||
appendUniqueUserTags(tags, resultMeta);
|
||||
tags.push(...preloadLinks);
|
||||
appendUniqueUserTags(tags, constructedLinks);
|
||||
tags.push(...manifestCssTags);
|
||||
appendUniqueUserTags(tags, styles);
|
||||
appendUniqueUserTags(tags, headScripts);
|
||||
return tags;
|
||||
}
|
||||
/**
|
||||
* Build the head/link/meta/script tags from the renderable presented prefix.
|
||||
* Used internally by `HeadContent`.
|
||||
*/
|
||||
var useTags = (assetCrossOrigin) => {
|
||||
const router = useRouter();
|
||||
const nonce = router.options.ssr?.nonce;
|
||||
if (isServer ?? router.isServer) return buildTagsFromMatches(router, nonce, router.stores.matches.get(), assetCrossOrigin);
|
||||
const selectTags = React$1.useCallback((matches) => buildTagsFromMatches(router, nonce, matches, assetCrossOrigin), [
|
||||
assetCrossOrigin,
|
||||
nonce,
|
||||
router
|
||||
]);
|
||||
return useSelector(router.stores.matches, selectTags, { compare: deepEqual });
|
||||
};
|
||||
//#endregion
|
||||
export { useTags };
|
||||
|
||||
//# sourceMappingURL=headContentUtils.js.map
|
||||
Reference in new issue
Block a user