@pyreon/head 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js ADDED
@@ -0,0 +1,276 @@
1
+ import { createContext, h, onMount, onUnmount, popContext, pushContext, useContext } from "@pyreon/core";
2
+ import { renderToString } from "@pyreon/runtime-server";
3
+ import { effect } from "@pyreon/reactivity";
4
+
5
+ //#region src/context.ts
6
+ function createHeadContext() {
7
+ const map = /* @__PURE__ */ new Map();
8
+ return {
9
+ add(id, entry) {
10
+ map.set(id, entry);
11
+ },
12
+ remove(id) {
13
+ map.delete(id);
14
+ },
15
+ resolve() {
16
+ const keyed = /* @__PURE__ */ new Map();
17
+ const unkeyed = [];
18
+ for (const entry of map.values()) for (const tag of entry.tags) if (tag.key) keyed.set(tag.key, tag);
19
+ else unkeyed.push(tag);
20
+ return [...keyed.values(), ...unkeyed];
21
+ },
22
+ resolveTitleTemplate() {
23
+ let template;
24
+ for (const entry of map.values()) if (entry.titleTemplate !== void 0) template = entry.titleTemplate;
25
+ return template;
26
+ },
27
+ resolveHtmlAttrs() {
28
+ const attrs = {};
29
+ for (const entry of map.values()) if (entry.htmlAttrs) Object.assign(attrs, entry.htmlAttrs);
30
+ return attrs;
31
+ },
32
+ resolveBodyAttrs() {
33
+ const attrs = {};
34
+ for (const entry of map.values()) if (entry.bodyAttrs) Object.assign(attrs, entry.bodyAttrs);
35
+ return attrs;
36
+ }
37
+ };
38
+ }
39
+ const HeadContext = createContext(null);
40
+
41
+ //#endregion
42
+ //#region src/provider.tsx
43
+ /**
44
+ * Provides a HeadContextValue to all descendant components.
45
+ * Wrap your app root with this to enable useHead() throughout the tree.
46
+ *
47
+ * If no `context` prop is passed, a new HeadContext is created automatically.
48
+ *
49
+ * @example
50
+ * // Auto-create context:
51
+ * <HeadProvider><App /></HeadProvider>
52
+ *
53
+ * // Explicit context (e.g. for SSR):
54
+ * const headCtx = createHeadContext()
55
+ * mount(h(HeadProvider, { context: headCtx }, h(App, null)), root)
56
+ */
57
+ const HeadProvider = (props) => {
58
+ const ctx = props.context ?? createHeadContext();
59
+ pushContext(new Map([[HeadContext.id, ctx]]));
60
+ onUnmount(() => popContext());
61
+ const ch = props.children;
62
+ return typeof ch === "function" ? ch() : ch;
63
+ };
64
+
65
+ //#endregion
66
+ //#region src/ssr.ts
67
+ const VOID_TAGS = new Set([
68
+ "meta",
69
+ "link",
70
+ "base"
71
+ ]);
72
+ async function renderWithHead(app) {
73
+ const ctx = createHeadContext();
74
+ function HeadInjector() {
75
+ pushContext(new Map([[HeadContext.id, ctx]]));
76
+ return app;
77
+ }
78
+ const html = await renderToString(h(HeadInjector, null));
79
+ const titleTemplate = ctx.resolveTitleTemplate();
80
+ return {
81
+ html,
82
+ head: ctx.resolve().map((tag) => serializeTag(tag, titleTemplate)).join("\n "),
83
+ htmlAttrs: ctx.resolveHtmlAttrs(),
84
+ bodyAttrs: ctx.resolveBodyAttrs()
85
+ };
86
+ }
87
+ function serializeTag(tag, titleTemplate) {
88
+ if (tag.tag === "title") {
89
+ const raw = tag.children || "";
90
+ return `<title>${esc(titleTemplate ? typeof titleTemplate === "function" ? titleTemplate(raw) : titleTemplate.replace(/%s/g, raw) : raw)}</title>`;
91
+ }
92
+ const props = tag.props;
93
+ const attrs = props ? Object.entries(props).map(([k, v]) => `${k}="${esc(v)}"`).join(" ") : "";
94
+ const open = attrs ? `<${tag.tag} ${attrs}` : `<${tag.tag}`;
95
+ if (VOID_TAGS.has(tag.tag)) return `${open} />`;
96
+ return `${open}>${(tag.children || "").replace(/<\/(script|style|noscript)/gi, "<\\/$1").replace(/<!--/g, "<\\!--")}</${tag.tag}>`;
97
+ }
98
+ function esc(s) {
99
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
100
+ }
101
+
102
+ //#endregion
103
+ //#region src/dom.ts
104
+ const ATTR = "data-pyreon-head";
105
+ /**
106
+ * Sync the resolved head tags to the real DOM <head>.
107
+ * Uses incremental diffing: matches existing elements by key, patches attributes
108
+ * in-place, adds new elements, and removes stale ones.
109
+ * Also syncs htmlAttrs, bodyAttrs, and applies titleTemplate.
110
+ * No-op on the server (typeof document === "undefined").
111
+ */
112
+ function patchExistingTag(found, tag, kept) {
113
+ kept.add(found);
114
+ patchAttrs(found, tag.props);
115
+ const content = String(tag.children);
116
+ if (found.textContent !== content) found.textContent = content;
117
+ }
118
+ function createNewTag(tag) {
119
+ const el = document.createElement(tag.tag);
120
+ el.setAttribute(ATTR, tag.key);
121
+ for (const [k, v] of Object.entries(tag.props)) el.setAttribute(k, v);
122
+ if (tag.children) el.textContent = tag.children;
123
+ document.head.appendChild(el);
124
+ }
125
+ function syncDom(ctx) {
126
+ if (typeof document === "undefined") return;
127
+ const tags = ctx.resolve();
128
+ const titleTemplate = ctx.resolveTitleTemplate();
129
+ const existing = document.head.querySelectorAll(`[${ATTR}]`);
130
+ const byKey = /* @__PURE__ */ new Map();
131
+ for (const el of existing) byKey.set(el.getAttribute(ATTR), el);
132
+ const kept = /* @__PURE__ */ new Set();
133
+ for (const tag of tags) {
134
+ if (tag.tag === "title") {
135
+ document.title = applyTitleTemplate(String(tag.children), titleTemplate);
136
+ continue;
137
+ }
138
+ const key = tag.key;
139
+ const found = byKey.get(key);
140
+ if (found && found.tagName.toLowerCase() === tag.tag) patchExistingTag(found, tag, kept);
141
+ else createNewTag(tag);
142
+ }
143
+ for (const el of existing) if (!kept.has(el)) el.remove();
144
+ syncElementAttrs(document.documentElement, ctx.resolveHtmlAttrs());
145
+ syncElementAttrs(document.body, ctx.resolveBodyAttrs());
146
+ }
147
+ /** Patch an element's attributes to match the desired props. */
148
+ function patchAttrs(el, props) {
149
+ for (let i = el.attributes.length - 1; i >= 0; i--) {
150
+ const attr = el.attributes[i];
151
+ if (!attr || attr.name === ATTR) continue;
152
+ if (!(attr.name in props)) el.removeAttribute(attr.name);
153
+ }
154
+ for (const [k, v] of Object.entries(props)) if (el.getAttribute(k) !== v) el.setAttribute(k, v);
155
+ }
156
+ function applyTitleTemplate(title, template) {
157
+ if (!template) return title;
158
+ if (typeof template === "function") return template(title);
159
+ return template.replace(/%s/g, title);
160
+ }
161
+ /** Sync pyreon-managed attributes on <html> or <body>. */
162
+ function syncElementAttrs(el, attrs) {
163
+ const managed = el.getAttribute(`${ATTR}-attrs`);
164
+ if (managed) {
165
+ for (const name of managed.split(",")) if (name && !(name in attrs)) el.removeAttribute(name);
166
+ }
167
+ const keys = [];
168
+ for (const [k, v] of Object.entries(attrs)) {
169
+ keys.push(k);
170
+ if (el.getAttribute(k) !== v) el.setAttribute(k, v);
171
+ }
172
+ if (keys.length > 0) el.setAttribute(`${ATTR}-attrs`, keys.join(","));
173
+ else if (managed) el.removeAttribute(`${ATTR}-attrs`);
174
+ }
175
+
176
+ //#endregion
177
+ //#region src/use-head.ts
178
+ function buildEntry(o) {
179
+ const tags = [];
180
+ if (o.title != null) tags.push({
181
+ tag: "title",
182
+ key: "title",
183
+ children: o.title
184
+ });
185
+ o.meta?.forEach((m, i) => {
186
+ tags.push({
187
+ tag: "meta",
188
+ key: m.name ?? m.property ?? `meta-${i}`,
189
+ props: m
190
+ });
191
+ });
192
+ o.link?.forEach((l, i) => {
193
+ tags.push({
194
+ tag: "link",
195
+ key: l.href ? `link-${l.rel || ""}-${l.href}` : l.rel ? `link-${l.rel}` : `link-${i}`,
196
+ props: l
197
+ });
198
+ });
199
+ o.script?.forEach((s, i) => {
200
+ const { children, ...rest } = s;
201
+ tags.push({
202
+ tag: "script",
203
+ key: s.src ?? `script-${i}`,
204
+ props: rest,
205
+ ...children != null ? { children } : {}
206
+ });
207
+ });
208
+ o.style?.forEach((s, i) => {
209
+ const { children, ...rest } = s;
210
+ tags.push({
211
+ tag: "style",
212
+ key: `style-${i}`,
213
+ props: rest,
214
+ children
215
+ });
216
+ });
217
+ o.noscript?.forEach((ns, i) => {
218
+ tags.push({
219
+ tag: "noscript",
220
+ key: `noscript-${i}`,
221
+ children: ns.children
222
+ });
223
+ });
224
+ if (o.jsonLd) tags.push({
225
+ tag: "script",
226
+ key: "jsonld",
227
+ props: { type: "application/ld+json" },
228
+ children: JSON.stringify(o.jsonLd)
229
+ });
230
+ if (o.base) tags.push({
231
+ tag: "base",
232
+ key: "base",
233
+ props: o.base
234
+ });
235
+ return {
236
+ tags,
237
+ titleTemplate: o.titleTemplate,
238
+ htmlAttrs: o.htmlAttrs,
239
+ bodyAttrs: o.bodyAttrs
240
+ };
241
+ }
242
+ /**
243
+ * Register head tags (title, meta, link, script, style, noscript, base, jsonLd)
244
+ * for the current component.
245
+ *
246
+ * Accepts a static object or a reactive getter:
247
+ * useHead({ title: "My Page", meta: [{ name: "description", content: "..." }] })
248
+ * useHead(() => ({ title: `${count()} items` })) // updates when signal changes
249
+ *
250
+ * Tags are deduplicated by key — innermost component wins.
251
+ * Requires a <HeadProvider> (CSR) or renderWithHead() (SSR) ancestor.
252
+ */
253
+ function useHead(input) {
254
+ const ctx = useContext(HeadContext);
255
+ if (!ctx) return;
256
+ const id = Symbol();
257
+ if (typeof input === "function") if (typeof document !== "undefined") effect(() => {
258
+ ctx.add(id, buildEntry(input()));
259
+ syncDom(ctx);
260
+ });
261
+ else ctx.add(id, buildEntry(input()));
262
+ else {
263
+ ctx.add(id, buildEntry(input));
264
+ onMount(() => {
265
+ syncDom(ctx);
266
+ });
267
+ }
268
+ onUnmount(() => {
269
+ ctx.remove(id);
270
+ syncDom(ctx);
271
+ });
272
+ }
273
+
274
+ //#endregion
275
+ export { HeadContext, HeadProvider, createHeadContext, renderWithHead, useHead };
276
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/provider.tsx","../src/ssr.ts","../src/dom.ts","../src/use-head.ts"],"sourcesContent":["import { createContext } from \"@pyreon/core\"\n\n// ─── Types ────────────────────────────────────────────────────────────────────\n\nexport interface HeadTag {\n /** HTML tag name */\n tag: \"title\" | \"meta\" | \"link\" | \"script\" | \"style\" | \"base\" | \"noscript\"\n /**\n * Deduplication key. Tags with the same key replace each other;\n * innermost component (last added) wins.\n * Example: all components setting the page title use key \"title\".\n */\n key?: string\n /** HTML attributes for the tag */\n props?: Record<string, string>\n /** Text content — for <title>, <script>, <style>, <noscript> */\n children?: string\n}\n\nexport interface UseHeadInput {\n title?: string\n /**\n * Title template — use `%s` as a placeholder for the page title.\n * Applied to the resolved title after deduplication.\n * @example useHead({ titleTemplate: \"%s | My App\" })\n */\n titleTemplate?: string | ((title: string) => string)\n meta?: Record<string, string>[]\n link?: Record<string, string>[]\n script?: ({ src?: string; children?: string } & Record<string, string | undefined>)[]\n style?: ({ children: string } & Record<string, string | undefined>)[]\n noscript?: { children: string }[]\n /** Convenience: emits a <script type=\"application/ld+json\"> tag with JSON.stringify'd content */\n jsonLd?: Record<string, unknown> | Record<string, unknown>[]\n base?: Record<string, string>\n /** Attributes to set on the <html> element (e.g. { lang: \"en\", dir: \"ltr\" }) */\n htmlAttrs?: Record<string, string>\n /** Attributes to set on the <body> element (e.g. { class: \"dark\" }) */\n bodyAttrs?: Record<string, string>\n}\n\n// ─── Context ──────────────────────────────────────────────────────────────────\n\nexport interface HeadEntry {\n tags: HeadTag[]\n titleTemplate?: string | ((title: string) => string) | undefined\n htmlAttrs?: Record<string, string> | undefined\n bodyAttrs?: Record<string, string> | undefined\n}\n\nexport interface HeadContextValue {\n add(id: symbol, entry: HeadEntry): void\n remove(id: symbol): void\n /** Returns deduplicated tags — last-added entry wins per key */\n resolve(): HeadTag[]\n /** Returns the merged titleTemplate (last-added wins) */\n resolveTitleTemplate(): (string | ((title: string) => string)) | undefined\n /** Returns merged htmlAttrs (later entries override earlier) */\n resolveHtmlAttrs(): Record<string, string>\n /** Returns merged bodyAttrs (later entries override earlier) */\n resolveBodyAttrs(): Record<string, string>\n}\n\nexport function createHeadContext(): HeadContextValue {\n const map = new Map<symbol, HeadEntry>()\n return {\n add(id, entry) {\n map.set(id, entry)\n },\n remove(id) {\n map.delete(id)\n },\n resolve() {\n const keyed = new Map<string, HeadTag>()\n const unkeyed: HeadTag[] = []\n for (const entry of map.values()) {\n for (const tag of entry.tags) {\n if (tag.key) keyed.set(tag.key, tag)\n else unkeyed.push(tag)\n }\n }\n return [...keyed.values(), ...unkeyed]\n },\n resolveTitleTemplate() {\n let template: (string | ((title: string) => string)) | undefined\n for (const entry of map.values()) {\n if (entry.titleTemplate !== undefined) template = entry.titleTemplate\n }\n return template\n },\n resolveHtmlAttrs() {\n const attrs: Record<string, string> = {}\n for (const entry of map.values()) {\n if (entry.htmlAttrs) Object.assign(attrs, entry.htmlAttrs)\n }\n return attrs\n },\n resolveBodyAttrs() {\n const attrs: Record<string, string> = {}\n for (const entry of map.values()) {\n if (entry.bodyAttrs) Object.assign(attrs, entry.bodyAttrs)\n }\n return attrs\n },\n }\n}\n\nexport const HeadContext = createContext<HeadContextValue | null>(null)\n","import type { ComponentFn, Props, VNodeChild } from \"@pyreon/core\"\nimport { onUnmount, popContext, pushContext } from \"@pyreon/core\"\nimport type { HeadContextValue } from \"./context\"\nimport { createHeadContext, HeadContext } from \"./context\"\n\nexport interface HeadProviderProps extends Props {\n context?: HeadContextValue | undefined\n children?: VNodeChild\n}\n\n/**\n * Provides a HeadContextValue to all descendant components.\n * Wrap your app root with this to enable useHead() throughout the tree.\n *\n * If no `context` prop is passed, a new HeadContext is created automatically.\n *\n * @example\n * // Auto-create context:\n * <HeadProvider><App /></HeadProvider>\n *\n * // Explicit context (e.g. for SSR):\n * const headCtx = createHeadContext()\n * mount(h(HeadProvider, { context: headCtx }, h(App, null)), root)\n */\nexport const HeadProvider: ComponentFn<HeadProviderProps> = (props) => {\n const ctx = props.context ?? createHeadContext()\n // Push context frame synchronously (before children mount) so all descendants\n // can read HeadContext via useContext(). Pop on unmount for correct cleanup.\n const frame = new Map([[HeadContext.id, ctx]])\n pushContext(frame)\n onUnmount(() => popContext())\n\n const ch = props.children\n return (typeof ch === \"function\" ? (ch as () => VNodeChild)() : ch) as ReturnType<ComponentFn>\n}\n","import type { ComponentFn, VNode } from \"@pyreon/core\"\nimport { h, pushContext } from \"@pyreon/core\"\nimport { renderToString } from \"@pyreon/runtime-server\"\nimport type { HeadTag } from \"./context\"\nimport { createHeadContext, HeadContext } from \"./context\"\n\nconst VOID_TAGS = new Set([\"meta\", \"link\", \"base\"])\n\n/**\n * Render a Pyreon app to an HTML fragment + a serialized <head> string.\n *\n * The returned `head` string can be injected directly into your HTML template:\n *\n * @example\n * const { html, head } = await renderWithHead(h(App, null))\n * const page = `<!DOCTYPE html>\n * <html>\n * <head>\n * <meta charset=\"UTF-8\" />\n * ${head}\n * </head>\n * <body><div id=\"app\">${html}</div></body>\n * </html>`\n */\nexport interface RenderWithHeadResult {\n html: string\n head: string\n /** Attributes to set on the <html> element */\n htmlAttrs: Record<string, string>\n /** Attributes to set on the <body> element */\n bodyAttrs: Record<string, string>\n}\n\nexport async function renderWithHead(app: VNode): Promise<RenderWithHeadResult> {\n const ctx = createHeadContext()\n\n // HeadInjector runs inside renderToString's ALS scope, so pushContext reaches\n // the per-request context stack rather than the module-level fallback stack.\n function HeadInjector(): VNode {\n pushContext(new Map([[HeadContext.id, ctx]]))\n return app\n }\n\n const html = await renderToString(h(HeadInjector as ComponentFn, null))\n const titleTemplate = ctx.resolveTitleTemplate()\n const head = ctx\n .resolve()\n .map((tag) => serializeTag(tag, titleTemplate))\n .join(\"\\n \")\n return {\n html,\n head,\n htmlAttrs: ctx.resolveHtmlAttrs(),\n bodyAttrs: ctx.resolveBodyAttrs(),\n }\n}\n\nfunction serializeTag(tag: HeadTag, titleTemplate?: string | ((title: string) => string)): string {\n if (tag.tag === \"title\") {\n const raw = tag.children || \"\"\n const title = titleTemplate\n ? typeof titleTemplate === \"function\"\n ? titleTemplate(raw)\n : titleTemplate.replace(/%s/g, raw)\n : raw\n return `<title>${esc(title)}</title>`\n }\n const props = tag.props as Record<string, string> | undefined\n const attrs = props\n ? Object.entries(props)\n .map(([k, v]) => `${k}=\"${esc(v)}\"`)\n .join(\" \")\n : \"\"\n const open = attrs ? `<${tag.tag} ${attrs}` : `<${tag.tag}`\n if (VOID_TAGS.has(tag.tag)) return `${open} />`\n const content = tag.children || \"\"\n // Escape sequences that could break out of script/style/noscript blocks:\n // 1. Closing tags like </script> — use Unicode escape in the slash\n // 2. HTML comment openers <!-- that could confuse parsers\n const body = content.replace(/<\\/(script|style|noscript)/gi, \"<\\\\/$1\").replace(/<!--/g, \"<\\\\!--\")\n return `${open}>${body}</${tag.tag}>`\n}\n\nfunction esc(s: string): string {\n return s\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n}\n","import type { HeadContextValue } from \"./context\"\n\nconst ATTR = \"data-pyreon-head\"\n\n/**\n * Sync the resolved head tags to the real DOM <head>.\n * Uses incremental diffing: matches existing elements by key, patches attributes\n * in-place, adds new elements, and removes stale ones.\n * Also syncs htmlAttrs, bodyAttrs, and applies titleTemplate.\n * No-op on the server (typeof document === \"undefined\").\n */\nfunction patchExistingTag(\n found: Element,\n tag: { props: Record<string, unknown>; children: string },\n kept: Set<Element>,\n): void {\n kept.add(found)\n patchAttrs(found, tag.props as Record<string, string>)\n const content = String(tag.children)\n if (found.textContent !== content) found.textContent = content\n}\n\nfunction createNewTag(tag: {\n tag: string\n props: Record<string, unknown>\n children: string\n key: unknown\n}): void {\n const el = document.createElement(tag.tag)\n el.setAttribute(ATTR, tag.key as string)\n for (const [k, v] of Object.entries(tag.props as Record<string, string>)) {\n el.setAttribute(k, v)\n }\n if (tag.children) el.textContent = tag.children\n document.head.appendChild(el)\n}\n\nexport function syncDom(ctx: HeadContextValue): void {\n if (typeof document === \"undefined\") return\n\n const tags = ctx.resolve()\n const titleTemplate = ctx.resolveTitleTemplate()\n const existing = document.head.querySelectorAll(`[${ATTR}]`)\n const byKey = new Map<string, Element>()\n for (const el of existing) {\n byKey.set(el.getAttribute(ATTR) as string, el)\n }\n\n const kept = new Set<Element>()\n\n for (const tag of tags) {\n if (tag.tag === \"title\") {\n document.title = applyTitleTemplate(String(tag.children), titleTemplate)\n continue\n }\n\n const key = tag.key as string\n const found = byKey.get(key)\n\n if (found && found.tagName.toLowerCase() === tag.tag) {\n patchExistingTag(found, tag as { props: Record<string, unknown>; children: string }, kept)\n } else {\n createNewTag(\n tag as { tag: string; props: Record<string, unknown>; children: string; key: unknown },\n )\n }\n }\n\n for (const el of existing) {\n if (!kept.has(el)) el.remove()\n }\n\n syncElementAttrs(document.documentElement, ctx.resolveHtmlAttrs())\n syncElementAttrs(document.body, ctx.resolveBodyAttrs())\n}\n\n/** Patch an element's attributes to match the desired props. */\nfunction patchAttrs(el: Element, props: Record<string, string>): void {\n for (let i = el.attributes.length - 1; i >= 0; i--) {\n const attr = el.attributes[i]\n if (!attr || attr.name === ATTR) continue\n if (!(attr.name in props)) el.removeAttribute(attr.name)\n }\n for (const [k, v] of Object.entries(props)) {\n if (el.getAttribute(k) !== v) el.setAttribute(k, v)\n }\n}\n\nfunction applyTitleTemplate(\n title: string,\n template: string | ((t: string) => string) | undefined,\n): string {\n if (!template) return title\n if (typeof template === \"function\") return template(title)\n return template.replace(/%s/g, title)\n}\n\n/** Sync pyreon-managed attributes on <html> or <body>. */\nfunction syncElementAttrs(el: Element, attrs: Record<string, string>): void {\n // Remove previously managed attrs that are no longer present\n const managed = el.getAttribute(`${ATTR}-attrs`)\n if (managed) {\n for (const name of managed.split(\",\")) {\n if (name && !(name in attrs)) el.removeAttribute(name)\n }\n }\n const keys: string[] = []\n for (const [k, v] of Object.entries(attrs)) {\n keys.push(k)\n if (el.getAttribute(k) !== v) el.setAttribute(k, v)\n }\n if (keys.length > 0) {\n el.setAttribute(`${ATTR}-attrs`, keys.join(\",\"))\n } else if (managed) {\n el.removeAttribute(`${ATTR}-attrs`)\n }\n}\n","import { onMount, onUnmount, useContext } from \"@pyreon/core\"\nimport { effect } from \"@pyreon/reactivity\"\nimport type { HeadEntry, HeadTag, UseHeadInput } from \"./context\"\nimport { HeadContext } from \"./context\"\nimport { syncDom } from \"./dom\"\n\nfunction buildEntry(o: UseHeadInput): HeadEntry {\n const tags: HeadTag[] = []\n if (o.title != null) tags.push({ tag: \"title\", key: \"title\", children: o.title })\n o.meta?.forEach((m, i) => {\n tags.push({\n tag: \"meta\",\n key: m.name ?? m.property ?? `meta-${i}`,\n props: m,\n })\n })\n o.link?.forEach((l, i) => {\n tags.push({\n tag: \"link\",\n key: l.href ? `link-${l.rel || \"\"}-${l.href}` : l.rel ? `link-${l.rel}` : `link-${i}`,\n props: l,\n })\n })\n o.script?.forEach((s, i) => {\n const { children, ...rest } = s\n tags.push({\n tag: \"script\",\n key: s.src ?? `script-${i}`,\n props: rest as Record<string, string>,\n ...(children != null ? { children } : {}),\n })\n })\n o.style?.forEach((s, i) => {\n const { children, ...rest } = s\n tags.push({\n tag: \"style\",\n key: `style-${i}`,\n props: rest as Record<string, string>,\n children,\n })\n })\n o.noscript?.forEach((ns, i) => {\n tags.push({ tag: \"noscript\", key: `noscript-${i}`, children: ns.children })\n })\n if (o.jsonLd) {\n tags.push({\n tag: \"script\",\n key: \"jsonld\",\n props: { type: \"application/ld+json\" },\n children: JSON.stringify(o.jsonLd),\n })\n }\n if (o.base) tags.push({ tag: \"base\", key: \"base\", props: o.base })\n return {\n tags,\n titleTemplate: o.titleTemplate,\n htmlAttrs: o.htmlAttrs,\n bodyAttrs: o.bodyAttrs,\n }\n}\n\n/**\n * Register head tags (title, meta, link, script, style, noscript, base, jsonLd)\n * for the current component.\n *\n * Accepts a static object or a reactive getter:\n * useHead({ title: \"My Page\", meta: [{ name: \"description\", content: \"...\" }] })\n * useHead(() => ({ title: `${count()} items` })) // updates when signal changes\n *\n * Tags are deduplicated by key — innermost component wins.\n * Requires a <HeadProvider> (CSR) or renderWithHead() (SSR) ancestor.\n */\nexport function useHead(input: UseHeadInput | (() => UseHeadInput)): void {\n const ctx = useContext(HeadContext)\n if (!ctx) return // no HeadProvider — silently no-op\n\n const id = Symbol()\n\n if (typeof input === \"function\") {\n if (typeof document !== \"undefined\") {\n // CSR: reactive — re-register whenever signals change\n effect(() => {\n ctx.add(id, buildEntry(input()))\n syncDom(ctx)\n })\n } else {\n // SSR: evaluate once synchronously (no effects on server)\n ctx.add(id, buildEntry(input()))\n }\n } else {\n ctx.add(id, buildEntry(input))\n // onMount is a no-op in SSR; syncDom has its own typeof document guard\n onMount(() => {\n syncDom(ctx)\n return undefined\n })\n }\n\n onUnmount(() => {\n ctx.remove(id)\n // syncDom has its own typeof document guard — no need to check here\n syncDom(ctx)\n })\n}\n"],"mappings":";;;;;AA+DA,SAAgB,oBAAsC;CACpD,MAAM,sBAAM,IAAI,KAAwB;AACxC,QAAO;EACL,IAAI,IAAI,OAAO;AACb,OAAI,IAAI,IAAI,MAAM;;EAEpB,OAAO,IAAI;AACT,OAAI,OAAO,GAAG;;EAEhB,UAAU;GACR,MAAM,wBAAQ,IAAI,KAAsB;GACxC,MAAM,UAAqB,EAAE;AAC7B,QAAK,MAAM,SAAS,IAAI,QAAQ,CAC9B,MAAK,MAAM,OAAO,MAAM,KACtB,KAAI,IAAI,IAAK,OAAM,IAAI,IAAI,KAAK,IAAI;OAC/B,SAAQ,KAAK,IAAI;AAG1B,UAAO,CAAC,GAAG,MAAM,QAAQ,EAAE,GAAG,QAAQ;;EAExC,uBAAuB;GACrB,IAAI;AACJ,QAAK,MAAM,SAAS,IAAI,QAAQ,CAC9B,KAAI,MAAM,kBAAkB,OAAW,YAAW,MAAM;AAE1D,UAAO;;EAET,mBAAmB;GACjB,MAAM,QAAgC,EAAE;AACxC,QAAK,MAAM,SAAS,IAAI,QAAQ,CAC9B,KAAI,MAAM,UAAW,QAAO,OAAO,OAAO,MAAM,UAAU;AAE5D,UAAO;;EAET,mBAAmB;GACjB,MAAM,QAAgC,EAAE;AACxC,QAAK,MAAM,SAAS,IAAI,QAAQ,CAC9B,KAAI,MAAM,UAAW,QAAO,OAAO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,KAAK,UAAU,EAAE,OAAO;EACnC,CAAC;AAEJ,KAAI,EAAE,KAAM,MAAK,KAAK;EAAE,KAAK;EAAQ,KAAK;EAAQ,OAAO,EAAE;EAAM,CAAC;AAClE,QAAO;EACL;EACA,eAAe,EAAE;EACjB,WAAW,EAAE;EACb,WAAW,EAAE;EACd;;;;;;;;;;;;;AAcH,SAAgB,QAAQ,OAAkD;CACxE,MAAM,MAAM,WAAW,YAAY;AACnC,KAAI,CAAC,IAAK;CAEV,MAAM,KAAK,QAAQ;AAEnB,KAAI,OAAO,UAAU,WACnB,KAAI,OAAO,aAAa,YAEtB,cAAa;AACX,MAAI,IAAI,IAAI,WAAW,OAAO,CAAC,CAAC;AAChC,UAAQ,IAAI;GACZ;KAGF,KAAI,IAAI,IAAI,WAAW,OAAO,CAAC,CAAC;MAE7B;AACL,MAAI,IAAI,IAAI,WAAW,MAAM,CAAC;AAE9B,gBAAc;AACZ,WAAQ,IAAI;IAEZ;;AAGJ,iBAAgB;AACd,MAAI,OAAO,GAAG;AAEd,UAAQ,IAAI;GACZ"}
@@ -0,0 +1,250 @@
1
+ import { createContext, h, onMount, onUnmount, popContext, pushContext, useContext } from "@pyreon/core";
2
+ import { renderToString } from "@pyreon/runtime-server";
3
+ import { effect } from "@pyreon/reactivity";
4
+
5
+ //#region src/context.ts
6
+ function createHeadContext() {
7
+ const map = /* @__PURE__ */new Map();
8
+ return {
9
+ add(id, entry) {
10
+ map.set(id, entry);
11
+ },
12
+ remove(id) {
13
+ map.delete(id);
14
+ },
15
+ resolve() {
16
+ const keyed = /* @__PURE__ */new Map();
17
+ const unkeyed = [];
18
+ for (const entry of map.values()) for (const tag of entry.tags) if (tag.key) keyed.set(tag.key, tag);else unkeyed.push(tag);
19
+ return [...keyed.values(), ...unkeyed];
20
+ },
21
+ resolveTitleTemplate() {
22
+ let template;
23
+ for (const entry of map.values()) if (entry.titleTemplate !== void 0) template = entry.titleTemplate;
24
+ return template;
25
+ },
26
+ resolveHtmlAttrs() {
27
+ const attrs = {};
28
+ for (const entry of map.values()) if (entry.htmlAttrs) Object.assign(attrs, entry.htmlAttrs);
29
+ return attrs;
30
+ },
31
+ resolveBodyAttrs() {
32
+ const attrs = {};
33
+ for (const entry of map.values()) if (entry.bodyAttrs) Object.assign(attrs, entry.bodyAttrs);
34
+ return attrs;
35
+ }
36
+ };
37
+ }
38
+ async function renderWithHead(app) {
39
+ const ctx = createHeadContext();
40
+ function HeadInjector() {
41
+ pushContext(new Map([[HeadContext.id, ctx]]));
42
+ return app;
43
+ }
44
+ const html = await renderToString(h(HeadInjector, null));
45
+ const titleTemplate = ctx.resolveTitleTemplate();
46
+ return {
47
+ html,
48
+ head: ctx.resolve().map(tag => serializeTag(tag, titleTemplate)).join("\n "),
49
+ htmlAttrs: ctx.resolveHtmlAttrs(),
50
+ bodyAttrs: ctx.resolveBodyAttrs()
51
+ };
52
+ }
53
+ function serializeTag(tag, titleTemplate) {
54
+ if (tag.tag === "title") {
55
+ const raw = tag.children || "";
56
+ return `<title>${esc(titleTemplate ? typeof titleTemplate === "function" ? titleTemplate(raw) : titleTemplate.replace(/%s/g, raw) : raw)}</title>`;
57
+ }
58
+ const props = tag.props;
59
+ const attrs = props ? Object.entries(props).map(([k, v]) => `${k}="${esc(v)}"`).join(" ") : "";
60
+ const open = attrs ? `<${tag.tag} ${attrs}` : `<${tag.tag}`;
61
+ if (VOID_TAGS.has(tag.tag)) return `${open} />`;
62
+ return `${open}>${(tag.children || "").replace(/<\/(script|style|noscript)/gi, "<\\/$1").replace(/<!--/g, "<\\!--")}</${tag.tag}>`;
63
+ }
64
+ function esc(s) {
65
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
66
+ }
67
+
68
+ //#endregion
69
+ //#region src/dom.ts
70
+
71
+ /**
72
+ * Sync the resolved head tags to the real DOM <head>.
73
+ * Uses incremental diffing: matches existing elements by key, patches attributes
74
+ * in-place, adds new elements, and removes stale ones.
75
+ * Also syncs htmlAttrs, bodyAttrs, and applies titleTemplate.
76
+ * No-op on the server (typeof document === "undefined").
77
+ */
78
+ function patchExistingTag(found, tag, kept) {
79
+ kept.add(found);
80
+ patchAttrs(found, tag.props);
81
+ const content = String(tag.children);
82
+ if (found.textContent !== content) found.textContent = content;
83
+ }
84
+ function createNewTag(tag) {
85
+ const el = document.createElement(tag.tag);
86
+ el.setAttribute(ATTR, tag.key);
87
+ for (const [k, v] of Object.entries(tag.props)) el.setAttribute(k, v);
88
+ if (tag.children) el.textContent = tag.children;
89
+ document.head.appendChild(el);
90
+ }
91
+ function syncDom(ctx) {
92
+ if (typeof document === "undefined") return;
93
+ const tags = ctx.resolve();
94
+ const titleTemplate = ctx.resolveTitleTemplate();
95
+ const existing = document.head.querySelectorAll(`[${ATTR}]`);
96
+ const byKey = /* @__PURE__ */new Map();
97
+ for (const el of existing) byKey.set(el.getAttribute(ATTR), el);
98
+ const kept = /* @__PURE__ */new Set();
99
+ for (const tag of tags) {
100
+ if (tag.tag === "title") {
101
+ document.title = applyTitleTemplate(String(tag.children), titleTemplate);
102
+ continue;
103
+ }
104
+ const key = tag.key;
105
+ const found = byKey.get(key);
106
+ if (found && found.tagName.toLowerCase() === tag.tag) patchExistingTag(found, tag, kept);else createNewTag(tag);
107
+ }
108
+ for (const el of existing) if (!kept.has(el)) el.remove();
109
+ syncElementAttrs(document.documentElement, ctx.resolveHtmlAttrs());
110
+ syncElementAttrs(document.body, ctx.resolveBodyAttrs());
111
+ }
112
+ /** Patch an element's attributes to match the desired props. */
113
+ function patchAttrs(el, props) {
114
+ for (let i = el.attributes.length - 1; i >= 0; i--) {
115
+ const attr = el.attributes[i];
116
+ if (!attr || attr.name === ATTR) continue;
117
+ if (!(attr.name in props)) el.removeAttribute(attr.name);
118
+ }
119
+ for (const [k, v] of Object.entries(props)) if (el.getAttribute(k) !== v) el.setAttribute(k, v);
120
+ }
121
+ function applyTitleTemplate(title, template) {
122
+ if (!template) return title;
123
+ if (typeof template === "function") return template(title);
124
+ return template.replace(/%s/g, title);
125
+ }
126
+ /** Sync pyreon-managed attributes on <html> or <body>. */
127
+ function syncElementAttrs(el, attrs) {
128
+ const managed = el.getAttribute(`${ATTR}-attrs`);
129
+ if (managed) {
130
+ for (const name of managed.split(",")) if (name && !(name in attrs)) el.removeAttribute(name);
131
+ }
132
+ const keys = [];
133
+ for (const [k, v] of Object.entries(attrs)) {
134
+ keys.push(k);
135
+ if (el.getAttribute(k) !== v) el.setAttribute(k, v);
136
+ }
137
+ if (keys.length > 0) el.setAttribute(`${ATTR}-attrs`, keys.join(","));else if (managed) el.removeAttribute(`${ATTR}-attrs`);
138
+ }
139
+
140
+ //#endregion
141
+ //#region src/use-head.ts
142
+ function buildEntry(o) {
143
+ const tags = [];
144
+ if (o.title != null) tags.push({
145
+ tag: "title",
146
+ key: "title",
147
+ children: o.title
148
+ });
149
+ o.meta?.forEach((m, i) => {
150
+ tags.push({
151
+ tag: "meta",
152
+ key: m.name ?? m.property ?? `meta-${i}`,
153
+ props: m
154
+ });
155
+ });
156
+ o.link?.forEach((l, i) => {
157
+ tags.push({
158
+ tag: "link",
159
+ key: l.href ? `link-${l.rel || ""}-${l.href}` : l.rel ? `link-${l.rel}` : `link-${i}`,
160
+ props: l
161
+ });
162
+ });
163
+ o.script?.forEach((s, i) => {
164
+ const {
165
+ children,
166
+ ...rest
167
+ } = s;
168
+ tags.push({
169
+ tag: "script",
170
+ key: s.src ?? `script-${i}`,
171
+ props: rest,
172
+ ...(children != null ? {
173
+ children
174
+ } : {})
175
+ });
176
+ });
177
+ o.style?.forEach((s, i) => {
178
+ const {
179
+ children,
180
+ ...rest
181
+ } = s;
182
+ tags.push({
183
+ tag: "style",
184
+ key: `style-${i}`,
185
+ props: rest,
186
+ children
187
+ });
188
+ });
189
+ o.noscript?.forEach((ns, i) => {
190
+ tags.push({
191
+ tag: "noscript",
192
+ key: `noscript-${i}`,
193
+ children: ns.children
194
+ });
195
+ });
196
+ if (o.jsonLd) tags.push({
197
+ tag: "script",
198
+ key: "jsonld",
199
+ props: {
200
+ type: "application/ld+json"
201
+ },
202
+ children: JSON.stringify(o.jsonLd)
203
+ });
204
+ if (o.base) tags.push({
205
+ tag: "base",
206
+ key: "base",
207
+ props: o.base
208
+ });
209
+ return {
210
+ tags,
211
+ titleTemplate: o.titleTemplate,
212
+ htmlAttrs: o.htmlAttrs,
213
+ bodyAttrs: o.bodyAttrs
214
+ };
215
+ }
216
+ /**
217
+ * Register head tags (title, meta, link, script, style, noscript, base, jsonLd)
218
+ * for the current component.
219
+ *
220
+ * Accepts a static object or a reactive getter:
221
+ * useHead({ title: "My Page", meta: [{ name: "description", content: "..." }] })
222
+ * useHead(() => ({ title: `${count()} items` })) // updates when signal changes
223
+ *
224
+ * Tags are deduplicated by key — innermost component wins.
225
+ * Requires a <HeadProvider> (CSR) or renderWithHead() (SSR) ancestor.
226
+ */
227
+ function useHead(input) {
228
+ const ctx = useContext(HeadContext);
229
+ if (!ctx) return;
230
+ const id = Symbol();
231
+ if (typeof input === "function") {
232
+ if (typeof document !== "undefined") effect(() => {
233
+ ctx.add(id, buildEntry(input()));
234
+ syncDom(ctx);
235
+ });else ctx.add(id, buildEntry(input()));
236
+ } else {
237
+ ctx.add(id, buildEntry(input));
238
+ onMount(() => {
239
+ syncDom(ctx);
240
+ });
241
+ }
242
+ onUnmount(() => {
243
+ ctx.remove(id);
244
+ syncDom(ctx);
245
+ });
246
+ }
247
+
248
+ //#endregion
249
+ export { HeadContext, HeadProvider, createHeadContext, renderWithHead, useHead };
250
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/context.ts","../../src/provider.tsx","../../src/ssr.ts","../../src/dom.ts","../../src/use-head.ts"],"mappings":";;;;;AA+DA,SAAgB,iBAAA,CAAA,EAAsC;EACpD,MAAM,GAAA,GAAA,eAAM,IAAI,GAAA,CAAA,CAAwB;EACxC,OAAO;IACL,GAAA,CAAI,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