@pithyjs/ssr 0.0.0-stage → 0.1.2-beta.9
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/LICENSE +21 -0
- package/README.md +100 -2
- package/bin/pithy-ssg.mjs +23 -0
- package/dist/browser.d.ts +38 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +40 -0
- package/dist/browser.js.map +1 -0
- package/dist/build/cli-args.d.ts +104 -0
- package/dist/build/cli-args.d.ts.map +1 -0
- package/dist/build/cli-args.js +88 -0
- package/dist/build/cli-args.js.map +1 -0
- package/dist/build/cli-exit.d.ts +95 -0
- package/dist/build/cli-exit.d.ts.map +1 -0
- package/dist/build/cli-exit.js +237 -0
- package/dist/build/cli-exit.js.map +1 -0
- package/dist/build/cli.d.ts +11 -0
- package/dist/build/cli.d.ts.map +1 -0
- package/dist/build/cli.js +93 -0
- package/dist/build/cli.js.map +1 -0
- package/dist/build/collect-css.d.ts +168 -0
- package/dist/build/collect-css.d.ts.map +1 -0
- package/dist/build/collect-css.js +479 -0
- package/dist/build/collect-css.js.map +1 -0
- package/dist/build/css-live-props.d.ts +89 -0
- package/dist/build/css-live-props.d.ts.map +1 -0
- package/dist/build/css-live-props.js +704 -0
- package/dist/build/css-live-props.js.map +1 -0
- package/dist/build/css-purge.d.ts +153 -0
- package/dist/build/css-purge.d.ts.map +1 -0
- package/dist/build/css-purge.js +463 -0
- package/dist/build/css-purge.js.map +1 -0
- package/dist/build/index.d.ts +24 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +31 -0
- package/dist/build/index.js.map +1 -0
- package/dist/build/not-found.d.ts +94 -0
- package/dist/build/not-found.d.ts.map +1 -0
- package/dist/build/not-found.js +99 -0
- package/dist/build/not-found.js.map +1 -0
- package/dist/build/relative-links.d.ts +31 -0
- package/dist/build/relative-links.d.ts.map +1 -0
- package/dist/build/relative-links.js +79 -0
- package/dist/build/relative-links.js.map +1 -0
- package/dist/build/shell-head.d.ts +68 -0
- package/dist/build/shell-head.d.ts.map +1 -0
- package/dist/build/shell-head.js +236 -0
- package/dist/build/shell-head.js.map +1 -0
- package/dist/build/sprite-ssg.d.ts +61 -0
- package/dist/build/sprite-ssg.d.ts.map +1 -0
- package/dist/build/sprite-ssg.js +114 -0
- package/dist/build/sprite-ssg.js.map +1 -0
- package/dist/build/ssg.d.ts +117 -0
- package/dist/build/ssg.d.ts.map +1 -0
- package/dist/build/ssg.js +404 -0
- package/dist/build/ssg.js.map +1 -0
- package/dist/build/vite-ssg.d.ts +396 -0
- package/dist/build/vite-ssg.d.ts.map +1 -0
- package/dist/build/vite-ssg.js +1130 -0
- package/dist/build/vite-ssg.js.map +1 -0
- package/dist/client/bootstrap.d.ts +51 -0
- package/dist/client/bootstrap.d.ts.map +1 -0
- package/dist/client/bootstrap.js +121 -0
- package/dist/client/bootstrap.js.map +1 -0
- package/dist/client/hydrate.d.ts +60 -0
- package/dist/client/hydrate.d.ts.map +1 -0
- package/dist/client/hydrate.js +168 -0
- package/dist/client/hydrate.js.map +1 -0
- package/dist/client/index.d.ts +21 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +16 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/server/app.d.ts +166 -0
- package/dist/server/app.d.ts.map +1 -0
- package/dist/server/app.js +200 -0
- package/dist/server/app.js.map +1 -0
- package/dist/server/context.d.ts +238 -0
- package/dist/server/context.d.ts.map +1 -0
- package/dist/server/context.js +371 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/environment.d.ts +88 -0
- package/dist/server/environment.d.ts.map +1 -0
- package/dist/server/environment.js +261 -0
- package/dist/server/environment.js.map +1 -0
- package/dist/server/html-shell.d.ts +41 -0
- package/dist/server/html-shell.d.ts.map +1 -0
- package/dist/server/html-shell.js +208 -0
- package/dist/server/html-shell.js.map +1 -0
- package/dist/server/index.d.ts +26 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +37 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/layout-taint.d.ts +62 -0
- package/dist/server/layout-taint.d.ts.map +1 -0
- package/dist/server/layout-taint.js +313 -0
- package/dist/server/layout-taint.js.map +1 -0
- package/dist/server/loaders.d.ts +194 -0
- package/dist/server/loaders.d.ts.map +1 -0
- package/dist/server/loaders.js +479 -0
- package/dist/server/loaders.js.map +1 -0
- package/dist/server/render-route.d.ts +102 -0
- package/dist/server/render-route.d.ts.map +1 -0
- package/dist/server/render-route.js +182 -0
- package/dist/server/render-route.js.map +1 -0
- package/dist/server/render.d.ts +80 -0
- package/dist/server/render.d.ts.map +1 -0
- package/dist/server/render.js +256 -0
- package/dist/server/render.js.map +1 -0
- package/dist/server/router-adapter.d.ts +134 -0
- package/dist/server/router-adapter.d.ts.map +1 -0
- package/dist/server/router-adapter.js +225 -0
- package/dist/server/router-adapter.js.map +1 -0
- package/dist/server/serialization.d.ts +54 -0
- package/dist/server/serialization.d.ts.map +1 -0
- package/dist/server/serialization.js +287 -0
- package/dist/server/serialization.js.map +1 -0
- package/dist/server/ssr-worker.d.ts +18 -0
- package/dist/server/ssr-worker.d.ts.map +1 -0
- package/dist/server/ssr-worker.js +118 -0
- package/dist/server/ssr-worker.js.map +1 -0
- package/dist/server/utils.d.ts +11 -0
- package/dist/server/utils.d.ts.map +1 -0
- package/dist/server/utils.js +14 -0
- package/dist/server/utils.js.map +1 -0
- package/dist/server/worker-pool.d.ts +153 -0
- package/dist/server/worker-pool.d.ts.map +1 -0
- package/dist/server/worker-pool.js +451 -0
- package/dist/server/worker-pool.js.map +1 -0
- package/dist/server/worker-protocol.d.ts +61 -0
- package/dist/server/worker-protocol.d.ts.map +1 -0
- package/dist/server/worker-protocol.js +9 -0
- package/dist/server/worker-protocol.js.map +1 -0
- package/dist/shared/css-purge.d.ts +71 -0
- package/dist/shared/css-purge.d.ts.map +1 -0
- package/dist/shared/css-purge.js +177 -0
- package/dist/shared/css-purge.js.map +1 -0
- package/dist/shared/entry-path.d.ts +41 -0
- package/dist/shared/entry-path.d.ts.map +1 -0
- package/dist/shared/entry-path.js +87 -0
- package/dist/shared/entry-path.js.map +1 -0
- package/dist/shared/head.d.ts +87 -0
- package/dist/shared/head.d.ts.map +1 -0
- package/dist/shared/head.js +412 -0
- package/dist/shared/head.js.map +1 -0
- package/dist/shared/shell-helpers.d.ts +69 -0
- package/dist/shared/shell-helpers.d.ts.map +1 -0
- package/dist/shared/shell-helpers.js +160 -0
- package/dist/shared/shell-helpers.js.map +1 -0
- package/dist/shared/types.d.ts +332 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +27 -0
- package/dist/shared/types.js.map +1 -0
- package/package.json +84 -3
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.shared.head",
|
|
4
|
+
"title": "Head API for SEO",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* @pithyjs/ssr - Head API
|
|
10
|
+
*
|
|
11
|
+
* Functions for managing document head (title, meta, links) during SSR and on client.
|
|
12
|
+
* On server: collects head state in SSR context
|
|
13
|
+
* On client: manipulates DOM directly
|
|
14
|
+
*/
|
|
15
|
+
import type { HeadConfig, HeadState, MetaTag } from './types.js';
|
|
16
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"setContextGetter","stability":"stable","signature":"(getter: () => SSRContext | undefined) => void"} */
|
|
17
|
+
/**
|
|
18
|
+
* Wire up SSR context access for the head() function.
|
|
19
|
+
*
|
|
20
|
+
* Called automatically when '@pithyjs/ssr/server' is imported.
|
|
21
|
+
* This bridges the shared head module to the server's AsyncLocalStorage-based
|
|
22
|
+
* context without creating a direct import dependency.
|
|
23
|
+
*
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export declare function setContextGetter(getter: () => import('./types.js').SSRContext | undefined): void;
|
|
27
|
+
/**
|
|
28
|
+
* Get unique key for a meta tag (for deduplication)
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function getMetaKey(meta: MetaTag): string;
|
|
33
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"head","stability":"stable","signature":"(config: HeadConfig) => void"} */
|
|
34
|
+
/**
|
|
35
|
+
* Declare head tags for current route/component
|
|
36
|
+
*
|
|
37
|
+
* Can be called multiple times - values are merged.
|
|
38
|
+
* On server: collects into SSR context
|
|
39
|
+
* On client: updates DOM directly
|
|
40
|
+
*
|
|
41
|
+
* @param config - Head configuration
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* head({
|
|
46
|
+
* title: 'My Page',
|
|
47
|
+
* meta: [
|
|
48
|
+
* { name: 'description', content: 'Page description' },
|
|
49
|
+
* { property: 'og:title', content: 'My Page' }
|
|
50
|
+
* ]
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function head(config: HeadConfig): void;
|
|
55
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"renderHead","stability":"stable","signature":"(state: HeadState) => string"} */
|
|
56
|
+
/**
|
|
57
|
+
* Render head state to HTML string
|
|
58
|
+
*
|
|
59
|
+
* Generates the content for the <head> element.
|
|
60
|
+
* Does not include <head> tags themselves.
|
|
61
|
+
*
|
|
62
|
+
* @param state - Head state to render
|
|
63
|
+
* @returns HTML string for head content
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const headHtml = renderHead(context.head);
|
|
68
|
+
* // <title>My Page</title>
|
|
69
|
+
* // <meta name="description" content="...">
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function renderHead(state: HeadState): string;
|
|
73
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"renderHtmlAttrs","stability":"stable","signature":"(state: HeadState) => string"} */
|
|
74
|
+
/**
|
|
75
|
+
* Render HTML element attributes from head state
|
|
76
|
+
*
|
|
77
|
+
* @param state - Head state
|
|
78
|
+
* @returns Attribute string for <html> element
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const attrs = renderHtmlAttrs(state);
|
|
83
|
+
* // 'lang="en" dir="ltr"'
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function renderHtmlAttrs(state: HeadState): string;
|
|
87
|
+
//# sourceMappingURL=head.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"head.d.ts","sourceRoot":"","sources":["../../src/shared/head.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EACT,OAAO,EAIR,MAAM,YAAY,CAAC;AA+BpB,+JAA+J;AAC/J;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,OAAO,YAAY,EAAE,UAAU,GAAG,SAAS,GACxD,IAAI,CAEN;AAMD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAMhD;AAwMD,iIAAiI;AACjI;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAU7C;AA6FD,uIAAuI;AACvI;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CA6BnD;AAED,4IAA4I;AAC5I;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAIxD"}
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.shared.head",
|
|
4
|
+
"title": "Head API for SEO",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
import { escapeHtml } from '@pithyjs/shared';
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// Context Access (lazy binding for environment separation)
|
|
11
|
+
// =============================================================================
|
|
12
|
+
// The context getter is set lazily by the server module.
|
|
13
|
+
// This pattern keeps server code (AsyncLocalStorage) out of the client bundle.
|
|
14
|
+
// If head.ts directly imported from server/context.ts, bundlers would include
|
|
15
|
+
// Node.js-specific code in browser builds, causing runtime errors.
|
|
16
|
+
let _getSSRContext;
|
|
17
|
+
/**
|
|
18
|
+
* Get SSR context if available.
|
|
19
|
+
*
|
|
20
|
+
* Returns undefined on client (no context getter set) or when called
|
|
21
|
+
* outside of runWithSSRContext() on server.
|
|
22
|
+
*/
|
|
23
|
+
function getContext() {
|
|
24
|
+
if (!_getSSRContext) {
|
|
25
|
+
// On client: context getter is never set, so head() updates DOM directly
|
|
26
|
+
// On server: context getter is set when '@pithyjs/ssr/server' is imported
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return _getSSRContext();
|
|
30
|
+
}
|
|
31
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"setContextGetter","stability":"stable","signature":"(getter: () => SSRContext | undefined) => void"} */
|
|
32
|
+
/**
|
|
33
|
+
* Wire up SSR context access for the head() function.
|
|
34
|
+
*
|
|
35
|
+
* Called automatically when '@pithyjs/ssr/server' is imported.
|
|
36
|
+
* This bridges the shared head module to the server's AsyncLocalStorage-based
|
|
37
|
+
* context without creating a direct import dependency.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export function setContextGetter(getter) {
|
|
42
|
+
_getSSRContext = getter;
|
|
43
|
+
}
|
|
44
|
+
// =============================================================================
|
|
45
|
+
// Meta Tag Deduplication
|
|
46
|
+
// =============================================================================
|
|
47
|
+
/**
|
|
48
|
+
* Get unique key for a meta tag (for deduplication)
|
|
49
|
+
*
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
export function getMetaKey(meta) {
|
|
53
|
+
if (meta.charset)
|
|
54
|
+
return 'charset';
|
|
55
|
+
if (meta.httpEquiv)
|
|
56
|
+
return `http-equiv:${meta.httpEquiv}`;
|
|
57
|
+
if (meta.property)
|
|
58
|
+
return `property:${meta.property}`;
|
|
59
|
+
if (meta.name)
|
|
60
|
+
return `name:${meta.name}`;
|
|
61
|
+
return `content:${meta.content}`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get unique key for a link tag (for deduplication)
|
|
65
|
+
*/
|
|
66
|
+
function getLinkKey(link) {
|
|
67
|
+
return `${link.rel}:${link.href}`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Merge meta tags, deduplicating by key (later values override)
|
|
71
|
+
*/
|
|
72
|
+
function mergeMeta(existing, incoming) {
|
|
73
|
+
const map = new Map();
|
|
74
|
+
// Add existing
|
|
75
|
+
for (const meta of existing) {
|
|
76
|
+
map.set(getMetaKey(meta), meta);
|
|
77
|
+
}
|
|
78
|
+
// Add/override with incoming
|
|
79
|
+
for (const meta of incoming) {
|
|
80
|
+
map.set(getMetaKey(meta), meta);
|
|
81
|
+
}
|
|
82
|
+
return Array.from(map.values());
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Merge link tags, deduplicating by rel+href
|
|
86
|
+
*/
|
|
87
|
+
function mergeLinks(existing, incoming) {
|
|
88
|
+
const map = new Map();
|
|
89
|
+
for (const link of existing) {
|
|
90
|
+
map.set(getLinkKey(link), link);
|
|
91
|
+
}
|
|
92
|
+
for (const link of incoming) {
|
|
93
|
+
map.set(getLinkKey(link), link);
|
|
94
|
+
}
|
|
95
|
+
return Array.from(map.values());
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Merge script tags (no deduplication - order matters)
|
|
99
|
+
*/
|
|
100
|
+
function mergeScripts(existing, incoming) {
|
|
101
|
+
// For scripts, we append rather than deduplicate
|
|
102
|
+
// as the same script might be intentionally included multiple times
|
|
103
|
+
return [...existing, ...incoming];
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Merge attributes (later values override)
|
|
107
|
+
*/
|
|
108
|
+
function mergeAttrs(existing, incoming) {
|
|
109
|
+
return { ...existing, ...incoming };
|
|
110
|
+
}
|
|
111
|
+
// =============================================================================
|
|
112
|
+
// Head State Mutation
|
|
113
|
+
// =============================================================================
|
|
114
|
+
/**
|
|
115
|
+
* Merge head config into head state
|
|
116
|
+
*/
|
|
117
|
+
function mergeHeadConfig(state, config) {
|
|
118
|
+
if (config.title !== undefined) {
|
|
119
|
+
state.title = config.title;
|
|
120
|
+
}
|
|
121
|
+
if (config.meta) {
|
|
122
|
+
state.meta = mergeMeta(state.meta, config.meta);
|
|
123
|
+
}
|
|
124
|
+
if (config.link) {
|
|
125
|
+
state.link = mergeLinks(state.link, config.link);
|
|
126
|
+
}
|
|
127
|
+
if (config.script) {
|
|
128
|
+
state.script = mergeScripts(state.script, config.script);
|
|
129
|
+
}
|
|
130
|
+
if (config.base) {
|
|
131
|
+
state.base = config.base;
|
|
132
|
+
}
|
|
133
|
+
if (config.htmlAttrs) {
|
|
134
|
+
state.htmlAttrs = mergeAttrs(state.htmlAttrs, config.htmlAttrs);
|
|
135
|
+
}
|
|
136
|
+
if (config.bodyAttrs) {
|
|
137
|
+
state.bodyAttrs = mergeAttrs(state.bodyAttrs, config.bodyAttrs);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// =============================================================================
|
|
141
|
+
// Client-Side DOM Manipulation
|
|
142
|
+
// =============================================================================
|
|
143
|
+
/**
|
|
144
|
+
* Check if running in browser environment
|
|
145
|
+
*/
|
|
146
|
+
function isBrowser() {
|
|
147
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Update document title on client
|
|
151
|
+
*/
|
|
152
|
+
function updateClientTitle(title) {
|
|
153
|
+
if (isBrowser()) {
|
|
154
|
+
document.title = title;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Add or update meta tag on client
|
|
159
|
+
*/
|
|
160
|
+
function updateClientMeta(meta) {
|
|
161
|
+
if (!isBrowser())
|
|
162
|
+
return;
|
|
163
|
+
// Find existing meta tag
|
|
164
|
+
let selector;
|
|
165
|
+
if (meta.charset) {
|
|
166
|
+
selector = 'meta[charset]';
|
|
167
|
+
}
|
|
168
|
+
else if (meta.httpEquiv) {
|
|
169
|
+
selector = `meta[http-equiv="${escapeHtml(meta.httpEquiv)}"]`;
|
|
170
|
+
}
|
|
171
|
+
else if (meta.property) {
|
|
172
|
+
selector = `meta[property="${escapeHtml(meta.property)}"]`;
|
|
173
|
+
}
|
|
174
|
+
else if (meta.name) {
|
|
175
|
+
selector = `meta[name="${escapeHtml(meta.name)}"]`;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// Can't update without identifier
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
let element = document.querySelector(selector);
|
|
182
|
+
if (!element) {
|
|
183
|
+
element = document.createElement('meta');
|
|
184
|
+
document.head.appendChild(element);
|
|
185
|
+
}
|
|
186
|
+
// Set attributes
|
|
187
|
+
if (meta.charset) {
|
|
188
|
+
element.setAttribute('charset', meta.charset);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
if (meta.httpEquiv)
|
|
192
|
+
element.httpEquiv = meta.httpEquiv;
|
|
193
|
+
if (meta.property)
|
|
194
|
+
element.setAttribute('property', meta.property);
|
|
195
|
+
if (meta.name)
|
|
196
|
+
element.name = meta.name;
|
|
197
|
+
element.content = meta.content;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Apply head config to client DOM
|
|
202
|
+
*/
|
|
203
|
+
function applyToClient(config) {
|
|
204
|
+
if (!isBrowser())
|
|
205
|
+
return;
|
|
206
|
+
if (config.title !== undefined) {
|
|
207
|
+
updateClientTitle(config.title);
|
|
208
|
+
}
|
|
209
|
+
if (config.meta) {
|
|
210
|
+
for (const meta of config.meta) {
|
|
211
|
+
updateClientMeta(meta);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Note: Link and script manipulation on client is more complex
|
|
215
|
+
// and typically not needed (handled by bundler/framework)
|
|
216
|
+
// We focus on title and meta for client-side updates
|
|
217
|
+
if (config.htmlAttrs) {
|
|
218
|
+
for (const [key, value] of Object.entries(config.htmlAttrs)) {
|
|
219
|
+
document.documentElement.setAttribute(key, value);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (config.bodyAttrs) {
|
|
223
|
+
for (const [key, value] of Object.entries(config.bodyAttrs)) {
|
|
224
|
+
document.body.setAttribute(key, value);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// =============================================================================
|
|
229
|
+
// Public API
|
|
230
|
+
// =============================================================================
|
|
231
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"head","stability":"stable","signature":"(config: HeadConfig) => void"} */
|
|
232
|
+
/**
|
|
233
|
+
* Declare head tags for current route/component
|
|
234
|
+
*
|
|
235
|
+
* Can be called multiple times - values are merged.
|
|
236
|
+
* On server: collects into SSR context
|
|
237
|
+
* On client: updates DOM directly
|
|
238
|
+
*
|
|
239
|
+
* @param config - Head configuration
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* head({
|
|
244
|
+
* title: 'My Page',
|
|
245
|
+
* meta: [
|
|
246
|
+
* { name: 'description', content: 'Page description' },
|
|
247
|
+
* { property: 'og:title', content: 'My Page' }
|
|
248
|
+
* ]
|
|
249
|
+
* });
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export function head(config) {
|
|
253
|
+
const ctx = getContext();
|
|
254
|
+
if (ctx) {
|
|
255
|
+
// Server-side: collect in context
|
|
256
|
+
mergeHeadConfig(ctx.head, config);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// Client-side: update DOM directly
|
|
260
|
+
applyToClient(config);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// =============================================================================
|
|
264
|
+
// HTML Rendering
|
|
265
|
+
// =============================================================================
|
|
266
|
+
/**
|
|
267
|
+
* Render a single meta tag to HTML
|
|
268
|
+
*/
|
|
269
|
+
function renderMetaTag(meta) {
|
|
270
|
+
const attrs = [];
|
|
271
|
+
if (meta.charset) {
|
|
272
|
+
attrs.push(`charset="${escapeHtml(meta.charset)}"`);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
if (meta.httpEquiv)
|
|
276
|
+
attrs.push(`http-equiv="${escapeHtml(meta.httpEquiv)}"`);
|
|
277
|
+
if (meta.property)
|
|
278
|
+
attrs.push(`property="${escapeHtml(meta.property)}"`);
|
|
279
|
+
if (meta.name)
|
|
280
|
+
attrs.push(`name="${escapeHtml(meta.name)}"`);
|
|
281
|
+
attrs.push(`content="${escapeHtml(meta.content)}"`);
|
|
282
|
+
}
|
|
283
|
+
return `<meta ${attrs.join(' ')}>`;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Render a single link tag to HTML
|
|
287
|
+
*/
|
|
288
|
+
function renderLinkTag(link) {
|
|
289
|
+
const attrs = [];
|
|
290
|
+
attrs.push(`rel="${escapeHtml(link.rel)}"`);
|
|
291
|
+
attrs.push(`href="${escapeHtml(link.href)}"`);
|
|
292
|
+
if (link.type)
|
|
293
|
+
attrs.push(`type="${escapeHtml(link.type)}"`);
|
|
294
|
+
if (link.crossorigin)
|
|
295
|
+
attrs.push(`crossorigin="${escapeHtml(link.crossorigin)}"`);
|
|
296
|
+
if (link.as)
|
|
297
|
+
attrs.push(`as="${escapeHtml(link.as)}"`);
|
|
298
|
+
if (link.media)
|
|
299
|
+
attrs.push(`media="${escapeHtml(link.media)}"`);
|
|
300
|
+
if (link.sizes)
|
|
301
|
+
attrs.push(`sizes="${escapeHtml(link.sizes)}"`);
|
|
302
|
+
if (link.fetchpriority)
|
|
303
|
+
attrs.push(`fetchpriority="${escapeHtml(link.fetchpriority)}"`);
|
|
304
|
+
if (link.referrerpolicy)
|
|
305
|
+
attrs.push(`referrerpolicy="${escapeHtml(link.referrerpolicy)}"`);
|
|
306
|
+
return `<link ${attrs.join(' ')}>`;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Render a single script tag to HTML
|
|
310
|
+
*/
|
|
311
|
+
function renderScriptTag(script) {
|
|
312
|
+
const attrs = [];
|
|
313
|
+
if (script.src)
|
|
314
|
+
attrs.push(`src="${escapeHtml(script.src)}"`);
|
|
315
|
+
if (script.type)
|
|
316
|
+
attrs.push(`type="${escapeHtml(script.type)}"`);
|
|
317
|
+
if (script.async)
|
|
318
|
+
attrs.push('async');
|
|
319
|
+
if (script.defer)
|
|
320
|
+
attrs.push('defer');
|
|
321
|
+
if (script.crossorigin)
|
|
322
|
+
attrs.push(`crossorigin="${escapeHtml(script.crossorigin)}"`);
|
|
323
|
+
if (script.fetchpriority)
|
|
324
|
+
attrs.push(`fetchpriority="${escapeHtml(script.fetchpriority)}"`);
|
|
325
|
+
if (script.referrerpolicy)
|
|
326
|
+
attrs.push(`referrerpolicy="${escapeHtml(script.referrerpolicy)}"`);
|
|
327
|
+
if (script.integrity)
|
|
328
|
+
attrs.push(`integrity="${escapeHtml(script.integrity)}"`);
|
|
329
|
+
if (script.nonce)
|
|
330
|
+
attrs.push(`nonce="${escapeHtml(script.nonce)}"`);
|
|
331
|
+
if (script.content) {
|
|
332
|
+
// Inline script - content should NOT be escaped (it's JS code)
|
|
333
|
+
// But we must prevent </script from appearing, as it terminates the tag.
|
|
334
|
+
// HTML allows whitespace before >, so </script > would also close the tag.
|
|
335
|
+
// Escaping </script to <\/script is safe: JS interprets \/ as /, but
|
|
336
|
+
// HTML doesn't see it as a closing tag.
|
|
337
|
+
const safeContent = script.content.replace(/<\/script/gi, '<\\/script');
|
|
338
|
+
return `<script${attrs.length ? ' ' + attrs.join(' ') : ''}>${safeContent}</script>`;
|
|
339
|
+
}
|
|
340
|
+
return `<script ${attrs.join(' ')}></script>`;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Render base tag to HTML
|
|
344
|
+
*/
|
|
345
|
+
function renderBaseTag(base) {
|
|
346
|
+
const attrs = [];
|
|
347
|
+
if (base.href)
|
|
348
|
+
attrs.push(`href="${escapeHtml(base.href)}"`);
|
|
349
|
+
if (base.target)
|
|
350
|
+
attrs.push(`target="${escapeHtml(base.target)}"`);
|
|
351
|
+
return `<base ${attrs.join(' ')}>`;
|
|
352
|
+
}
|
|
353
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"renderHead","stability":"stable","signature":"(state: HeadState) => string"} */
|
|
354
|
+
/**
|
|
355
|
+
* Render head state to HTML string
|
|
356
|
+
*
|
|
357
|
+
* Generates the content for the <head> element.
|
|
358
|
+
* Does not include <head> tags themselves.
|
|
359
|
+
*
|
|
360
|
+
* @param state - Head state to render
|
|
361
|
+
* @returns HTML string for head content
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* const headHtml = renderHead(context.head);
|
|
366
|
+
* // <title>My Page</title>
|
|
367
|
+
* // <meta name="description" content="...">
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
export function renderHead(state) {
|
|
371
|
+
const parts = [];
|
|
372
|
+
// Base tag first (if present)
|
|
373
|
+
if (state.base) {
|
|
374
|
+
parts.push(renderBaseTag(state.base));
|
|
375
|
+
}
|
|
376
|
+
// Title
|
|
377
|
+
if (state.title) {
|
|
378
|
+
parts.push(`<title>${escapeHtml(state.title)}</title>`);
|
|
379
|
+
}
|
|
380
|
+
// Meta tags
|
|
381
|
+
for (const meta of state.meta) {
|
|
382
|
+
parts.push(renderMetaTag(meta));
|
|
383
|
+
}
|
|
384
|
+
// Link tags
|
|
385
|
+
for (const link of state.link) {
|
|
386
|
+
parts.push(renderLinkTag(link));
|
|
387
|
+
}
|
|
388
|
+
// Script tags (in head)
|
|
389
|
+
for (const script of state.script) {
|
|
390
|
+
parts.push(renderScriptTag(script));
|
|
391
|
+
}
|
|
392
|
+
return parts.join('\n');
|
|
393
|
+
}
|
|
394
|
+
/** @codexApi {"parent":"pithy.ssr.shared.head","name":"renderHtmlAttrs","stability":"stable","signature":"(state: HeadState) => string"} */
|
|
395
|
+
/**
|
|
396
|
+
* Render HTML element attributes from head state
|
|
397
|
+
*
|
|
398
|
+
* @param state - Head state
|
|
399
|
+
* @returns Attribute string for <html> element
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```ts
|
|
403
|
+
* const attrs = renderHtmlAttrs(state);
|
|
404
|
+
* // 'lang="en" dir="ltr"'
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
export function renderHtmlAttrs(state) {
|
|
408
|
+
return Object.entries(state.htmlAttrs)
|
|
409
|
+
.map(([key, value]) => `${escapeHtml(key)}="${escapeHtml(value)}"`)
|
|
410
|
+
.join(' ');
|
|
411
|
+
}
|
|
412
|
+
//# sourceMappingURL=head.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"head.js","sourceRoot":"","sources":["../../src/shared/head.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAkBF,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,gFAAgF;AAChF,2DAA2D;AAC3D,gFAAgF;AAEhF,yDAAyD;AACzD,+EAA+E;AAC/E,8EAA8E;AAC9E,mEAAmE;AAEnE,IAAI,cAES,CAAC;AAEd;;;;;GAKG;AACH,SAAS,UAAU;IACjB,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,yEAAyE;QACzE,0EAA0E;QAC1E,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,cAAc,EAAE,CAAC;AAC1B,CAAC;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AAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC;SAClE,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.ssr.shared.head\",\n \"title\": \"Head API for SEO\",\n \"category\": \"feature\"\n}\n*/\n\n/**\n * @pithyjs/ssr - Head API\n *\n * Functions for managing document head (title, meta, links) during SSR and on client.\n * On server: collects head state in SSR context\n * On client: manipulates DOM directly\n */\n\nimport type {\n HeadConfig,\n HeadState,\n MetaTag,\n LinkTag,\n ScriptTag,\n BaseTag,\n} from './types.js';\nimport { escapeHtml } from '@pithyjs/shared';\n\n// =============================================================================\n// Context Access (lazy binding for environment separation)\n// =============================================================================\n\n// The context getter is set lazily by the server module.\n// This pattern keeps server code (AsyncLocalStorage) out of the client bundle.\n// If head.ts directly imported from server/context.ts, bundlers would include\n// Node.js-specific code in browser builds, causing runtime errors.\n\nlet _getSSRContext:\n | (() => import('./types.js').SSRContext | undefined)\n | undefined;\n\n/**\n * Get SSR context if available.\n *\n * Returns undefined on client (no context getter set) or when called\n * outside of runWithSSRContext() on server.\n */\nfunction getContext(): import('./types.js').SSRContext | undefined {\n if (!_getSSRContext) {\n // On client: context getter is never set, so head() updates DOM directly\n // On server: context getter is set when '@pithyjs/ssr/server' is imported\n return undefined;\n }\n return _getSSRContext();\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.shared.head\",\"name\":\"setContextGetter\",\"stability\":\"stable\",\"signature\":\"(getter: () => SSRContext | undefined) => void\"} */\n/**\n * Wire up SSR context access for the head() function.\n *\n * Called automatically when '@pithyjs/ssr/server' is imported.\n * This bridges the shared head module to the server's AsyncLocalStorage-based\n * context without creating a direct import dependency.\n *\n * @internal\n */\nexport function setContextGetter(\n getter: () => import('./types.js').SSRContext | undefined\n): void {\n _getSSRContext = getter;\n}\n\n// =============================================================================\n// Meta Tag Deduplication\n// =============================================================================\n\n/**\n * Get unique key for a meta tag (for deduplication)\n *\n * @internal\n */\nexport function getMetaKey(meta: MetaTag): string {\n if (meta.charset) return 'charset';\n if (meta.httpEquiv) return `http-equiv:${meta.httpEquiv}`;\n if (meta.property) return `property:${meta.property}`;\n if (meta.name) return `name:${meta.name}`;\n return `content:${meta.content}`;\n}\n\n/**\n * Get unique key for a link tag (for deduplication)\n */\nfunction getLinkKey(link: LinkTag): string {\n return `${link.rel}:${link.href}`;\n}\n\n/**\n * Merge meta tags, deduplicating by key (later values override)\n */\nfunction mergeMeta(existing: MetaTag[], incoming: MetaTag[]): MetaTag[] {\n const map = new Map<string, MetaTag>();\n\n // Add existing\n for (const meta of existing) {\n map.set(getMetaKey(meta), meta);\n }\n\n // Add/override with incoming\n for (const meta of incoming) {\n map.set(getMetaKey(meta), meta);\n }\n\n return Array.from(map.values());\n}\n\n/**\n * Merge link tags, deduplicating by rel+href\n */\nfunction mergeLinks(existing: LinkTag[], incoming: LinkTag[]): LinkTag[] {\n const map = new Map<string, LinkTag>();\n\n for (const link of existing) {\n map.set(getLinkKey(link), link);\n }\n\n for (const link of incoming) {\n map.set(getLinkKey(link), link);\n }\n\n return Array.from(map.values());\n}\n\n/**\n * Merge script tags (no deduplication - order matters)\n */\nfunction mergeScripts(\n existing: ScriptTag[],\n incoming: ScriptTag[]\n): ScriptTag[] {\n // For scripts, we append rather than deduplicate\n // as the same script might be intentionally included multiple times\n return [...existing, ...incoming];\n}\n\n/**\n * Merge attributes (later values override)\n */\nfunction mergeAttrs(\n existing: Record<string, string>,\n incoming: Record<string, string>\n): Record<string, string> {\n return { ...existing, ...incoming };\n}\n\n// =============================================================================\n// Head State Mutation\n// =============================================================================\n\n/**\n * Merge head config into head state\n */\nfunction mergeHeadConfig(state: HeadState, config: HeadConfig): void {\n if (config.title !== undefined) {\n state.title = config.title;\n }\n\n if (config.meta) {\n state.meta = mergeMeta(state.meta, config.meta);\n }\n\n if (config.link) {\n state.link = mergeLinks(state.link, config.link);\n }\n\n if (config.script) {\n state.script = mergeScripts(state.script, config.script);\n }\n\n if (config.base) {\n state.base = config.base;\n }\n\n if (config.htmlAttrs) {\n state.htmlAttrs = mergeAttrs(state.htmlAttrs, config.htmlAttrs);\n }\n\n if (config.bodyAttrs) {\n state.bodyAttrs = mergeAttrs(state.bodyAttrs, config.bodyAttrs);\n }\n}\n\n// =============================================================================\n// Client-Side DOM Manipulation\n// =============================================================================\n\n/**\n * Check if running in browser environment\n */\nfunction isBrowser(): boolean {\n return typeof window !== 'undefined' && typeof document !== 'undefined';\n}\n\n/**\n * Update document title on client\n */\nfunction updateClientTitle(title: string): void {\n if (isBrowser()) {\n document.title = title;\n }\n}\n\n/**\n * Add or update meta tag on client\n */\nfunction updateClientMeta(meta: MetaTag): void {\n if (!isBrowser()) return;\n\n // Find existing meta tag\n let selector: string;\n if (meta.charset) {\n selector = 'meta[charset]';\n } else if (meta.httpEquiv) {\n selector = `meta[http-equiv=\"${escapeHtml(meta.httpEquiv)}\"]`;\n } else if (meta.property) {\n selector = `meta[property=\"${escapeHtml(meta.property)}\"]`;\n } else if (meta.name) {\n selector = `meta[name=\"${escapeHtml(meta.name)}\"]`;\n } else {\n // Can't update without identifier\n return;\n }\n\n let element = document.querySelector(selector) as HTMLMetaElement | null;\n\n if (!element) {\n element = document.createElement('meta');\n document.head.appendChild(element);\n }\n\n // Set attributes\n if (meta.charset) {\n element.setAttribute('charset', meta.charset);\n } else {\n if (meta.httpEquiv) element.httpEquiv = meta.httpEquiv;\n if (meta.property) element.setAttribute('property', meta.property);\n if (meta.name) element.name = meta.name;\n element.content = meta.content;\n }\n}\n\n/**\n * Apply head config to client DOM\n */\nfunction applyToClient(config: HeadConfig): void {\n if (!isBrowser()) return;\n\n if (config.title !== undefined) {\n updateClientTitle(config.title);\n }\n\n if (config.meta) {\n for (const meta of config.meta) {\n updateClientMeta(meta);\n }\n }\n\n // Note: Link and script manipulation on client is more complex\n // and typically not needed (handled by bundler/framework)\n // We focus on title and meta for client-side updates\n\n if (config.htmlAttrs) {\n for (const [key, value] of Object.entries(config.htmlAttrs)) {\n document.documentElement.setAttribute(key, value);\n }\n }\n\n if (config.bodyAttrs) {\n for (const [key, value] of Object.entries(config.bodyAttrs)) {\n document.body.setAttribute(key, value);\n }\n }\n}\n\n// =============================================================================\n// Public API\n// =============================================================================\n\n/** @codexApi {\"parent\":\"pithy.ssr.shared.head\",\"name\":\"head\",\"stability\":\"stable\",\"signature\":\"(config: HeadConfig) => void\"} */\n/**\n * Declare head tags for current route/component\n *\n * Can be called multiple times - values are merged.\n * On server: collects into SSR context\n * On client: updates DOM directly\n *\n * @param config - Head configuration\n *\n * @example\n * ```ts\n * head({\n * title: 'My Page',\n * meta: [\n * { name: 'description', content: 'Page description' },\n * { property: 'og:title', content: 'My Page' }\n * ]\n * });\n * ```\n */\nexport function head(config: HeadConfig): void {\n const ctx = getContext();\n\n if (ctx) {\n // Server-side: collect in context\n mergeHeadConfig(ctx.head, config);\n } else {\n // Client-side: update DOM directly\n applyToClient(config);\n }\n}\n\n// =============================================================================\n// HTML Rendering\n// =============================================================================\n\n/**\n * Render a single meta tag to HTML\n */\nfunction renderMetaTag(meta: MetaTag): string {\n const attrs: string[] = [];\n\n if (meta.charset) {\n attrs.push(`charset=\"${escapeHtml(meta.charset)}\"`);\n } else {\n if (meta.httpEquiv)\n attrs.push(`http-equiv=\"${escapeHtml(meta.httpEquiv)}\"`);\n if (meta.property) attrs.push(`property=\"${escapeHtml(meta.property)}\"`);\n if (meta.name) attrs.push(`name=\"${escapeHtml(meta.name)}\"`);\n attrs.push(`content=\"${escapeHtml(meta.content)}\"`);\n }\n\n return `<meta ${attrs.join(' ')}>`;\n}\n\n/**\n * Render a single link tag to HTML\n */\nfunction renderLinkTag(link: LinkTag): string {\n const attrs: string[] = [];\n\n attrs.push(`rel=\"${escapeHtml(link.rel)}\"`);\n attrs.push(`href=\"${escapeHtml(link.href)}\"`);\n\n if (link.type) attrs.push(`type=\"${escapeHtml(link.type)}\"`);\n if (link.crossorigin)\n attrs.push(`crossorigin=\"${escapeHtml(link.crossorigin)}\"`);\n if (link.as) attrs.push(`as=\"${escapeHtml(link.as)}\"`);\n if (link.media) attrs.push(`media=\"${escapeHtml(link.media)}\"`);\n if (link.sizes) attrs.push(`sizes=\"${escapeHtml(link.sizes)}\"`);\n if (link.fetchpriority)\n attrs.push(`fetchpriority=\"${escapeHtml(link.fetchpriority)}\"`);\n if (link.referrerpolicy)\n attrs.push(`referrerpolicy=\"${escapeHtml(link.referrerpolicy)}\"`);\n\n return `<link ${attrs.join(' ')}>`;\n}\n\n/**\n * Render a single script tag to HTML\n */\nfunction renderScriptTag(script: ScriptTag): string {\n const attrs: string[] = [];\n\n if (script.src) attrs.push(`src=\"${escapeHtml(script.src)}\"`);\n if (script.type) attrs.push(`type=\"${escapeHtml(script.type)}\"`);\n if (script.async) attrs.push('async');\n if (script.defer) attrs.push('defer');\n if (script.crossorigin)\n attrs.push(`crossorigin=\"${escapeHtml(script.crossorigin)}\"`);\n if (script.fetchpriority)\n attrs.push(`fetchpriority=\"${escapeHtml(script.fetchpriority)}\"`);\n if (script.referrerpolicy)\n attrs.push(`referrerpolicy=\"${escapeHtml(script.referrerpolicy)}\"`);\n if (script.integrity)\n attrs.push(`integrity=\"${escapeHtml(script.integrity)}\"`);\n if (script.nonce) attrs.push(`nonce=\"${escapeHtml(script.nonce)}\"`);\n\n if (script.content) {\n // Inline script - content should NOT be escaped (it's JS code)\n // But we must prevent </script from appearing, as it terminates the tag.\n // HTML allows whitespace before >, so </script > would also close the tag.\n // Escaping </script to <\\/script is safe: JS interprets \\/ as /, but\n // HTML doesn't see it as a closing tag.\n const safeContent = script.content.replace(/<\\/script/gi, '<\\\\/script');\n return `<script${attrs.length ? ' ' + attrs.join(' ') : ''}>${safeContent}</script>`;\n }\n\n return `<script ${attrs.join(' ')}></script>`;\n}\n\n/**\n * Render base tag to HTML\n */\nfunction renderBaseTag(base: BaseTag): string {\n const attrs: string[] = [];\n\n if (base.href) attrs.push(`href=\"${escapeHtml(base.href)}\"`);\n if (base.target) attrs.push(`target=\"${escapeHtml(base.target)}\"`);\n\n return `<base ${attrs.join(' ')}>`;\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.shared.head\",\"name\":\"renderHead\",\"stability\":\"stable\",\"signature\":\"(state: HeadState) => string\"} */\n/**\n * Render head state to HTML string\n *\n * Generates the content for the <head> element.\n * Does not include <head> tags themselves.\n *\n * @param state - Head state to render\n * @returns HTML string for head content\n *\n * @example\n * ```ts\n * const headHtml = renderHead(context.head);\n * // <title>My Page</title>\n * // <meta name=\"description\" content=\"...\">\n * ```\n */\nexport function renderHead(state: HeadState): string {\n const parts: string[] = [];\n\n // Base tag first (if present)\n if (state.base) {\n parts.push(renderBaseTag(state.base));\n }\n\n // Title\n if (state.title) {\n parts.push(`<title>${escapeHtml(state.title)}</title>`);\n }\n\n // Meta tags\n for (const meta of state.meta) {\n parts.push(renderMetaTag(meta));\n }\n\n // Link tags\n for (const link of state.link) {\n parts.push(renderLinkTag(link));\n }\n\n // Script tags (in head)\n for (const script of state.script) {\n parts.push(renderScriptTag(script));\n }\n\n return parts.join('\\n');\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.shared.head\",\"name\":\"renderHtmlAttrs\",\"stability\":\"stable\",\"signature\":\"(state: HeadState) => string\"} */\n/**\n * Render HTML element attributes from head state\n *\n * @param state - Head state\n * @returns Attribute string for <html> element\n *\n * @example\n * ```ts\n * const attrs = renderHtmlAttrs(state);\n * // 'lang=\"en\" dir=\"ltr\"'\n * ```\n */\nexport function renderHtmlAttrs(state: HeadState): string {\n return Object.entries(state.htmlAttrs)\n .map(([key, value]) => `${escapeHtml(key)}=\"${escapeHtml(value)}\"`)\n .join(' ');\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared helpers for SSR shell assembly.
|
|
3
|
+
*
|
|
4
|
+
* Used by both the Vite SSR dev plugin (via ssrLoadModule) and the SSG
|
|
5
|
+
* build pipeline (direct import). Keeping a single source of truth avoids
|
|
6
|
+
* behavioral drift between dev and production rendering.
|
|
7
|
+
*/
|
|
8
|
+
import type { HeadState } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Convert a URL path to a file path for the output directory.
|
|
11
|
+
*
|
|
12
|
+
* Segment text passes through VERBATIM, by design — no slugifying, no
|
|
13
|
+
* escaping. Naming an entry and rewriting the links that point at it are one
|
|
14
|
+
* policy: `pithy-ssg` pairs a raw name with raw links (`makeLinksRelative`
|
|
15
|
+
* only adds a `../` prefix; `generateStaticSite` does not rebase at all), so
|
|
16
|
+
* renaming here alone emits pages whose links resolve to nothing. Exception:
|
|
17
|
+
* `makeLinksRelative` skips a final `/\.\w+$/` segment as an asset, so `/v1.0`
|
|
18
|
+
* lands here with links pointing at `v1.0` — Bug 10 / BACKLOG #61. Escaping was
|
|
19
|
+
* added (`f1b6ea5b`) and reverted 13 minutes later (`a5d77f40`) for exactly
|
|
20
|
+
* that. Pinned by `build/tests/ssg-helpers.test.ts`; rationale in
|
|
21
|
+
* `docs/ssg/static-export-unification.md`.
|
|
22
|
+
*
|
|
23
|
+
* Examples:
|
|
24
|
+
* - '/' → 'index.html'
|
|
25
|
+
* - '/about' → 'about/index.html'
|
|
26
|
+
* - '/blog/post-1' → 'blog/post-1/index.html'
|
|
27
|
+
*
|
|
28
|
+
* @param urlPath - URL path (e.g., '/about')
|
|
29
|
+
* @returns Relative file path (e.g., 'about/index.html')
|
|
30
|
+
*/
|
|
31
|
+
export declare function pathToFilePath(urlPath: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Merge shell head with page head. Page values take priority:
|
|
34
|
+
* - title: page wins if non-empty
|
|
35
|
+
* - meta: page replaces a shell tag of the same identity, otherwise appended
|
|
36
|
+
* - link/script arrays: concatenated (page after shell)
|
|
37
|
+
* - htmlAttrs/bodyAttrs: merged (page wins on conflict)
|
|
38
|
+
*
|
|
39
|
+
* Meta is keyed the way `head()` already keys its own merge, so a description
|
|
40
|
+
* replaced across shell and page and one replaced across two `head()` calls
|
|
41
|
+
* agree. Concatenating instead shipped two `<meta name="description">`, which
|
|
42
|
+
* is undefined for crawlers.
|
|
43
|
+
*/
|
|
44
|
+
export declare function mergeHead(shell: Partial<HeadState>, page: Partial<HeadState>): HeadState;
|
|
45
|
+
/**
|
|
46
|
+
* Find the router outlet element in the shell HTML.
|
|
47
|
+
*
|
|
48
|
+
* Detection order:
|
|
49
|
+
* 1. `data-router-outlet` attribute (auto-generated by @routerOutlet directive)
|
|
50
|
+
* 2. `id="{outletId}"` attribute (explicit fallback via config.outletId)
|
|
51
|
+
*
|
|
52
|
+
* This means users only need `@routerOutlet` on their outlet element —
|
|
53
|
+
* no extra `id` attribute required.
|
|
54
|
+
*/
|
|
55
|
+
export declare function findOutletMatch(shellHtml: string, outletId: string): RegExpMatchArray | null;
|
|
56
|
+
/**
|
|
57
|
+
* Inject page HTML into the App shell's outlet element.
|
|
58
|
+
*
|
|
59
|
+
* Auto-detects the outlet via `@routerOutlet` (data-router-outlet attribute),
|
|
60
|
+
* falling back to `id="{outletId}"` from config.
|
|
61
|
+
* Uses depth-counting to handle nested elements of the same tag correctly.
|
|
62
|
+
*
|
|
63
|
+
* @param shellHtml - Full shell HTML string
|
|
64
|
+
* @param pageHtml - Rendered page content to inject
|
|
65
|
+
* @param outletId - Outlet element ID (default: 'outlet')
|
|
66
|
+
* @param logPrefix - Prefix for warning messages (e.g. '[SSG]' or '[pithy-ssr]')
|
|
67
|
+
*/
|
|
68
|
+
export declare function injectIntoOutlet(shellHtml: string, pageHtml: string, outletId: string, logPrefix?: string): string;
|
|
69
|
+
//# sourceMappingURL=shell-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-helpers.d.ts","sourceRoot":"","sources":["../../src/shared/shell-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAW,MAAM,YAAY,CAAC;AAOrD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAgBtD;AAMD;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,EACzB,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,GACvB,SAAS,CAeX;AAMD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,gBAAgB,GAAG,IAAI,CAczB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,SAAS,SAAU,GAClB,MAAM,CAwDR"}
|