Files
med-notes/.pnpm-store/v10/files/fe/eae4e77a0dc5bd0d5aee7007885cfef1564948f314e13be8dac7f82c804334b9fab457472e58b69562497e93dc8ed9ea5e86f71fc5fc1e83041cd061052a6a
2025-05-09 05:30:08 +02:00

123 lines
3.1 KiB
Plaintext

import * as React from "react";
import { createElement } from "react";
import { Asset } from "./Asset.js";
import { useRouter } from "./useRouter.js";
import { useRouterState } from "./useRouterState.js";
const useTags = () => {
const router = useRouter();
const routeMeta = useRouterState({
select: (state) => {
return state.matches.map((match) => match.meta).filter(Boolean);
}
});
const meta = React.useMemo(() => {
const resultMeta = [];
const metaByAttribute = {};
let title;
[...routeMeta].reverse().forEach((metas) => {
[...metas].reverse().forEach((m) => {
if (!m) return;
if (m.title) {
if (!title) {
title = {
tag: "title",
children: m.title
};
}
} 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;
}, [routeMeta]);
const links = useRouterState({
select: (state) => state.matches.map((match) => match.links).filter(Boolean).flat(1).map((link) => ({
tag: "link",
attrs: {
...link
}
})),
structuralSharing: true
});
const preloadMeta = useRouterState({
select: (state) => {
const preloadMeta2 = [];
state.matches.map((match) => router.looseRoutesById[match.routeId]).forEach(
(route) => {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = router.ssr) == null ? void 0 : _a.manifest) == null ? void 0 : _b.routes[route.id]) == null ? void 0 : _c.preloads) == null ? void 0 : _d.filter(Boolean).forEach((preload) => {
preloadMeta2.push({
tag: "link",
attrs: {
rel: "modulepreload",
href: preload
}
});
});
}
);
return preloadMeta2;
},
structuralSharing: true
});
const headScripts = useRouterState({
select: (state) => state.matches.map((match) => match.headScripts).flat(1).filter(Boolean).map(({ children, ...script }) => ({
tag: "script",
attrs: {
...script
},
children
})),
structuralSharing: true
});
return uniqBy(
[
...meta,
...preloadMeta,
...links,
...headScripts
],
(d) => {
return JSON.stringify(d);
}
);
};
function HeadContent() {
const tags = useTags();
return tags.map((tag) => /* @__PURE__ */ createElement(Asset, { ...tag, key: `tsr-meta-${JSON.stringify(tag)}` }));
}
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;
});
}
export {
HeadContent,
useTags
};
//# sourceMappingURL=HeadContent.js.map