@alepha/react 0.14.0 → 0.14.1
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/README.md +1 -1
- package/dist/head/index.browser.js +189 -17
- package/dist/head/index.browser.js.map +1 -1
- package/dist/head/index.d.ts +101 -24
- package/dist/head/index.d.ts.map +1 -1
- package/dist/head/index.js +195 -18
- package/dist/head/index.js.map +1 -1
- package/package.json +3 -3
- package/src/head/helpers/SeoExpander.ts +141 -0
- package/src/head/index.browser.ts +1 -0
- package/src/head/index.ts +1 -0
- package/src/head/interfaces/Head.ts +66 -27
- package/src/head/providers/BrowserHeadProvider.ts +45 -12
- package/src/head/providers/HeadProvider.ts +26 -7
- package/src/head/providers/ServerHeadProvider.ts +14 -2
package/dist/head/index.js
CHANGED
|
@@ -3,25 +3,174 @@ import { $hook, $inject, $module, Alepha, KIND, Primitive, createPrimitive } fro
|
|
|
3
3
|
import { ServerTimingProvider } from "alepha/server";
|
|
4
4
|
import { useCallback, useEffect, useMemo } from "react";
|
|
5
5
|
|
|
6
|
+
//#region ../../src/head/helpers/SeoExpander.ts
|
|
7
|
+
/**
|
|
8
|
+
* Expands Head configuration into SEO meta tags.
|
|
9
|
+
*
|
|
10
|
+
* Generates:
|
|
11
|
+
* - `<meta name="description">` from head.description
|
|
12
|
+
* - `<meta property="og:*">` OpenGraph tags
|
|
13
|
+
* - `<meta name="twitter:*">` Twitter Card tags
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const helper = new SeoExpander();
|
|
18
|
+
* const { meta, link } = helper.expand({
|
|
19
|
+
* title: "My App",
|
|
20
|
+
* description: "Build amazing apps",
|
|
21
|
+
* image: "https://example.com/og.png",
|
|
22
|
+
* url: "https://example.com/",
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
var SeoExpander = class {
|
|
27
|
+
expand(head) {
|
|
28
|
+
const meta = [];
|
|
29
|
+
const link = [];
|
|
30
|
+
if (!(head.description || head.image || head.url || head.siteName || head.locale || head.type || head.og || head.twitter)) return {
|
|
31
|
+
meta,
|
|
32
|
+
link
|
|
33
|
+
};
|
|
34
|
+
if (head.description) meta.push({
|
|
35
|
+
name: "description",
|
|
36
|
+
content: head.description
|
|
37
|
+
});
|
|
38
|
+
if (head.url) link.push({
|
|
39
|
+
rel: "canonical",
|
|
40
|
+
href: head.url
|
|
41
|
+
});
|
|
42
|
+
this.expandOpenGraph(head, meta);
|
|
43
|
+
this.expandTwitter(head, meta);
|
|
44
|
+
return {
|
|
45
|
+
meta,
|
|
46
|
+
link
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
expandOpenGraph(head, meta) {
|
|
50
|
+
const ogTitle = head.og?.title ?? head.title;
|
|
51
|
+
const ogDescription = head.og?.description ?? head.description;
|
|
52
|
+
const ogImage = head.og?.image ?? head.image;
|
|
53
|
+
if (head.type || ogTitle) meta.push({
|
|
54
|
+
property: "og:type",
|
|
55
|
+
content: head.type ?? "website"
|
|
56
|
+
});
|
|
57
|
+
if (head.url) meta.push({
|
|
58
|
+
property: "og:url",
|
|
59
|
+
content: head.url
|
|
60
|
+
});
|
|
61
|
+
if (ogTitle) meta.push({
|
|
62
|
+
property: "og:title",
|
|
63
|
+
content: ogTitle
|
|
64
|
+
});
|
|
65
|
+
if (ogDescription) meta.push({
|
|
66
|
+
property: "og:description",
|
|
67
|
+
content: ogDescription
|
|
68
|
+
});
|
|
69
|
+
if (ogImage) {
|
|
70
|
+
meta.push({
|
|
71
|
+
property: "og:image",
|
|
72
|
+
content: ogImage
|
|
73
|
+
});
|
|
74
|
+
if (head.imageWidth) meta.push({
|
|
75
|
+
property: "og:image:width",
|
|
76
|
+
content: String(head.imageWidth)
|
|
77
|
+
});
|
|
78
|
+
if (head.imageHeight) meta.push({
|
|
79
|
+
property: "og:image:height",
|
|
80
|
+
content: String(head.imageHeight)
|
|
81
|
+
});
|
|
82
|
+
if (head.imageAlt) meta.push({
|
|
83
|
+
property: "og:image:alt",
|
|
84
|
+
content: head.imageAlt
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (head.siteName) meta.push({
|
|
88
|
+
property: "og:site_name",
|
|
89
|
+
content: head.siteName
|
|
90
|
+
});
|
|
91
|
+
if (head.locale) meta.push({
|
|
92
|
+
property: "og:locale",
|
|
93
|
+
content: head.locale
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
expandTwitter(head, meta) {
|
|
97
|
+
const twitterTitle = head.twitter?.title ?? head.title;
|
|
98
|
+
const twitterDescription = head.twitter?.description ?? head.description;
|
|
99
|
+
const twitterImage = head.twitter?.image ?? head.image;
|
|
100
|
+
if (head.twitter?.card || twitterTitle || twitterImage) meta.push({
|
|
101
|
+
name: "twitter:card",
|
|
102
|
+
content: head.twitter?.card ?? (twitterImage ? "summary_large_image" : "summary")
|
|
103
|
+
});
|
|
104
|
+
if (head.url) meta.push({
|
|
105
|
+
name: "twitter:url",
|
|
106
|
+
content: head.url
|
|
107
|
+
});
|
|
108
|
+
if (twitterTitle) meta.push({
|
|
109
|
+
name: "twitter:title",
|
|
110
|
+
content: twitterTitle
|
|
111
|
+
});
|
|
112
|
+
if (twitterDescription) meta.push({
|
|
113
|
+
name: "twitter:description",
|
|
114
|
+
content: twitterDescription
|
|
115
|
+
});
|
|
116
|
+
if (twitterImage) {
|
|
117
|
+
meta.push({
|
|
118
|
+
name: "twitter:image",
|
|
119
|
+
content: twitterImage
|
|
120
|
+
});
|
|
121
|
+
if (head.imageAlt) meta.push({
|
|
122
|
+
name: "twitter:image:alt",
|
|
123
|
+
content: head.imageAlt
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (head.twitter?.site) meta.push({
|
|
127
|
+
name: "twitter:site",
|
|
128
|
+
content: head.twitter.site
|
|
129
|
+
});
|
|
130
|
+
if (head.twitter?.creator) meta.push({
|
|
131
|
+
name: "twitter:creator",
|
|
132
|
+
content: head.twitter.creator
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
6
138
|
//#region ../../src/head/providers/HeadProvider.ts
|
|
7
139
|
var HeadProvider = class {
|
|
140
|
+
seoExpander = $inject(SeoExpander);
|
|
8
141
|
global = [];
|
|
9
142
|
fillHead(state) {
|
|
10
143
|
state.head = { ...state.head };
|
|
11
144
|
for (const h of this.global ?? []) {
|
|
12
145
|
const head = typeof h === "function" ? h() : h;
|
|
13
|
-
state
|
|
14
|
-
...state.head,
|
|
15
|
-
...head,
|
|
16
|
-
meta: [...state.head.meta ?? [], ...head.meta ?? []]
|
|
17
|
-
};
|
|
146
|
+
this.mergeHead(state, head);
|
|
18
147
|
}
|
|
19
148
|
for (const layer of state.layers) if (layer.route?.head && !layer.error) this.fillHeadByPage(layer.route, state, layer.props ?? {});
|
|
20
149
|
}
|
|
150
|
+
mergeHead(state, head) {
|
|
151
|
+
const { meta, link } = this.seoExpander.expand(head);
|
|
152
|
+
state.head = {
|
|
153
|
+
...state.head,
|
|
154
|
+
...head,
|
|
155
|
+
meta: [
|
|
156
|
+
...state.head.meta ?? [],
|
|
157
|
+
...meta,
|
|
158
|
+
...head.meta ?? []
|
|
159
|
+
],
|
|
160
|
+
link: [
|
|
161
|
+
...state.head.link ?? [],
|
|
162
|
+
...link,
|
|
163
|
+
...head.link ?? []
|
|
164
|
+
]
|
|
165
|
+
};
|
|
166
|
+
}
|
|
21
167
|
fillHeadByPage(page, state, props) {
|
|
22
168
|
if (!page.head) return;
|
|
23
169
|
state.head ??= {};
|
|
24
170
|
const head = typeof page.head === "function" ? page.head(props, state.head) : page.head;
|
|
171
|
+
const { meta, link } = this.seoExpander.expand(head);
|
|
172
|
+
state.head.meta = [...state.head.meta ?? [], ...meta];
|
|
173
|
+
state.head.link = [...state.head.link ?? [], ...link];
|
|
25
174
|
if (head.title) {
|
|
26
175
|
state.head ??= {};
|
|
27
176
|
if (state.head.titleSeparator) state.head.title = `${head.title}${state.head.titleSeparator}${state.head.title}`;
|
|
@@ -37,6 +186,7 @@ var HeadProvider = class {
|
|
|
37
186
|
...head.bodyAttributes
|
|
38
187
|
};
|
|
39
188
|
if (head.meta) state.head.meta = [...state.head.meta ?? [], ...head.meta ?? []];
|
|
189
|
+
if (head.link) state.head.link = [...state.head.link ?? [], ...head.link ?? []];
|
|
40
190
|
}
|
|
41
191
|
};
|
|
42
192
|
|
|
@@ -80,7 +230,7 @@ var ServerHeadProvider = class {
|
|
|
80
230
|
const title = head.title;
|
|
81
231
|
if (title) if (template.includes("<title>")) result = result.replace(/<title>(.*?)<\/title>/i, () => `<title>${this.escapeHtml(title)}</title>`);
|
|
82
232
|
else headContent += `<title>${this.escapeHtml(title)}</title>\n`;
|
|
83
|
-
if (head.meta) for (const meta of head.meta) headContent +=
|
|
233
|
+
if (head.meta) for (const meta of head.meta) headContent += this.renderMetaTag(meta);
|
|
84
234
|
if (head.link) for (const link of head.link) headContent += `<link rel="${this.escapeHtml(link.rel)}" href="${this.escapeHtml(link.href)}">\n`;
|
|
85
235
|
result = result.replace(/<head([^>]*)>(.*?)<\/head>/is, (_, existingAttrs, existingHead) => `<head${existingAttrs}>${existingHead}${headContent}</head>`);
|
|
86
236
|
return result.trim();
|
|
@@ -106,6 +256,11 @@ var ServerHeadProvider = class {
|
|
|
106
256
|
escapeHtml(str) {
|
|
107
257
|
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
108
258
|
}
|
|
259
|
+
renderMetaTag(meta) {
|
|
260
|
+
if (meta.property) return `<meta property="${this.escapeHtml(meta.property)}" content="${this.escapeHtml(meta.content)}">\n`;
|
|
261
|
+
if (meta.name) return `<meta name="${this.escapeHtml(meta.name)}" content="${this.escapeHtml(meta.content)}">\n`;
|
|
262
|
+
return "";
|
|
263
|
+
}
|
|
109
264
|
};
|
|
110
265
|
|
|
111
266
|
//#endregion
|
|
@@ -154,6 +309,14 @@ var BrowserHeadProvider = class {
|
|
|
154
309
|
content
|
|
155
310
|
});
|
|
156
311
|
}
|
|
312
|
+
for (const meta of document.head.querySelectorAll("meta[property]")) {
|
|
313
|
+
const property = meta.getAttribute("property");
|
|
314
|
+
const content = meta.getAttribute("content");
|
|
315
|
+
if (property && content) metas.push({
|
|
316
|
+
property,
|
|
317
|
+
content
|
|
318
|
+
});
|
|
319
|
+
}
|
|
157
320
|
return metas;
|
|
158
321
|
}
|
|
159
322
|
};
|
|
@@ -164,17 +327,7 @@ var BrowserHeadProvider = class {
|
|
|
164
327
|
else document.body.removeAttribute(key);
|
|
165
328
|
if (head.htmlAttributes) for (const [key, value] of Object.entries(head.htmlAttributes)) if (value) document.documentElement.setAttribute(key, value);
|
|
166
329
|
else document.documentElement.removeAttribute(key);
|
|
167
|
-
if (head.meta) for (const it of head.meta)
|
|
168
|
-
const { name, content } = it;
|
|
169
|
-
const meta = document.querySelector(`meta[name="${name}"]`);
|
|
170
|
-
if (meta) meta.setAttribute("content", content);
|
|
171
|
-
else {
|
|
172
|
-
const newMeta = document.createElement("meta");
|
|
173
|
-
newMeta.setAttribute("name", name);
|
|
174
|
-
newMeta.setAttribute("content", content);
|
|
175
|
-
document.head.appendChild(newMeta);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
330
|
+
if (head.meta) for (const it of head.meta) this.renderMetaTag(document, it);
|
|
178
331
|
if (head.link) for (const it of head.link) {
|
|
179
332
|
const { rel, href } = it;
|
|
180
333
|
let link = document.querySelector(`link[rel="${rel}"][href="${href}"]`);
|
|
@@ -186,6 +339,30 @@ var BrowserHeadProvider = class {
|
|
|
186
339
|
}
|
|
187
340
|
}
|
|
188
341
|
}
|
|
342
|
+
renderMetaTag(document, meta) {
|
|
343
|
+
const { content } = meta;
|
|
344
|
+
if (meta.property) {
|
|
345
|
+
const existing = document.querySelector(`meta[property="${meta.property}"]`);
|
|
346
|
+
if (existing) existing.setAttribute("content", content);
|
|
347
|
+
else {
|
|
348
|
+
const newMeta = document.createElement("meta");
|
|
349
|
+
newMeta.setAttribute("property", meta.property);
|
|
350
|
+
newMeta.setAttribute("content", content);
|
|
351
|
+
document.head.appendChild(newMeta);
|
|
352
|
+
}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (meta.name) {
|
|
356
|
+
const existing = document.querySelector(`meta[name="${meta.name}"]`);
|
|
357
|
+
if (existing) existing.setAttribute("content", content);
|
|
358
|
+
else {
|
|
359
|
+
const newMeta = document.createElement("meta");
|
|
360
|
+
newMeta.setAttribute("name", meta.name);
|
|
361
|
+
newMeta.setAttribute("content", content);
|
|
362
|
+
document.head.appendChild(newMeta);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
189
366
|
};
|
|
190
367
|
|
|
191
368
|
//#endregion
|
|
@@ -242,5 +419,5 @@ const AlephaReactHead = $module({
|
|
|
242
419
|
});
|
|
243
420
|
|
|
244
421
|
//#endregion
|
|
245
|
-
export { $head, AlephaReactHead, HeadPrimitive, ServerHeadProvider, useHead };
|
|
422
|
+
export { $head, AlephaReactHead, HeadPrimitive, SeoExpander, ServerHeadProvider, useHead };
|
|
246
423
|
//# sourceMappingURL=index.js.map
|
package/dist/head/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["attrs: Record<string, string>","match: RegExpExecArray | null","attrs: Record<string, string>","metas: { name: string; content: string }[]"],"sources":["../../src/head/providers/HeadProvider.ts","../../src/head/primitives/$head.ts","../../src/head/providers/ServerHeadProvider.ts","../../src/head/providers/BrowserHeadProvider.ts","../../src/head/hooks/useHead.ts","../../src/head/index.ts"],"sourcesContent":["import type { PageRoute, ReactRouterState } from \"@alepha/react\";\nimport type { Head } from \"../interfaces/Head.ts\";\n\nexport class HeadProvider {\n public global?: Array<Head | (() => Head)> = [];\n\n public fillHead(state: ReactRouterState) {\n state.head = {\n ...state.head,\n };\n\n for (const h of this.global ?? []) {\n const head =\n typeof h === \"function\" ? h() : h;\n state.head = {\n ...state.head,\n ...head,\n meta: [...(state.head.meta ?? []), ...(head.meta ?? [])],\n };\n }\n\n for (const layer of state.layers) {\n if (layer.route?.head && !layer.error) {\n this.fillHeadByPage(layer.route, state, layer.props ?? {});\n }\n }\n }\n\n protected fillHeadByPage(\n page: PageRoute,\n state: ReactRouterState,\n props: Record<string, any>,\n ): void {\n if (!page.head) {\n return;\n }\n\n state.head ??= {};\n\n const head =\n typeof page.head === \"function\"\n ? page.head(props, state.head)\n : page.head;\n\n if (head.title) {\n state.head ??= {};\n\n if (state.head.titleSeparator) {\n state.head.title = `${head.title}${state.head.titleSeparator}${state.head.title}`;\n } else {\n state.head.title = head.title;\n }\n\n state.head.titleSeparator = head.titleSeparator;\n }\n\n if (head.htmlAttributes) {\n state.head.htmlAttributes = {\n ...state.head.htmlAttributes,\n ...head.htmlAttributes,\n };\n }\n\n if (head.bodyAttributes) {\n state.head.bodyAttributes = {\n ...state.head.bodyAttributes,\n ...head.bodyAttributes,\n };\n }\n\n if (head.meta) {\n state.head.meta = [...(state.head.meta ?? []), ...(head.meta ?? [])];\n }\n }\n}\n","import { $inject, createPrimitive, Primitive, KIND } from \"alepha\";\nimport type { Head } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"../providers/HeadProvider.ts\";\n\n/**\n * Set global `<head>` options for the application.\n */\nexport const $head = (options: HeadPrimitiveOptions) => {\n return createPrimitive(HeadPrimitive, options);\n};\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport type HeadPrimitiveOptions = Head | (() => Head);\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport class HeadPrimitive extends Primitive<HeadPrimitiveOptions> {\n protected readonly provider = $inject(HeadProvider);\n protected onInit() {\n this.provider.global = [\n ...(this.provider.global ?? []),\n this.options,\n ];\n }\n}\n\n$head[KIND] = HeadPrimitive;\n","import { $hook, $inject } from \"alepha\";\nimport { ServerTimingProvider } from \"alepha/server\";\nimport type { SimpleHead } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"./HeadProvider.ts\";\n\nexport class ServerHeadProvider {\n protected readonly headProvider = $inject(HeadProvider);\n protected readonly serverTimingProvider = $inject(ServerTimingProvider);\n\n protected readonly onServerRenderEnd = $hook({\n on: \"react:server:render:end\",\n handler: async (ev) => {\n this.serverTimingProvider.beginTiming(\"renderHead\");\n this.headProvider.fillHead(ev.state);\n if (ev.state.head) {\n ev.html = this.renderHead(ev.html, ev.state.head);\n }\n this.serverTimingProvider.endTiming(\"renderHead\");\n },\n });\n\n public renderHead(template: string, head: SimpleHead): string {\n let result = template;\n\n // Inject htmlAttributes\n const htmlAttributes = head.htmlAttributes;\n if (htmlAttributes) {\n result = result.replace(\n /<html([^>]*)>/i,\n (_, existingAttrs) =>\n `<html${this.mergeAttributes(existingAttrs, htmlAttributes)}>`,\n );\n }\n\n // Inject bodyAttributes\n const bodyAttributes = head.bodyAttributes;\n if (bodyAttributes) {\n result = result.replace(\n /<body([^>]*)>/i,\n (_, existingAttrs) =>\n `<body${this.mergeAttributes(existingAttrs, bodyAttributes)}>`,\n );\n }\n\n // Build head content\n let headContent = \"\";\n const title = head.title;\n if (title) {\n if (template.includes(\"<title>\")) {\n result = result.replace(\n /<title>(.*?)<\\/title>/i,\n () => `<title>${this.escapeHtml(title)}</title>`,\n );\n } else {\n headContent += `<title>${this.escapeHtml(title)}</title>\\n`;\n }\n }\n\n if (head.meta) {\n for (const meta of head.meta) {\n headContent += `<meta name=\"${this.escapeHtml(meta.name)}\" content=\"${this.escapeHtml(meta.content)}\">\\n`;\n }\n }\n\n if (head.link) {\n for (const link of head.link) {\n headContent += `<link rel=\"${this.escapeHtml(link.rel)}\" href=\"${this.escapeHtml(link.href)}\">\\n`;\n }\n }\n\n // Inject into <head>...</head>\n result = result.replace(\n /<head([^>]*)>(.*?)<\\/head>/is,\n (_, existingAttrs, existingHead) =>\n `<head${existingAttrs}>${existingHead}${headContent}</head>`,\n );\n\n return result.trim();\n }\n\n protected mergeAttributes(\n existing: string,\n attrs: Record<string, string>,\n ): string {\n const existingAttrs = this.parseAttributes(existing);\n const merged = { ...existingAttrs, ...attrs };\n return Object.entries(merged)\n .map(([k, v]) => ` ${k}=\"${this.escapeHtml(v)}\"`)\n .join(\"\");\n }\n\n protected parseAttributes(attrStr: string): Record<string, string> {\n attrStr = attrStr.replaceAll(\"'\", '\"');\n\n const attrs: Record<string, string> = {};\n const attrRegex = /([^\\s=]+)(?:=\"([^\"]*)\")?/g;\n let match: RegExpExecArray | null = attrRegex.exec(attrStr);\n\n while (match) {\n attrs[match[1]] = match[2] ?? \"\";\n match = attrRegex.exec(attrStr);\n }\n\n return attrs;\n }\n\n protected escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\");\n }\n}\n","import { $hook, $inject } from \"alepha\";\nimport type { Head } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"./HeadProvider.ts\";\n\nexport class BrowserHeadProvider {\n protected readonly headProvider = $inject(HeadProvider);\n\n protected get document(): Document {\n return window.document;\n }\n\n protected readonly onBrowserRender = $hook({\n on: \"react:browser:render\",\n handler: async ({ state }) => {\n this.headProvider.fillHead(state);\n if (state.head) {\n this.renderHead(this.document, state.head);\n }\n },\n });\n\n protected readonly onTransitionEnd = $hook({\n on: \"react:transition:end\",\n handler: async ({ state }) => {\n this.headProvider.fillHead(state);\n if (state.head) {\n this.renderHead(this.document, state.head);\n }\n },\n });\n\n public getHead(document: Document): Head {\n return {\n get title() {\n return document.title;\n },\n get htmlAttributes() {\n const attrs: Record<string, string> = {};\n for (const attr of document.documentElement.attributes) {\n attrs[attr.name] = attr.value;\n }\n return attrs;\n },\n get bodyAttributes() {\n const attrs: Record<string, string> = {};\n for (const attr of document.body.attributes) {\n attrs[attr.name] = attr.value;\n }\n return attrs;\n },\n get meta() {\n const metas: { name: string; content: string }[] = [];\n for (const meta of document.head.querySelectorAll(\"meta[name]\")) {\n const name = meta.getAttribute(\"name\");\n const content = meta.getAttribute(\"content\");\n if (name && content) {\n metas.push({ name, content });\n }\n }\n return metas;\n },\n };\n }\n\n public renderHead(document: Document, head: Head): void {\n if (head.title) {\n document.title = head.title;\n }\n\n if (head.bodyAttributes) {\n for (const [key, value] of Object.entries(head.bodyAttributes)) {\n if (value) {\n document.body.setAttribute(key, value);\n } else {\n document.body.removeAttribute(key);\n }\n }\n }\n\n if (head.htmlAttributes) {\n for (const [key, value] of Object.entries(head.htmlAttributes)) {\n if (value) {\n document.documentElement.setAttribute(key, value);\n } else {\n document.documentElement.removeAttribute(key);\n }\n }\n }\n\n if (head.meta) {\n for (const it of head.meta) {\n const { name, content } = it;\n const meta = document.querySelector(`meta[name=\"${name}\"]`);\n if (meta) {\n meta.setAttribute(\"content\", content);\n } else {\n const newMeta = document.createElement(\"meta\");\n newMeta.setAttribute(\"name\", name);\n newMeta.setAttribute(\"content\", content);\n document.head.appendChild(newMeta);\n }\n }\n }\n\n if (head.link) {\n for (const it of head.link) {\n const { rel, href } = it;\n let link = document.querySelector(`link[rel=\"${rel}\"][href=\"${href}\"]`);\n if (!link) {\n link = document.createElement(\"link\");\n link.setAttribute(\"rel\", rel);\n link.setAttribute(\"href\", href);\n document.head.appendChild(link);\n }\n }\n }\n }\n}\n","import { useInject } from \"@alepha/react\";\nimport { Alepha } from \"alepha\";\nimport { useCallback, useEffect, useMemo } from \"react\";\nimport type { Head } from \"../interfaces/Head.ts\";\nimport { BrowserHeadProvider } from \"../providers/BrowserHeadProvider.ts\";\n\n/**\n * ```tsx\n * const App = () => {\n * const [head, setHead] = useHead({\n * // will set the document title on the first render\n * title: \"My App\",\n * });\n *\n * return (\n * // This will update the document title when the button is clicked\n * <button onClick={() => setHead({ title: \"Change Title\" })}>\n * Change Title {head.title}\n * </button>\n * );\n * }\n * ```\n */\nexport const useHead = (options?: UseHeadOptions): UseHeadReturn => {\n const alepha = useInject(Alepha);\n\n const current = useMemo(() => {\n if (!alepha.isBrowser()) {\n return {};\n }\n\n return alepha.inject(BrowserHeadProvider).getHead(window.document);\n }, []);\n\n const setHead = useCallback((head?: Head | ((previous?: Head) => Head)) => {\n if (!alepha.isBrowser()) {\n return;\n }\n\n alepha\n .inject(BrowserHeadProvider)\n .renderHead(\n window.document,\n typeof head === \"function\" ? head(current) : head || {},\n );\n }, []);\n\n useEffect(() => {\n if (options) {\n setHead(options);\n }\n }, []);\n\n return [current, setHead];\n};\n\nexport type UseHeadOptions = Head | ((previous?: Head) => Head);\n\nexport type UseHeadReturn = [\n Head,\n (head?: Head | ((previous?: Head) => Head)) => void,\n];\n","import {\n AlephaReact,\n type PageConfigSchema,\n type TPropsDefault,\n type TPropsParentDefault,\n} from \"@alepha/react\";\nimport { $module } from \"alepha\";\nimport { $head } from \"./primitives/$head.ts\";\nimport type { Head } from \"./interfaces/Head.ts\";\nimport { ServerHeadProvider } from \"./providers/ServerHeadProvider.ts\";\nimport { HeadProvider } from \"./providers/HeadProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./primitives/$head.ts\";\nexport * from \"./hooks/useHead.ts\";\nexport * from \"./interfaces/Head.ts\";\nexport * from \"./providers/ServerHeadProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\ndeclare module \"@alepha/react\" {\n interface PagePrimitiveOptions<\n TConfig extends PageConfigSchema = PageConfigSchema,\n TProps extends object = TPropsDefault,\n TPropsParent extends object = TPropsParentDefault,\n > {\n head?: Head | ((props: TProps, previous?: Head) => Head);\n }\n\n interface ReactRouterState {\n head: Head;\n }\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Fill `<head>` server & client side.\n *\n * @see {@link ServerHeadProvider}\n * @module alepha.react.head\n */\nexport const AlephaReactHead = $module({\n name: \"alepha.react.head\",\n primitives: [$head],\n services: [AlephaReact, ServerHeadProvider, HeadProvider],\n});\n"],"mappings":";;;;;;AAGA,IAAa,eAAb,MAA0B;CACxB,AAAO,SAAsC,EAAE;CAE/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Ac,OAAO;AAC9C,YAAQ,aAAa,QAAQ,KAAK;AAClC,YAAQ,aAAa,WAAW,QAAQ;AACxC,aAAS,KAAK,YAAY,QAAQ;;;AAKxC,MAAI,KAAK,KACP,MAAK,MAAM,MAAM,KAAK,MAAM;GAC1B,MAAM,EAAE,KAAK,SAAS;GACtB,IAAI,OAAO,SAAS,cAAc,aAAa,IAAI,WAAW,KAAK,IAAI;AACvE,OAAI,CAAC,MAAM;AACT,WAAO,SAAS,cAAc,OAAO;AACrC,SAAK,aAAa,OAAO,IAAI;AAC7B,SAAK,aAAa,QAAQ,KAAK;AAC/B,aAAS,KAAK,YAAY,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;ACzFzC,MAAa,WAAW,YAA4C;CAClE,MAAM,SAAS,UAAU,OAAO;CAEhC,MAAM,UAAU,cAAc;AAC5B,MAAI,CAAC,OAAO,WAAW,CACrB,QAAO,EAAE;AAGX,SAAO,OAAO,OAAO,oBAAoB,CAAC,QAAQ,OAAO,SAAS;IACjE,EAAE,CAAC;CAEN,MAAM,UAAU,aAAa,SAA8C;AACzE,MAAI,CAAC,OAAO,WAAW,CACrB;AAGF,SACG,OAAO,oBAAoB,CAC3B,WACC,OAAO,UACP,OAAO,SAAS,aAAa,KAAK,QAAQ,GAAG,QAAQ,EAAE,CACxD;IACF,EAAE,CAAC;AAEN,iBAAgB;AACd,MAAI,QACF,SAAQ,QAAQ;IAEjB,EAAE,CAAC;AAEN,QAAO,CAAC,SAAS,QAAQ;;;;;;;;;;;ACV3B,MAAa,kBAAkB,QAAQ;CACrC,MAAM;CACN,YAAY,CAAC,MAAM;CACnB,UAAU;EAAC;EAAa;EAAoB;EAAa;CAC1D,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["meta: HeadMeta[]","link: Array<{ rel: string; href: string }>","attrs: Record<string, string>","match: RegExpExecArray | null","attrs: Record<string, string>","metas: HeadMeta[]"],"sources":["../../src/head/helpers/SeoExpander.ts","../../src/head/providers/HeadProvider.ts","../../src/head/primitives/$head.ts","../../src/head/providers/ServerHeadProvider.ts","../../src/head/providers/BrowserHeadProvider.ts","../../src/head/hooks/useHead.ts","../../src/head/index.ts"],"sourcesContent":["import type { Head, HeadMeta } from \"../interfaces/Head.ts\";\n\n/**\n * Expands Head configuration into SEO meta tags.\n *\n * Generates:\n * - `<meta name=\"description\">` from head.description\n * - `<meta property=\"og:*\">` OpenGraph tags\n * - `<meta name=\"twitter:*\">` Twitter Card tags\n *\n * @example\n * ```ts\n * const helper = new SeoExpander();\n * const { meta, link } = helper.expand({\n * title: \"My App\",\n * description: \"Build amazing apps\",\n * image: \"https://example.com/og.png\",\n * url: \"https://example.com/\",\n * });\n * ```\n */\nexport class SeoExpander {\n public expand(head: Head): {\n meta: HeadMeta[];\n link: Array<{ rel: string; href: string }>;\n } {\n const meta: HeadMeta[] = [];\n const link: Array<{ rel: string; href: string }> = [];\n\n // Only expand SEO if there's meaningful content beyond just title\n const hasSeoContent =\n head.description ||\n head.image ||\n head.url ||\n head.siteName ||\n head.locale ||\n head.type ||\n head.og ||\n head.twitter;\n\n if (!hasSeoContent) {\n return { meta, link };\n }\n\n // Base description\n if (head.description) {\n meta.push({ name: \"description\", content: head.description });\n }\n\n // Canonical URL\n if (head.url) {\n link.push({ rel: \"canonical\", href: head.url });\n }\n\n // OpenGraph tags\n this.expandOpenGraph(head, meta);\n\n // Twitter Card tags\n this.expandTwitter(head, meta);\n\n return { meta, link };\n }\n\n protected expandOpenGraph(head: Head, meta: HeadMeta[]): void {\n const ogTitle = head.og?.title ?? head.title;\n const ogDescription = head.og?.description ?? head.description;\n const ogImage = head.og?.image ?? head.image;\n\n if (head.type || ogTitle) {\n meta.push({ property: \"og:type\", content: head.type ?? \"website\" });\n }\n if (head.url) {\n meta.push({ property: \"og:url\", content: head.url });\n }\n if (ogTitle) {\n meta.push({ property: \"og:title\", content: ogTitle });\n }\n if (ogDescription) {\n meta.push({ property: \"og:description\", content: ogDescription });\n }\n if (ogImage) {\n meta.push({ property: \"og:image\", content: ogImage });\n if (head.imageWidth) {\n meta.push({\n property: \"og:image:width\",\n content: String(head.imageWidth),\n });\n }\n if (head.imageHeight) {\n meta.push({\n property: \"og:image:height\",\n content: String(head.imageHeight),\n });\n }\n if (head.imageAlt) {\n meta.push({ property: \"og:image:alt\", content: head.imageAlt });\n }\n }\n if (head.siteName) {\n meta.push({ property: \"og:site_name\", content: head.siteName });\n }\n if (head.locale) {\n meta.push({ property: \"og:locale\", content: head.locale });\n }\n }\n\n protected expandTwitter(head: Head, meta: HeadMeta[]): void {\n const twitterTitle = head.twitter?.title ?? head.title;\n const twitterDescription = head.twitter?.description ?? head.description;\n const twitterImage = head.twitter?.image ?? head.image;\n\n if (head.twitter?.card || twitterTitle || twitterImage) {\n meta.push({\n name: \"twitter:card\",\n content:\n head.twitter?.card ?? (twitterImage ? \"summary_large_image\" : \"summary\"),\n });\n }\n if (head.url) {\n meta.push({ name: \"twitter:url\", content: head.url });\n }\n if (twitterTitle) {\n meta.push({ name: \"twitter:title\", content: twitterTitle });\n }\n if (twitterDescription) {\n meta.push({ name: \"twitter:description\", content: twitterDescription });\n }\n if (twitterImage) {\n meta.push({ name: \"twitter:image\", content: twitterImage });\n if (head.imageAlt) {\n meta.push({ name: \"twitter:image:alt\", content: head.imageAlt });\n }\n }\n if (head.twitter?.site) {\n meta.push({ name: \"twitter:site\", content: head.twitter.site });\n }\n if (head.twitter?.creator) {\n meta.push({ name: \"twitter:creator\", content: head.twitter.creator });\n }\n }\n}\n","import type { PageRoute, ReactRouterState } from \"@alepha/react\";\nimport { $inject } from \"alepha\";\nimport { SeoExpander } from \"../helpers/SeoExpander.ts\";\nimport type { Head } from \"../interfaces/Head.ts\";\n\nexport class HeadProvider {\n protected readonly seoExpander = $inject(SeoExpander);\n\n public global?: Array<Head | (() => Head)> = [];\n\n public fillHead(state: ReactRouterState) {\n state.head = {\n ...state.head,\n };\n\n for (const h of this.global ?? []) {\n const head = typeof h === \"function\" ? h() : h;\n this.mergeHead(state, head);\n }\n\n for (const layer of state.layers) {\n if (layer.route?.head && !layer.error) {\n this.fillHeadByPage(layer.route, state, layer.props ?? {});\n }\n }\n }\n\n protected mergeHead(state: ReactRouterState, head: Head): void {\n // Expand SEO fields into meta tags\n const { meta, link } = this.seoExpander.expand(head);\n state.head = {\n ...state.head,\n ...head,\n meta: [...(state.head.meta ?? []), ...meta, ...(head.meta ?? [])],\n link: [...(state.head.link ?? []), ...link, ...(head.link ?? [])],\n };\n }\n\n protected fillHeadByPage(\n page: PageRoute,\n state: ReactRouterState,\n props: Record<string, any>,\n ): void {\n if (!page.head) {\n return;\n }\n\n state.head ??= {};\n\n const head =\n typeof page.head === \"function\"\n ? page.head(props, state.head)\n : page.head;\n\n // Expand SEO fields into meta tags\n const { meta, link } = this.seoExpander.expand(head);\n state.head.meta = [...(state.head.meta ?? []), ...meta];\n state.head.link = [...(state.head.link ?? []), ...link];\n\n if (head.title) {\n state.head ??= {};\n\n if (state.head.titleSeparator) {\n state.head.title = `${head.title}${state.head.titleSeparator}${state.head.title}`;\n } else {\n state.head.title = head.title;\n }\n\n state.head.titleSeparator = head.titleSeparator;\n }\n\n if (head.htmlAttributes) {\n state.head.htmlAttributes = {\n ...state.head.htmlAttributes,\n ...head.htmlAttributes,\n };\n }\n\n if (head.bodyAttributes) {\n state.head.bodyAttributes = {\n ...state.head.bodyAttributes,\n ...head.bodyAttributes,\n };\n }\n\n if (head.meta) {\n state.head.meta = [...(state.head.meta ?? []), ...(head.meta ?? [])];\n }\n\n if (head.link) {\n state.head.link = [...(state.head.link ?? []), ...(head.link ?? [])];\n }\n }\n}\n","import { $inject, createPrimitive, Primitive, KIND } from \"alepha\";\nimport type { Head } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"../providers/HeadProvider.ts\";\n\n/**\n * Set global `<head>` options for the application.\n */\nexport const $head = (options: HeadPrimitiveOptions) => {\n return createPrimitive(HeadPrimitive, options);\n};\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport type HeadPrimitiveOptions = Head | (() => Head);\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport class HeadPrimitive extends Primitive<HeadPrimitiveOptions> {\n protected readonly provider = $inject(HeadProvider);\n protected onInit() {\n this.provider.global = [\n ...(this.provider.global ?? []),\n this.options,\n ];\n }\n}\n\n$head[KIND] = HeadPrimitive;\n","import { $hook, $inject } from \"alepha\";\nimport { ServerTimingProvider } from \"alepha/server\";\nimport type { HeadMeta, SimpleHead } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"./HeadProvider.ts\";\n\nexport class ServerHeadProvider {\n protected readonly headProvider = $inject(HeadProvider);\n protected readonly serverTimingProvider = $inject(ServerTimingProvider);\n\n protected readonly onServerRenderEnd = $hook({\n on: \"react:server:render:end\",\n handler: async (ev) => {\n this.serverTimingProvider.beginTiming(\"renderHead\");\n this.headProvider.fillHead(ev.state);\n if (ev.state.head) {\n ev.html = this.renderHead(ev.html, ev.state.head);\n }\n this.serverTimingProvider.endTiming(\"renderHead\");\n },\n });\n\n public renderHead(template: string, head: SimpleHead): string {\n let result = template;\n\n // Inject htmlAttributes\n const htmlAttributes = head.htmlAttributes;\n if (htmlAttributes) {\n result = result.replace(\n /<html([^>]*)>/i,\n (_, existingAttrs) =>\n `<html${this.mergeAttributes(existingAttrs, htmlAttributes)}>`,\n );\n }\n\n // Inject bodyAttributes\n const bodyAttributes = head.bodyAttributes;\n if (bodyAttributes) {\n result = result.replace(\n /<body([^>]*)>/i,\n (_, existingAttrs) =>\n `<body${this.mergeAttributes(existingAttrs, bodyAttributes)}>`,\n );\n }\n\n // Build head content\n let headContent = \"\";\n const title = head.title;\n if (title) {\n if (template.includes(\"<title>\")) {\n result = result.replace(\n /<title>(.*?)<\\/title>/i,\n () => `<title>${this.escapeHtml(title)}</title>`,\n );\n } else {\n headContent += `<title>${this.escapeHtml(title)}</title>\\n`;\n }\n }\n\n if (head.meta) {\n for (const meta of head.meta) {\n headContent += this.renderMetaTag(meta);\n }\n }\n\n if (head.link) {\n for (const link of head.link) {\n headContent += `<link rel=\"${this.escapeHtml(link.rel)}\" href=\"${this.escapeHtml(link.href)}\">\\n`;\n }\n }\n\n // Inject into <head>...</head>\n result = result.replace(\n /<head([^>]*)>(.*?)<\\/head>/is,\n (_, existingAttrs, existingHead) =>\n `<head${existingAttrs}>${existingHead}${headContent}</head>`,\n );\n\n return result.trim();\n }\n\n protected mergeAttributes(\n existing: string,\n attrs: Record<string, string>,\n ): string {\n const existingAttrs = this.parseAttributes(existing);\n const merged = { ...existingAttrs, ...attrs };\n return Object.entries(merged)\n .map(([k, v]) => ` ${k}=\"${this.escapeHtml(v)}\"`)\n .join(\"\");\n }\n\n protected parseAttributes(attrStr: string): Record<string, string> {\n attrStr = attrStr.replaceAll(\"'\", '\"');\n\n const attrs: Record<string, string> = {};\n const attrRegex = /([^\\s=]+)(?:=\"([^\"]*)\")?/g;\n let match: RegExpExecArray | null = attrRegex.exec(attrStr);\n\n while (match) {\n attrs[match[1]] = match[2] ?? \"\";\n match = attrRegex.exec(attrStr);\n }\n\n return attrs;\n }\n\n protected escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\");\n }\n\n protected renderMetaTag(meta: HeadMeta): string {\n // OpenGraph tags use property attribute\n if (meta.property) {\n return `<meta property=\"${this.escapeHtml(meta.property)}\" content=\"${this.escapeHtml(meta.content)}\">\\n`;\n }\n // Standard meta tags use name attribute\n if (meta.name) {\n return `<meta name=\"${this.escapeHtml(meta.name)}\" content=\"${this.escapeHtml(meta.content)}\">\\n`;\n }\n return \"\";\n }\n}\n","import { $hook, $inject } from \"alepha\";\nimport type { Head, HeadMeta } from \"../interfaces/Head.ts\";\nimport { HeadProvider } from \"./HeadProvider.ts\";\n\nexport class BrowserHeadProvider {\n protected readonly headProvider = $inject(HeadProvider);\n\n protected get document(): Document {\n return window.document;\n }\n\n protected readonly onBrowserRender = $hook({\n on: \"react:browser:render\",\n handler: async ({ state }) => {\n this.headProvider.fillHead(state);\n if (state.head) {\n this.renderHead(this.document, state.head);\n }\n },\n });\n\n protected readonly onTransitionEnd = $hook({\n on: \"react:transition:end\",\n handler: async ({ state }) => {\n this.headProvider.fillHead(state);\n if (state.head) {\n this.renderHead(this.document, state.head);\n }\n },\n });\n\n public getHead(document: Document): Head {\n return {\n get title() {\n return document.title;\n },\n get htmlAttributes() {\n const attrs: Record<string, string> = {};\n for (const attr of document.documentElement.attributes) {\n attrs[attr.name] = attr.value;\n }\n return attrs;\n },\n get bodyAttributes() {\n const attrs: Record<string, string> = {};\n for (const attr of document.body.attributes) {\n attrs[attr.name] = attr.value;\n }\n return attrs;\n },\n get meta() {\n const metas: HeadMeta[] = [];\n // Get meta tags with name attribute\n for (const meta of document.head.querySelectorAll(\"meta[name]\")) {\n const name = meta.getAttribute(\"name\");\n const content = meta.getAttribute(\"content\");\n if (name && content) {\n metas.push({ name, content });\n }\n }\n // Get meta tags with property attribute (OpenGraph)\n for (const meta of document.head.querySelectorAll(\"meta[property]\")) {\n const property = meta.getAttribute(\"property\");\n const content = meta.getAttribute(\"content\");\n if (property && content) {\n metas.push({ property, content });\n }\n }\n return metas;\n },\n };\n }\n\n public renderHead(document: Document, head: Head): void {\n if (head.title) {\n document.title = head.title;\n }\n\n if (head.bodyAttributes) {\n for (const [key, value] of Object.entries(head.bodyAttributes)) {\n if (value) {\n document.body.setAttribute(key, value);\n } else {\n document.body.removeAttribute(key);\n }\n }\n }\n\n if (head.htmlAttributes) {\n for (const [key, value] of Object.entries(head.htmlAttributes)) {\n if (value) {\n document.documentElement.setAttribute(key, value);\n } else {\n document.documentElement.removeAttribute(key);\n }\n }\n }\n\n if (head.meta) {\n for (const it of head.meta) {\n this.renderMetaTag(document, it);\n }\n }\n\n if (head.link) {\n for (const it of head.link) {\n const { rel, href } = it;\n let link = document.querySelector(`link[rel=\"${rel}\"][href=\"${href}\"]`);\n if (!link) {\n link = document.createElement(\"link\");\n link.setAttribute(\"rel\", rel);\n link.setAttribute(\"href\", href);\n document.head.appendChild(link);\n }\n }\n }\n }\n\n protected renderMetaTag(document: Document, meta: HeadMeta): void {\n const { content } = meta;\n\n // Handle OpenGraph tags (property attribute)\n if (meta.property) {\n const existing = document.querySelector(\n `meta[property=\"${meta.property}\"]`,\n );\n if (existing) {\n existing.setAttribute(\"content\", content);\n } else {\n const newMeta = document.createElement(\"meta\");\n newMeta.setAttribute(\"property\", meta.property);\n newMeta.setAttribute(\"content\", content);\n document.head.appendChild(newMeta);\n }\n return;\n }\n\n // Handle standard meta tags (name attribute)\n if (meta.name) {\n const existing = document.querySelector(`meta[name=\"${meta.name}\"]`);\n if (existing) {\n existing.setAttribute(\"content\", content);\n } else {\n const newMeta = document.createElement(\"meta\");\n newMeta.setAttribute(\"name\", meta.name);\n newMeta.setAttribute(\"content\", content);\n document.head.appendChild(newMeta);\n }\n }\n }\n}\n","import { useInject } from \"@alepha/react\";\nimport { Alepha } from \"alepha\";\nimport { useCallback, useEffect, useMemo } from \"react\";\nimport type { Head } from \"../interfaces/Head.ts\";\nimport { BrowserHeadProvider } from \"../providers/BrowserHeadProvider.ts\";\n\n/**\n * ```tsx\n * const App = () => {\n * const [head, setHead] = useHead({\n * // will set the document title on the first render\n * title: \"My App\",\n * });\n *\n * return (\n * // This will update the document title when the button is clicked\n * <button onClick={() => setHead({ title: \"Change Title\" })}>\n * Change Title {head.title}\n * </button>\n * );\n * }\n * ```\n */\nexport const useHead = (options?: UseHeadOptions): UseHeadReturn => {\n const alepha = useInject(Alepha);\n\n const current = useMemo(() => {\n if (!alepha.isBrowser()) {\n return {};\n }\n\n return alepha.inject(BrowserHeadProvider).getHead(window.document);\n }, []);\n\n const setHead = useCallback((head?: Head | ((previous?: Head) => Head)) => {\n if (!alepha.isBrowser()) {\n return;\n }\n\n alepha\n .inject(BrowserHeadProvider)\n .renderHead(\n window.document,\n typeof head === \"function\" ? head(current) : head || {},\n );\n }, []);\n\n useEffect(() => {\n if (options) {\n setHead(options);\n }\n }, []);\n\n return [current, setHead];\n};\n\nexport type UseHeadOptions = Head | ((previous?: Head) => Head);\n\nexport type UseHeadReturn = [\n Head,\n (head?: Head | ((previous?: Head) => Head)) => void,\n];\n","import {\n AlephaReact,\n type PageConfigSchema,\n type TPropsDefault,\n type TPropsParentDefault,\n} from \"@alepha/react\";\nimport { $module } from \"alepha\";\nimport { $head } from \"./primitives/$head.ts\";\nimport type { Head } from \"./interfaces/Head.ts\";\nimport { ServerHeadProvider } from \"./providers/ServerHeadProvider.ts\";\nimport { HeadProvider } from \"./providers/HeadProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./primitives/$head.ts\";\nexport * from \"./hooks/useHead.ts\";\nexport * from \"./interfaces/Head.ts\";\nexport * from \"./helpers/SeoExpander.ts\";\nexport * from \"./providers/ServerHeadProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\ndeclare module \"@alepha/react\" {\n interface PagePrimitiveOptions<\n TConfig extends PageConfigSchema = PageConfigSchema,\n TProps extends object = TPropsDefault,\n TPropsParent extends object = TPropsParentDefault,\n > {\n head?: Head | ((props: TProps, previous?: Head) => Head);\n }\n\n interface ReactRouterState {\n head: Head;\n }\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Fill `<head>` server & client side.\n *\n * @see {@link ServerHeadProvider}\n * @module alepha.react.head\n */\nexport const AlephaReactHead = $module({\n name: \"alepha.react.head\",\n primitives: [$head],\n services: [AlephaReact, ServerHeadProvider, HeadProvider],\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,IAAa,cAAb,MAAyB;CACvB,AAAO,OAAO,MAGZ;EACA,MAAMA,OAAmB,EAAE;EAC3B,MAAMC,OAA6C,EAAE;AAarD,MAAI,EATF,KAAK,eACL,KAAK,SACL,KAAK,OACL,KAAK,YACL,KAAK,UACL,KAAK,QACL,KAAK,MACL,KAAK,SAGL,QAAO;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AAI;AACpE,OAAI,SACF,UAAS,aAAa,WAAW,QAAQ;QACpC;IACL,MAAM,UAAU,SAAS,cAAc,OAAO;AAC9C,YAAQ,aAAa,QAAQ,KAAK,KAAK;AACvC,YAAQ,aAAa,WAAW,QAAQ;AACxC,aAAS,KAAK,YAAY,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;AC3H1C,MAAa,WAAW,YAA4C;CAClE,MAAM,SAAS,UAAU,OAAO;CAEhC,MAAM,UAAU,cAAc;AAC5B,MAAI,CAAC,OAAO,WAAW,CACrB,QAAO,EAAE;AAGX,SAAO,OAAO,OAAO,oBAAoB,CAAC,QAAQ,OAAO,SAAS;IACjE,EAAE,CAAC;CAEN,MAAM,UAAU,aAAa,SAA8C;AACzE,MAAI,CAAC,OAAO,WAAW,CACrB;AAGF,SACG,OAAO,oBAAoB,CAC3B,WACC,OAAO,UACP,OAAO,SAAS,aAAa,KAAK,QAAQ,GAAG,QAAQ,EAAE,CACxD;IACF,EAAE,CAAC;AAEN,iBAAgB;AACd,MAAI,QACF,SAAQ,QAAQ;IAEjB,EAAE,CAAC;AAEN,QAAO,CAAC,SAAS,QAAQ;;;;;;;;;;;ACT3B,MAAa,kBAAkB,QAAQ;CACrC,MAAM;CACN,YAAY,CAAC,MAAM;CACnB,UAAU;EAAC;EAAa;EAAoB;EAAa;CAC1D,CAAC"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@alepha/react",
|
|
3
3
|
"description": "React components and hooks for building Alepha applications.",
|
|
4
4
|
"author": "Nicolas Foures",
|
|
5
|
-
"version": "0.14.
|
|
5
|
+
"version": "0.14.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
8
8
|
"node": ">=22.0.0"
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
"@testing-library/react": "^16.3.1",
|
|
26
26
|
"@types/react": "^19",
|
|
27
27
|
"@types/react-dom": "^19",
|
|
28
|
-
"alepha": "0.14.
|
|
28
|
+
"alepha": "0.14.1",
|
|
29
29
|
"jsdom": "^27.4.0",
|
|
30
30
|
"react": "^19.2.3",
|
|
31
31
|
"vitest": "^4.0.16"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"alepha": "0.14.
|
|
34
|
+
"alepha": "0.14.1",
|
|
35
35
|
"react": "^19"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { Head, HeadMeta } from "../interfaces/Head.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Expands Head configuration into SEO meta tags.
|
|
5
|
+
*
|
|
6
|
+
* Generates:
|
|
7
|
+
* - `<meta name="description">` from head.description
|
|
8
|
+
* - `<meta property="og:*">` OpenGraph tags
|
|
9
|
+
* - `<meta name="twitter:*">` Twitter Card tags
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const helper = new SeoExpander();
|
|
14
|
+
* const { meta, link } = helper.expand({
|
|
15
|
+
* title: "My App",
|
|
16
|
+
* description: "Build amazing apps",
|
|
17
|
+
* image: "https://example.com/og.png",
|
|
18
|
+
* url: "https://example.com/",
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class SeoExpander {
|
|
23
|
+
public expand(head: Head): {
|
|
24
|
+
meta: HeadMeta[];
|
|
25
|
+
link: Array<{ rel: string; href: string }>;
|
|
26
|
+
} {
|
|
27
|
+
const meta: HeadMeta[] = [];
|
|
28
|
+
const link: Array<{ rel: string; href: string }> = [];
|
|
29
|
+
|
|
30
|
+
// Only expand SEO if there's meaningful content beyond just title
|
|
31
|
+
const hasSeoContent =
|
|
32
|
+
head.description ||
|
|
33
|
+
head.image ||
|
|
34
|
+
head.url ||
|
|
35
|
+
head.siteName ||
|
|
36
|
+
head.locale ||
|
|
37
|
+
head.type ||
|
|
38
|
+
head.og ||
|
|
39
|
+
head.twitter;
|
|
40
|
+
|
|
41
|
+
if (!hasSeoContent) {
|
|
42
|
+
return { meta, link };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Base description
|
|
46
|
+
if (head.description) {
|
|
47
|
+
meta.push({ name: "description", content: head.description });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Canonical URL
|
|
51
|
+
if (head.url) {
|
|
52
|
+
link.push({ rel: "canonical", href: head.url });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// OpenGraph tags
|
|
56
|
+
this.expandOpenGraph(head, meta);
|
|
57
|
+
|
|
58
|
+
// Twitter Card tags
|
|
59
|
+
this.expandTwitter(head, meta);
|
|
60
|
+
|
|
61
|
+
return { meta, link };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected expandOpenGraph(head: Head, meta: HeadMeta[]): void {
|
|
65
|
+
const ogTitle = head.og?.title ?? head.title;
|
|
66
|
+
const ogDescription = head.og?.description ?? head.description;
|
|
67
|
+
const ogImage = head.og?.image ?? head.image;
|
|
68
|
+
|
|
69
|
+
if (head.type || ogTitle) {
|
|
70
|
+
meta.push({ property: "og:type", content: head.type ?? "website" });
|
|
71
|
+
}
|
|
72
|
+
if (head.url) {
|
|
73
|
+
meta.push({ property: "og:url", content: head.url });
|
|
74
|
+
}
|
|
75
|
+
if (ogTitle) {
|
|
76
|
+
meta.push({ property: "og:title", content: ogTitle });
|
|
77
|
+
}
|
|
78
|
+
if (ogDescription) {
|
|
79
|
+
meta.push({ property: "og:description", content: ogDescription });
|
|
80
|
+
}
|
|
81
|
+
if (ogImage) {
|
|
82
|
+
meta.push({ property: "og:image", content: ogImage });
|
|
83
|
+
if (head.imageWidth) {
|
|
84
|
+
meta.push({
|
|
85
|
+
property: "og:image:width",
|
|
86
|
+
content: String(head.imageWidth),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (head.imageHeight) {
|
|
90
|
+
meta.push({
|
|
91
|
+
property: "og:image:height",
|
|
92
|
+
content: String(head.imageHeight),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (head.imageAlt) {
|
|
96
|
+
meta.push({ property: "og:image:alt", content: head.imageAlt });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (head.siteName) {
|
|
100
|
+
meta.push({ property: "og:site_name", content: head.siteName });
|
|
101
|
+
}
|
|
102
|
+
if (head.locale) {
|
|
103
|
+
meta.push({ property: "og:locale", content: head.locale });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected expandTwitter(head: Head, meta: HeadMeta[]): void {
|
|
108
|
+
const twitterTitle = head.twitter?.title ?? head.title;
|
|
109
|
+
const twitterDescription = head.twitter?.description ?? head.description;
|
|
110
|
+
const twitterImage = head.twitter?.image ?? head.image;
|
|
111
|
+
|
|
112
|
+
if (head.twitter?.card || twitterTitle || twitterImage) {
|
|
113
|
+
meta.push({
|
|
114
|
+
name: "twitter:card",
|
|
115
|
+
content:
|
|
116
|
+
head.twitter?.card ?? (twitterImage ? "summary_large_image" : "summary"),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (head.url) {
|
|
120
|
+
meta.push({ name: "twitter:url", content: head.url });
|
|
121
|
+
}
|
|
122
|
+
if (twitterTitle) {
|
|
123
|
+
meta.push({ name: "twitter:title", content: twitterTitle });
|
|
124
|
+
}
|
|
125
|
+
if (twitterDescription) {
|
|
126
|
+
meta.push({ name: "twitter:description", content: twitterDescription });
|
|
127
|
+
}
|
|
128
|
+
if (twitterImage) {
|
|
129
|
+
meta.push({ name: "twitter:image", content: twitterImage });
|
|
130
|
+
if (head.imageAlt) {
|
|
131
|
+
meta.push({ name: "twitter:image:alt", content: head.imageAlt });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (head.twitter?.site) {
|
|
135
|
+
meta.push({ name: "twitter:site", content: head.twitter.site });
|
|
136
|
+
}
|
|
137
|
+
if (head.twitter?.creator) {
|
|
138
|
+
meta.push({ name: "twitter:creator", content: head.twitter.creator });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -8,6 +8,7 @@ import { BrowserHeadProvider } from "./providers/BrowserHeadProvider.ts";
|
|
|
8
8
|
export * from "./primitives/$head.ts";
|
|
9
9
|
export * from "./hooks/useHead.ts";
|
|
10
10
|
export * from "./interfaces/Head.ts";
|
|
11
|
+
export * from "./helpers/SeoExpander.ts";
|
|
11
12
|
export * from "./providers/BrowserHeadProvider.ts";
|
|
12
13
|
|
|
13
14
|
// ---------------------------------------------------------------------------------------------------------------------
|
package/src/head/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { HeadProvider } from "./providers/HeadProvider.ts";
|
|
|
15
15
|
export * from "./primitives/$head.ts";
|
|
16
16
|
export * from "./hooks/useHead.ts";
|
|
17
17
|
export * from "./interfaces/Head.ts";
|
|
18
|
+
export * from "./helpers/SeoExpander.ts";
|
|
18
19
|
export * from "./providers/ServerHeadProvider.ts";
|
|
19
20
|
|
|
20
21
|
// ---------------------------------------------------------------------------------------------------------------------
|