zero-query 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +2 -0
- package/cli/args.js +33 -33
- package/cli/commands/build-api.js +443 -442
- package/cli/commands/build.js +254 -247
- package/cli/commands/bundle.js +1228 -1224
- package/cli/commands/create.js +137 -121
- package/cli/commands/dev/devtools/index.js +56 -56
- package/cli/commands/dev/devtools/js/components.js +49 -49
- package/cli/commands/dev/devtools/js/core.js +423 -423
- package/cli/commands/dev/devtools/js/elements.js +421 -421
- package/cli/commands/dev/devtools/js/network.js +166 -166
- package/cli/commands/dev/devtools/js/performance.js +73 -73
- package/cli/commands/dev/devtools/js/router.js +105 -105
- package/cli/commands/dev/devtools/js/source.js +132 -132
- package/cli/commands/dev/devtools/js/stats.js +35 -35
- package/cli/commands/dev/devtools/js/tabs.js +79 -79
- package/cli/commands/dev/devtools/panel.html +95 -95
- package/cli/commands/dev/devtools/styles.css +244 -244
- package/cli/commands/dev/index.js +107 -107
- package/cli/commands/dev/logger.js +75 -75
- package/cli/commands/dev/overlay.js +858 -858
- package/cli/commands/dev/server.js +220 -220
- package/cli/commands/dev/validator.js +94 -94
- package/cli/commands/dev/watcher.js +172 -172
- package/cli/help.js +114 -112
- package/cli/index.js +52 -52
- package/cli/scaffold/default/LICENSE +21 -21
- package/cli/scaffold/default/app/app.js +207 -207
- package/cli/scaffold/default/app/components/about.js +201 -201
- package/cli/scaffold/default/app/components/api-demo.js +143 -143
- package/cli/scaffold/default/app/components/contact-card.js +231 -231
- package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
- package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
- package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
- package/cli/scaffold/default/app/components/counter.js +127 -127
- package/cli/scaffold/default/app/components/home.js +249 -249
- package/cli/scaffold/default/app/components/not-found.js +16 -16
- package/cli/scaffold/default/app/components/playground/playground.css +115 -115
- package/cli/scaffold/default/app/components/playground/playground.html +161 -161
- package/cli/scaffold/default/app/components/playground/playground.js +116 -116
- package/cli/scaffold/default/app/components/todos.js +225 -225
- package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
- package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
- package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
- package/cli/scaffold/default/app/routes.js +15 -15
- package/cli/scaffold/default/app/store.js +101 -101
- package/cli/scaffold/default/global.css +552 -552
- package/cli/scaffold/default/index.html +99 -99
- package/cli/scaffold/minimal/app/app.js +85 -85
- package/cli/scaffold/minimal/app/components/about.js +68 -68
- package/cli/scaffold/minimal/app/components/counter.js +122 -122
- package/cli/scaffold/minimal/app/components/home.js +68 -68
- package/cli/scaffold/minimal/app/components/not-found.js +16 -16
- package/cli/scaffold/minimal/app/routes.js +9 -9
- package/cli/scaffold/minimal/app/store.js +36 -36
- package/cli/scaffold/minimal/global.css +300 -300
- package/cli/scaffold/minimal/index.html +44 -44
- package/cli/scaffold/ssr/app/app.js +41 -41
- package/cli/scaffold/ssr/app/components/about.js +55 -55
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
- package/cli/scaffold/ssr/app/components/home.js +37 -37
- package/cli/scaffold/ssr/app/components/not-found.js +15 -15
- package/cli/scaffold/ssr/app/routes.js +8 -8
- package/cli/scaffold/ssr/global.css +228 -228
- package/cli/scaffold/ssr/index.html +37 -37
- package/cli/scaffold/ssr/package.json +8 -8
- package/cli/scaffold/ssr/server/data/posts.js +144 -144
- package/cli/scaffold/ssr/server/index.js +213 -213
- package/cli/scaffold/webrtc/app/app.js +11 -0
- package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
- package/cli/scaffold/webrtc/app/lib/room.js +252 -0
- package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
- package/cli/scaffold/webrtc/global.css +250 -0
- package/cli/scaffold/webrtc/index.html +21 -0
- package/cli/utils.js +305 -287
- package/dist/API.md +661 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +10313 -6614
- package/dist/zquery.min.js +8 -631
- package/index.d.ts +570 -371
- package/index.js +311 -240
- package/package.json +76 -70
- package/src/component.js +1709 -1691
- package/src/core.js +921 -921
- package/src/diff.js +497 -497
- package/src/errors.js +209 -209
- package/src/expression.js +922 -922
- package/src/http.js +242 -242
- package/src/package.json +1 -1
- package/src/reactive.js +255 -255
- package/src/router.js +843 -843
- package/src/ssr.js +418 -418
- package/src/store.js +318 -318
- package/src/utils.js +515 -515
- package/src/webrtc/e2ee.js +351 -0
- package/src/webrtc/errors.js +116 -0
- package/src/webrtc/ice.js +301 -0
- package/src/webrtc/index.js +131 -0
- package/src/webrtc/joinToken.js +119 -0
- package/src/webrtc/observe.js +172 -0
- package/src/webrtc/peer.js +351 -0
- package/src/webrtc/reactive.js +268 -0
- package/src/webrtc/room.js +625 -0
- package/src/webrtc/sdp.js +302 -0
- package/src/webrtc/sfu/index.js +43 -0
- package/src/webrtc/sfu/livekit.js +131 -0
- package/src/webrtc/sfu/mediasoup.js +150 -0
- package/src/webrtc/signaling.js +373 -0
- package/src/webrtc/turn.js +237 -0
- package/tests/_helpers/webrtcFakes.js +289 -0
- package/tests/audit.test.js +4158 -4158
- package/tests/cli.test.js +1136 -1103
- package/tests/compare.test.js +497 -486
- package/tests/component.test.js +3969 -3938
- package/tests/core.test.js +1910 -1910
- package/tests/dev-server.test.js +489 -489
- package/tests/diff.test.js +1416 -1416
- package/tests/docs.test.js +1664 -1650
- package/tests/electron-features.test.js +864 -864
- package/tests/errors.test.js +619 -619
- package/tests/expression.test.js +1056 -1056
- package/tests/http.test.js +648 -648
- package/tests/reactive.test.js +819 -819
- package/tests/router.test.js +2327 -2327
- package/tests/ssr.test.js +870 -870
- package/tests/store.test.js +830 -830
- package/tests/test-minifier.js +153 -153
- package/tests/test-ssr.js +27 -27
- package/tests/utils.test.js +1377 -1377
- package/tests/webrtc/e2ee.test.js +283 -0
- package/tests/webrtc/ice.test.js +202 -0
- package/tests/webrtc/joinToken.test.js +89 -0
- package/tests/webrtc/observe.test.js +111 -0
- package/tests/webrtc/peer.test.js +373 -0
- package/tests/webrtc/reactive.test.js +235 -0
- package/tests/webrtc/room.test.js +406 -0
- package/tests/webrtc/sdp.test.js +151 -0
- package/tests/webrtc/sfu-livekit.test.js +119 -0
- package/tests/webrtc/sfu.test.js +160 -0
- package/tests/webrtc/signaling.test.js +251 -0
- package/tests/webrtc/turn.test.js +256 -0
- package/types/collection.d.ts +383 -383
- package/types/component.d.ts +186 -186
- package/types/errors.d.ts +135 -135
- package/types/http.d.ts +92 -92
- package/types/misc.d.ts +201 -201
- package/types/reactive.d.ts +98 -98
- package/types/router.d.ts +190 -190
- package/types/ssr.d.ts +102 -102
- package/types/store.d.ts +146 -146
- package/types/utils.d.ts +245 -245
- package/types/webrtc.d.ts +653 -0
package/src/ssr.js
CHANGED
|
@@ -1,418 +1,418 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* zQuery SSR - Server-side rendering to HTML string
|
|
3
|
-
*
|
|
4
|
-
* Renders registered components to static HTML strings for SEO,
|
|
5
|
-
* initial page load performance, and static site generation.
|
|
6
|
-
*
|
|
7
|
-
* Works in Node.js - no DOM required for basic rendering.
|
|
8
|
-
* Supports hydration markers for client-side takeover.
|
|
9
|
-
*
|
|
10
|
-
* Usage (Node.js):
|
|
11
|
-
* import { renderToString, createSSRApp } from 'zero-query/ssr';
|
|
12
|
-
*
|
|
13
|
-
* const app = createSSRApp();
|
|
14
|
-
* app.component('my-page', {
|
|
15
|
-
* state: () => ({ title: 'Hello' }),
|
|
16
|
-
* render() { return `<h1>${this.state.title}</h1>`; }
|
|
17
|
-
* });
|
|
18
|
-
*
|
|
19
|
-
* const html = await app.renderToString('my-page', { title: 'World' });
|
|
20
|
-
* // → '<my-page data-zq-ssr><h1>World</h1></my-page>'
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { safeEval } from './expression.js';
|
|
24
|
-
import { reportError, ErrorCode, ZQueryError } from './errors.js';
|
|
25
|
-
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
// SSR Component renderer
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
class SSRComponent {
|
|
30
|
-
constructor(definition, props = {}) {
|
|
31
|
-
this._def = definition;
|
|
32
|
-
this.props = Object.freeze({ ...props });
|
|
33
|
-
this.refs = {};
|
|
34
|
-
this.templates = {};
|
|
35
|
-
this.computed = {};
|
|
36
|
-
|
|
37
|
-
// Initialize state
|
|
38
|
-
const initialState = typeof definition.state === 'function'
|
|
39
|
-
? definition.state()
|
|
40
|
-
: { ...(definition.state || {}) };
|
|
41
|
-
this.state = initialState;
|
|
42
|
-
|
|
43
|
-
// Add __raw to match client-side API
|
|
44
|
-
Object.defineProperty(this.state, '__raw', { value: this.state, enumerable: false });
|
|
45
|
-
|
|
46
|
-
// Computed properties
|
|
47
|
-
if (definition.computed) {
|
|
48
|
-
for (const [name, fn] of Object.entries(definition.computed)) {
|
|
49
|
-
Object.defineProperty(this.computed, name, {
|
|
50
|
-
get: () => {
|
|
51
|
-
try {
|
|
52
|
-
return fn.call(this, this.state);
|
|
53
|
-
} catch (err) {
|
|
54
|
-
reportError(ErrorCode.SSR_RENDER, `Computed property "${name}" threw during SSR`, { property: name }, err);
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
enumerable: true
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Bind user methods
|
|
64
|
-
for (const [key, val] of Object.entries(definition)) {
|
|
65
|
-
if (typeof val === 'function' && !SSR_RESERVED.has(key)) {
|
|
66
|
-
this[key] = val.bind(this);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Init lifecycle - guarded so a broken init doesn't crash the whole render
|
|
71
|
-
if (definition.init) {
|
|
72
|
-
try {
|
|
73
|
-
definition.init.call(this);
|
|
74
|
-
} catch (err) {
|
|
75
|
-
reportError(ErrorCode.SSR_RENDER, 'Component init() threw during SSR', {}, err);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
render() {
|
|
81
|
-
if (this._def.render) {
|
|
82
|
-
try {
|
|
83
|
-
let html = this._def.render.call(this);
|
|
84
|
-
html = this._interpolate(html);
|
|
85
|
-
return html;
|
|
86
|
-
} catch (err) {
|
|
87
|
-
reportError(ErrorCode.SSR_RENDER, 'Component render() threw during SSR', {}, err);
|
|
88
|
-
return `<!-- SSR render error -->`;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return '';
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Basic {{expression}} interpolation for SSR
|
|
95
|
-
_interpolate(html) {
|
|
96
|
-
return html.replace(/\{\{(.+?)\}\}/g, (_, expr) => {
|
|
97
|
-
try {
|
|
98
|
-
const result = safeEval(expr.trim(), [
|
|
99
|
-
this.state,
|
|
100
|
-
{ props: this.props, computed: this.computed }
|
|
101
|
-
]);
|
|
102
|
-
return result != null ? _escapeHtml(String(result)) : '';
|
|
103
|
-
} catch (err) {
|
|
104
|
-
reportError(ErrorCode.SSR_RENDER, `Expression "{{${expr.trim()}}}" failed during SSR`, { expression: expr.trim() }, err);
|
|
105
|
-
return '';
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const SSR_RESERVED = new Set([
|
|
112
|
-
'state', 'render', 'styles', 'init', 'mounted', 'updated', 'destroyed',
|
|
113
|
-
'props', 'templateUrl', 'styleUrl', 'templates',
|
|
114
|
-
'base', 'computed', 'watch'
|
|
115
|
-
]);
|
|
116
|
-
|
|
117
|
-
// ---------------------------------------------------------------------------
|
|
118
|
-
// HTML escaping for SSR output
|
|
119
|
-
// ---------------------------------------------------------------------------
|
|
120
|
-
const _escapeMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
121
|
-
function _escapeHtml(str) {
|
|
122
|
-
return str.replace(/[&<>"']/g, c => _escapeMap[c]);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// ---------------------------------------------------------------------------
|
|
126
|
-
// SSR App - component registry + renderer
|
|
127
|
-
// ---------------------------------------------------------------------------
|
|
128
|
-
class SSRApp {
|
|
129
|
-
constructor() {
|
|
130
|
-
this._registry = new Map();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Register a component for SSR.
|
|
135
|
-
* @param {string} name
|
|
136
|
-
* @param {object} definition
|
|
137
|
-
*/
|
|
138
|
-
component(name, definition) {
|
|
139
|
-
if (typeof name !== 'string' || !name) {
|
|
140
|
-
throw new ZQueryError(ErrorCode.SSR_COMPONENT, 'Component name must be a non-empty string');
|
|
141
|
-
}
|
|
142
|
-
if (!definition || typeof definition !== 'object') {
|
|
143
|
-
throw new ZQueryError(ErrorCode.SSR_COMPONENT, `Invalid definition for component "${name}"`);
|
|
144
|
-
}
|
|
145
|
-
this._registry.set(name, definition);
|
|
146
|
-
return this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Check whether a component is registered.
|
|
151
|
-
* @param {string} name
|
|
152
|
-
* @returns {boolean}
|
|
153
|
-
*/
|
|
154
|
-
has(name) {
|
|
155
|
-
return this._registry.has(name);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Render a component to an HTML string.
|
|
160
|
-
*
|
|
161
|
-
* @param {string} componentName - registered component name
|
|
162
|
-
* @param {object} [props] - props to pass
|
|
163
|
-
* @param {object} [options] - rendering options
|
|
164
|
-
* @param {boolean} [options.hydrate=true] - add hydration marker
|
|
165
|
-
* @param {string} [options.mode='html'] - 'html' (default) or 'fragment' (no wrapper tag)
|
|
166
|
-
* @returns {Promise<string>} - rendered HTML
|
|
167
|
-
*/
|
|
168
|
-
async renderToString(componentName, props = {}, options = {}) {
|
|
169
|
-
const def = this._registry.get(componentName);
|
|
170
|
-
if (!def) {
|
|
171
|
-
throw new ZQueryError(
|
|
172
|
-
ErrorCode.SSR_COMPONENT,
|
|
173
|
-
`SSR: Component "${componentName}" not registered`,
|
|
174
|
-
{ component: componentName }
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const instance = new SSRComponent(def, props);
|
|
179
|
-
let html = instance.render();
|
|
180
|
-
|
|
181
|
-
// Strip z-cloak attributes (they're only for client-side FOUC prevention)
|
|
182
|
-
html = html.replace(/\s*z-cloak\s*/g, ' ');
|
|
183
|
-
|
|
184
|
-
// Clean up SSR-irrelevant attributes
|
|
185
|
-
html = html.replace(/\s*@[\w.]+="[^"]*"/g, ''); // Remove event bindings
|
|
186
|
-
html = html.replace(/\s*z-on:[\w.]+="[^"]*"/g, '');
|
|
187
|
-
|
|
188
|
-
// Fragment mode - return inner HTML without wrapper tag
|
|
189
|
-
if (options.mode === 'fragment') return html;
|
|
190
|
-
|
|
191
|
-
const hydrate = options.hydrate !== false;
|
|
192
|
-
const marker = hydrate ? ' data-zq-ssr' : '';
|
|
193
|
-
|
|
194
|
-
return `<${componentName}${marker}>${html}</${componentName}>`;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Render multiple components as a batch.
|
|
199
|
-
*
|
|
200
|
-
* @param {Array<{ name: string, props?: object, options?: object }>} entries
|
|
201
|
-
* @returns {Promise<string[]>} - array of rendered HTML strings
|
|
202
|
-
*/
|
|
203
|
-
async renderBatch(entries) {
|
|
204
|
-
return Promise.all(
|
|
205
|
-
entries.map(({ name, props, options }) => this.renderToString(name, props, options))
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Render a full HTML page with a component mounted in a shell.
|
|
211
|
-
*
|
|
212
|
-
* @param {object} options
|
|
213
|
-
* @param {string} options.component - component name to render
|
|
214
|
-
* @param {object} [options.props] - props
|
|
215
|
-
* @param {string} [options.title] - page title
|
|
216
|
-
* @param {string} [options.description] - meta description for SEO
|
|
217
|
-
* @param {string[]} [options.styles] - CSS file paths
|
|
218
|
-
* @param {string[]} [options.scripts] - JS file paths
|
|
219
|
-
* @param {string} [options.lang] - html lang attribute
|
|
220
|
-
* @param {string} [options.meta] - additional head content
|
|
221
|
-
* @param {string} [options.bodyAttrs] - extra body attributes
|
|
222
|
-
* @param {object} [options.head] - structured head options
|
|
223
|
-
* @param {string} [options.head.canonical] - canonical URL
|
|
224
|
-
* @param {object} [options.head.og] - Open Graph tags
|
|
225
|
-
* @returns {Promise<string>}
|
|
226
|
-
*/
|
|
227
|
-
async renderPage(options = {}) {
|
|
228
|
-
const {
|
|
229
|
-
component: comp,
|
|
230
|
-
props = {},
|
|
231
|
-
title = '',
|
|
232
|
-
description = '',
|
|
233
|
-
styles = [],
|
|
234
|
-
scripts = [],
|
|
235
|
-
lang = 'en',
|
|
236
|
-
meta = '',
|
|
237
|
-
bodyAttrs = '',
|
|
238
|
-
head = {},
|
|
239
|
-
} = options;
|
|
240
|
-
|
|
241
|
-
let content = '';
|
|
242
|
-
if (comp) {
|
|
243
|
-
try {
|
|
244
|
-
content = await this.renderToString(comp, props);
|
|
245
|
-
} catch (err) {
|
|
246
|
-
reportError(ErrorCode.SSR_PAGE, `renderPage failed for component "${comp}"`, { component: comp }, err);
|
|
247
|
-
content = `<!-- SSR error: ${_escapeHtml(err.message)} -->`;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const styleLinks = styles.map(s => `<link rel="stylesheet" href="${_escapeHtml(s)}">`).join('\n ');
|
|
252
|
-
const scriptTags = scripts.map(s => `<script src="${_escapeHtml(s)}"></script>`).join('\n ');
|
|
253
|
-
|
|
254
|
-
// Build SEO / structured head tags
|
|
255
|
-
let headExtra = meta;
|
|
256
|
-
if (description) {
|
|
257
|
-
headExtra += `\n <meta name="description" content="${_escapeHtml(description)}">`;
|
|
258
|
-
}
|
|
259
|
-
if (head.canonical) {
|
|
260
|
-
headExtra += `\n <link rel="canonical" href="${_escapeHtml(head.canonical)}">`;
|
|
261
|
-
}
|
|
262
|
-
if (head.og) {
|
|
263
|
-
for (const [key, val] of Object.entries(head.og)) {
|
|
264
|
-
headExtra += `\n <meta property="og:${_escapeHtml(key)}" content="${_escapeHtml(String(val))}">`;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return `<!DOCTYPE html>
|
|
269
|
-
<html lang="${_escapeHtml(lang)}">
|
|
270
|
-
<head>
|
|
271
|
-
<meta charset="UTF-8">
|
|
272
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
273
|
-
<title>${_escapeHtml(title)}</title>
|
|
274
|
-
${headExtra}
|
|
275
|
-
${styleLinks}
|
|
276
|
-
</head>
|
|
277
|
-
<body ${bodyAttrs.replace(/on\w+\s*=/gi, '').replace(/javascript\s*:/gi, '')}>
|
|
278
|
-
<div id="app">${content}</div>
|
|
279
|
-
${scriptTags}
|
|
280
|
-
</body>
|
|
281
|
-
</html>`;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Render a component into an existing HTML shell template.
|
|
286
|
-
*
|
|
287
|
-
* Unlike renderPage() which generates a full HTML document from scratch,
|
|
288
|
-
* renderShell() takes your own index.html (with nav, footer, custom markup)
|
|
289
|
-
* and injects the SSR-rendered component body plus metadata into it.
|
|
290
|
-
*
|
|
291
|
-
* Handles:
|
|
292
|
-
* - Component rendering into <z-outlet>
|
|
293
|
-
* - <title> replacement
|
|
294
|
-
* - <meta name="description"> replacement
|
|
295
|
-
* - Open Graph meta tag replacement (og:title, og:description, og:type, etc.)
|
|
296
|
-
* - window.__SSR_DATA__ hydration script injection
|
|
297
|
-
*
|
|
298
|
-
* @param {string} shell - HTML template string (your index.html)
|
|
299
|
-
* @param {object} options
|
|
300
|
-
* @param {string} options.component - registered component name to render
|
|
301
|
-
* @param {object} [options.props] - props passed to the component
|
|
302
|
-
* @param {string} [options.title] - page title (replaces <title>)
|
|
303
|
-
* @param {string} [options.description] - meta description (replaces <meta name="description">)
|
|
304
|
-
* @param {object} [options.og] - Open Graph tags to replace (e.g. { title, description, type, image })
|
|
305
|
-
* @param {any} [options.ssrData] - data to embed as window.__SSR_DATA__ for client hydration
|
|
306
|
-
* @param {object} [options.renderOptions] - options passed to renderToString (hydrate, mode)
|
|
307
|
-
* @returns {Promise<string>} - the shell with SSR content and metadata injected
|
|
308
|
-
*/
|
|
309
|
-
async renderShell(shell, options = {}) {
|
|
310
|
-
const {
|
|
311
|
-
component: comp,
|
|
312
|
-
props = {},
|
|
313
|
-
title,
|
|
314
|
-
description,
|
|
315
|
-
og,
|
|
316
|
-
ssrData,
|
|
317
|
-
renderOptions,
|
|
318
|
-
} = options;
|
|
319
|
-
|
|
320
|
-
// Render the component
|
|
321
|
-
let body = '';
|
|
322
|
-
if (comp) {
|
|
323
|
-
try {
|
|
324
|
-
body = await this.renderToString(comp, props, renderOptions);
|
|
325
|
-
} catch (err) {
|
|
326
|
-
reportError(ErrorCode.SSR_PAGE, `renderShell failed for component "${comp}"`, { component: comp }, err);
|
|
327
|
-
body = `<!-- SSR error: ${_escapeHtml(err.message)} -->`;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
let html = shell;
|
|
332
|
-
|
|
333
|
-
// Inject SSR body into <z-outlet>
|
|
334
|
-
// Use a replacer function to avoid $ substitution patterns in body
|
|
335
|
-
html = html.replace(/(<z-outlet[^>]*>)([\s\S]*?)(<\/z-outlet>)?/, (_, open) => `${open}${body}</z-outlet>`);
|
|
336
|
-
|
|
337
|
-
// Replace <title>
|
|
338
|
-
if (title != null) {
|
|
339
|
-
const safeTitle = _escapeHtml(title);
|
|
340
|
-
html = html.replace(/<title>[^<]*<\/title>/, () => `<title>${safeTitle}</title>`);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// Replace <meta name="description">
|
|
344
|
-
if (description != null) {
|
|
345
|
-
const safeDesc = _escapeHtml(description);
|
|
346
|
-
html = html.replace(
|
|
347
|
-
/<meta\s+name="description"\s+content="[^"]*">/,
|
|
348
|
-
() => `<meta name="description" content="${safeDesc}">`
|
|
349
|
-
);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// Replace Open Graph meta tags
|
|
353
|
-
if (og) {
|
|
354
|
-
for (const [key, value] of Object.entries(og)) {
|
|
355
|
-
// Sanitize key: allow only safe OG property characters (alphanumeric, hyphens, underscores, colons)
|
|
356
|
-
const safeKey = key.replace(/[^a-zA-Z0-9_:\-]/g, '');
|
|
357
|
-
if (!safeKey) continue;
|
|
358
|
-
const escaped = _escapeHtml(String(value));
|
|
359
|
-
// Escape key for use in RegExp to prevent ReDoS
|
|
360
|
-
const escapedKey = safeKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
361
|
-
const pattern = new RegExp(`<meta\\s+property="og:${escapedKey}"\\s+content="[^"]*">`);
|
|
362
|
-
if (pattern.test(html)) {
|
|
363
|
-
html = html.replace(pattern, () => `<meta property="og:${safeKey}" content="${escaped}">`);
|
|
364
|
-
} else {
|
|
365
|
-
// Tag doesn't exist — inject before </head>
|
|
366
|
-
html = html.replace('</head>', () => `<meta property="og:${safeKey}" content="${escaped}">\n</head>`);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// Inject hydration data as window.__SSR_DATA__
|
|
372
|
-
if (ssrData !== undefined) {
|
|
373
|
-
// Escape </script> and <!-- sequences to prevent breaking out of the script tag
|
|
374
|
-
const json = JSON.stringify(ssrData).replace(/<\//g, '<\\/').replace(/<!--/g, '<\\!--');
|
|
375
|
-
html = html.replace('</head>', () => `<script>window.__SSR_DATA__=${json};</script>\n</head>`);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
return html;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
// ---------------------------------------------------------------------------
|
|
383
|
-
// Public API
|
|
384
|
-
// ---------------------------------------------------------------------------
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Create an SSR application instance.
|
|
388
|
-
* @returns {SSRApp}
|
|
389
|
-
*/
|
|
390
|
-
export function createSSRApp() {
|
|
391
|
-
return new SSRApp();
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Quick one-shot render of a component definition to string.
|
|
396
|
-
* @param {object} definition - component definition
|
|
397
|
-
* @param {object} [props] - props
|
|
398
|
-
* @returns {string}
|
|
399
|
-
*/
|
|
400
|
-
export function renderToString(definition, props = {}) {
|
|
401
|
-
if (!definition || typeof definition !== 'object') {
|
|
402
|
-
throw new ZQueryError(ErrorCode.SSR_COMPONENT, 'renderToString requires a component definition object');
|
|
403
|
-
}
|
|
404
|
-
const instance = new SSRComponent(definition, props);
|
|
405
|
-
return instance.render();
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* Escape HTML entities - exposed for use in SSR templates.
|
|
410
|
-
* @param {string} str
|
|
411
|
-
* @returns {string}
|
|
412
|
-
*/
|
|
413
|
-
export function escapeHtml(str) {
|
|
414
|
-
return _escapeHtml(String(str));
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// Re-export matchRoute so SSR servers can import from 'zero-query/ssr'
|
|
418
|
-
export { matchRoute } from './router.js';
|
|
1
|
+
/**
|
|
2
|
+
* zQuery SSR - Server-side rendering to HTML string
|
|
3
|
+
*
|
|
4
|
+
* Renders registered components to static HTML strings for SEO,
|
|
5
|
+
* initial page load performance, and static site generation.
|
|
6
|
+
*
|
|
7
|
+
* Works in Node.js - no DOM required for basic rendering.
|
|
8
|
+
* Supports hydration markers for client-side takeover.
|
|
9
|
+
*
|
|
10
|
+
* Usage (Node.js):
|
|
11
|
+
* import { renderToString, createSSRApp } from 'zero-query/ssr';
|
|
12
|
+
*
|
|
13
|
+
* const app = createSSRApp();
|
|
14
|
+
* app.component('my-page', {
|
|
15
|
+
* state: () => ({ title: 'Hello' }),
|
|
16
|
+
* render() { return `<h1>${this.state.title}</h1>`; }
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const html = await app.renderToString('my-page', { title: 'World' });
|
|
20
|
+
* // → '<my-page data-zq-ssr><h1>World</h1></my-page>'
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { safeEval } from './expression.js';
|
|
24
|
+
import { reportError, ErrorCode, ZQueryError } from './errors.js';
|
|
25
|
+
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// SSR Component renderer
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
class SSRComponent {
|
|
30
|
+
constructor(definition, props = {}) {
|
|
31
|
+
this._def = definition;
|
|
32
|
+
this.props = Object.freeze({ ...props });
|
|
33
|
+
this.refs = {};
|
|
34
|
+
this.templates = {};
|
|
35
|
+
this.computed = {};
|
|
36
|
+
|
|
37
|
+
// Initialize state
|
|
38
|
+
const initialState = typeof definition.state === 'function'
|
|
39
|
+
? definition.state()
|
|
40
|
+
: { ...(definition.state || {}) };
|
|
41
|
+
this.state = initialState;
|
|
42
|
+
|
|
43
|
+
// Add __raw to match client-side API
|
|
44
|
+
Object.defineProperty(this.state, '__raw', { value: this.state, enumerable: false });
|
|
45
|
+
|
|
46
|
+
// Computed properties
|
|
47
|
+
if (definition.computed) {
|
|
48
|
+
for (const [name, fn] of Object.entries(definition.computed)) {
|
|
49
|
+
Object.defineProperty(this.computed, name, {
|
|
50
|
+
get: () => {
|
|
51
|
+
try {
|
|
52
|
+
return fn.call(this, this.state);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
reportError(ErrorCode.SSR_RENDER, `Computed property "${name}" threw during SSR`, { property: name }, err);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
enumerable: true
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Bind user methods
|
|
64
|
+
for (const [key, val] of Object.entries(definition)) {
|
|
65
|
+
if (typeof val === 'function' && !SSR_RESERVED.has(key)) {
|
|
66
|
+
this[key] = val.bind(this);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Init lifecycle - guarded so a broken init doesn't crash the whole render
|
|
71
|
+
if (definition.init) {
|
|
72
|
+
try {
|
|
73
|
+
definition.init.call(this);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
reportError(ErrorCode.SSR_RENDER, 'Component init() threw during SSR', {}, err);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
render() {
|
|
81
|
+
if (this._def.render) {
|
|
82
|
+
try {
|
|
83
|
+
let html = this._def.render.call(this);
|
|
84
|
+
html = this._interpolate(html);
|
|
85
|
+
return html;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
reportError(ErrorCode.SSR_RENDER, 'Component render() threw during SSR', {}, err);
|
|
88
|
+
return `<!-- SSR render error -->`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return '';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Basic {{expression}} interpolation for SSR
|
|
95
|
+
_interpolate(html) {
|
|
96
|
+
return html.replace(/\{\{(.+?)\}\}/g, (_, expr) => {
|
|
97
|
+
try {
|
|
98
|
+
const result = safeEval(expr.trim(), [
|
|
99
|
+
this.state,
|
|
100
|
+
{ props: this.props, computed: this.computed }
|
|
101
|
+
]);
|
|
102
|
+
return result != null ? _escapeHtml(String(result)) : '';
|
|
103
|
+
} catch (err) {
|
|
104
|
+
reportError(ErrorCode.SSR_RENDER, `Expression "{{${expr.trim()}}}" failed during SSR`, { expression: expr.trim() }, err);
|
|
105
|
+
return '';
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const SSR_RESERVED = new Set([
|
|
112
|
+
'state', 'render', 'styles', 'init', 'mounted', 'updated', 'destroyed',
|
|
113
|
+
'props', 'templateUrl', 'styleUrl', 'templates',
|
|
114
|
+
'base', 'computed', 'watch'
|
|
115
|
+
]);
|
|
116
|
+
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
// HTML escaping for SSR output
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
const _escapeMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
121
|
+
function _escapeHtml(str) {
|
|
122
|
+
return str.replace(/[&<>"']/g, c => _escapeMap[c]);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
// SSR App - component registry + renderer
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
class SSRApp {
|
|
129
|
+
constructor() {
|
|
130
|
+
this._registry = new Map();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Register a component for SSR.
|
|
135
|
+
* @param {string} name
|
|
136
|
+
* @param {object} definition
|
|
137
|
+
*/
|
|
138
|
+
component(name, definition) {
|
|
139
|
+
if (typeof name !== 'string' || !name) {
|
|
140
|
+
throw new ZQueryError(ErrorCode.SSR_COMPONENT, 'Component name must be a non-empty string');
|
|
141
|
+
}
|
|
142
|
+
if (!definition || typeof definition !== 'object') {
|
|
143
|
+
throw new ZQueryError(ErrorCode.SSR_COMPONENT, `Invalid definition for component "${name}"`);
|
|
144
|
+
}
|
|
145
|
+
this._registry.set(name, definition);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Check whether a component is registered.
|
|
151
|
+
* @param {string} name
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
has(name) {
|
|
155
|
+
return this._registry.has(name);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Render a component to an HTML string.
|
|
160
|
+
*
|
|
161
|
+
* @param {string} componentName - registered component name
|
|
162
|
+
* @param {object} [props] - props to pass
|
|
163
|
+
* @param {object} [options] - rendering options
|
|
164
|
+
* @param {boolean} [options.hydrate=true] - add hydration marker
|
|
165
|
+
* @param {string} [options.mode='html'] - 'html' (default) or 'fragment' (no wrapper tag)
|
|
166
|
+
* @returns {Promise<string>} - rendered HTML
|
|
167
|
+
*/
|
|
168
|
+
async renderToString(componentName, props = {}, options = {}) {
|
|
169
|
+
const def = this._registry.get(componentName);
|
|
170
|
+
if (!def) {
|
|
171
|
+
throw new ZQueryError(
|
|
172
|
+
ErrorCode.SSR_COMPONENT,
|
|
173
|
+
`SSR: Component "${componentName}" not registered`,
|
|
174
|
+
{ component: componentName }
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const instance = new SSRComponent(def, props);
|
|
179
|
+
let html = instance.render();
|
|
180
|
+
|
|
181
|
+
// Strip z-cloak attributes (they're only for client-side FOUC prevention)
|
|
182
|
+
html = html.replace(/\s*z-cloak\s*/g, ' ');
|
|
183
|
+
|
|
184
|
+
// Clean up SSR-irrelevant attributes
|
|
185
|
+
html = html.replace(/\s*@[\w.]+="[^"]*"/g, ''); // Remove event bindings
|
|
186
|
+
html = html.replace(/\s*z-on:[\w.]+="[^"]*"/g, '');
|
|
187
|
+
|
|
188
|
+
// Fragment mode - return inner HTML without wrapper tag
|
|
189
|
+
if (options.mode === 'fragment') return html;
|
|
190
|
+
|
|
191
|
+
const hydrate = options.hydrate !== false;
|
|
192
|
+
const marker = hydrate ? ' data-zq-ssr' : '';
|
|
193
|
+
|
|
194
|
+
return `<${componentName}${marker}>${html}</${componentName}>`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Render multiple components as a batch.
|
|
199
|
+
*
|
|
200
|
+
* @param {Array<{ name: string, props?: object, options?: object }>} entries
|
|
201
|
+
* @returns {Promise<string[]>} - array of rendered HTML strings
|
|
202
|
+
*/
|
|
203
|
+
async renderBatch(entries) {
|
|
204
|
+
return Promise.all(
|
|
205
|
+
entries.map(({ name, props, options }) => this.renderToString(name, props, options))
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Render a full HTML page with a component mounted in a shell.
|
|
211
|
+
*
|
|
212
|
+
* @param {object} options
|
|
213
|
+
* @param {string} options.component - component name to render
|
|
214
|
+
* @param {object} [options.props] - props
|
|
215
|
+
* @param {string} [options.title] - page title
|
|
216
|
+
* @param {string} [options.description] - meta description for SEO
|
|
217
|
+
* @param {string[]} [options.styles] - CSS file paths
|
|
218
|
+
* @param {string[]} [options.scripts] - JS file paths
|
|
219
|
+
* @param {string} [options.lang] - html lang attribute
|
|
220
|
+
* @param {string} [options.meta] - additional head content
|
|
221
|
+
* @param {string} [options.bodyAttrs] - extra body attributes
|
|
222
|
+
* @param {object} [options.head] - structured head options
|
|
223
|
+
* @param {string} [options.head.canonical] - canonical URL
|
|
224
|
+
* @param {object} [options.head.og] - Open Graph tags
|
|
225
|
+
* @returns {Promise<string>}
|
|
226
|
+
*/
|
|
227
|
+
async renderPage(options = {}) {
|
|
228
|
+
const {
|
|
229
|
+
component: comp,
|
|
230
|
+
props = {},
|
|
231
|
+
title = '',
|
|
232
|
+
description = '',
|
|
233
|
+
styles = [],
|
|
234
|
+
scripts = [],
|
|
235
|
+
lang = 'en',
|
|
236
|
+
meta = '',
|
|
237
|
+
bodyAttrs = '',
|
|
238
|
+
head = {},
|
|
239
|
+
} = options;
|
|
240
|
+
|
|
241
|
+
let content = '';
|
|
242
|
+
if (comp) {
|
|
243
|
+
try {
|
|
244
|
+
content = await this.renderToString(comp, props);
|
|
245
|
+
} catch (err) {
|
|
246
|
+
reportError(ErrorCode.SSR_PAGE, `renderPage failed for component "${comp}"`, { component: comp }, err);
|
|
247
|
+
content = `<!-- SSR error: ${_escapeHtml(err.message)} -->`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const styleLinks = styles.map(s => `<link rel="stylesheet" href="${_escapeHtml(s)}">`).join('\n ');
|
|
252
|
+
const scriptTags = scripts.map(s => `<script src="${_escapeHtml(s)}"></script>`).join('\n ');
|
|
253
|
+
|
|
254
|
+
// Build SEO / structured head tags
|
|
255
|
+
let headExtra = meta;
|
|
256
|
+
if (description) {
|
|
257
|
+
headExtra += `\n <meta name="description" content="${_escapeHtml(description)}">`;
|
|
258
|
+
}
|
|
259
|
+
if (head.canonical) {
|
|
260
|
+
headExtra += `\n <link rel="canonical" href="${_escapeHtml(head.canonical)}">`;
|
|
261
|
+
}
|
|
262
|
+
if (head.og) {
|
|
263
|
+
for (const [key, val] of Object.entries(head.og)) {
|
|
264
|
+
headExtra += `\n <meta property="og:${_escapeHtml(key)}" content="${_escapeHtml(String(val))}">`;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return `<!DOCTYPE html>
|
|
269
|
+
<html lang="${_escapeHtml(lang)}">
|
|
270
|
+
<head>
|
|
271
|
+
<meta charset="UTF-8">
|
|
272
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
273
|
+
<title>${_escapeHtml(title)}</title>
|
|
274
|
+
${headExtra}
|
|
275
|
+
${styleLinks}
|
|
276
|
+
</head>
|
|
277
|
+
<body ${bodyAttrs.replace(/on\w+\s*=/gi, '').replace(/javascript\s*:/gi, '')}>
|
|
278
|
+
<div id="app">${content}</div>
|
|
279
|
+
${scriptTags}
|
|
280
|
+
</body>
|
|
281
|
+
</html>`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Render a component into an existing HTML shell template.
|
|
286
|
+
*
|
|
287
|
+
* Unlike renderPage() which generates a full HTML document from scratch,
|
|
288
|
+
* renderShell() takes your own index.html (with nav, footer, custom markup)
|
|
289
|
+
* and injects the SSR-rendered component body plus metadata into it.
|
|
290
|
+
*
|
|
291
|
+
* Handles:
|
|
292
|
+
* - Component rendering into <z-outlet>
|
|
293
|
+
* - <title> replacement
|
|
294
|
+
* - <meta name="description"> replacement
|
|
295
|
+
* - Open Graph meta tag replacement (og:title, og:description, og:type, etc.)
|
|
296
|
+
* - window.__SSR_DATA__ hydration script injection
|
|
297
|
+
*
|
|
298
|
+
* @param {string} shell - HTML template string (your index.html)
|
|
299
|
+
* @param {object} options
|
|
300
|
+
* @param {string} options.component - registered component name to render
|
|
301
|
+
* @param {object} [options.props] - props passed to the component
|
|
302
|
+
* @param {string} [options.title] - page title (replaces <title>)
|
|
303
|
+
* @param {string} [options.description] - meta description (replaces <meta name="description">)
|
|
304
|
+
* @param {object} [options.og] - Open Graph tags to replace (e.g. { title, description, type, image })
|
|
305
|
+
* @param {any} [options.ssrData] - data to embed as window.__SSR_DATA__ for client hydration
|
|
306
|
+
* @param {object} [options.renderOptions] - options passed to renderToString (hydrate, mode)
|
|
307
|
+
* @returns {Promise<string>} - the shell with SSR content and metadata injected
|
|
308
|
+
*/
|
|
309
|
+
async renderShell(shell, options = {}) {
|
|
310
|
+
const {
|
|
311
|
+
component: comp,
|
|
312
|
+
props = {},
|
|
313
|
+
title,
|
|
314
|
+
description,
|
|
315
|
+
og,
|
|
316
|
+
ssrData,
|
|
317
|
+
renderOptions,
|
|
318
|
+
} = options;
|
|
319
|
+
|
|
320
|
+
// Render the component
|
|
321
|
+
let body = '';
|
|
322
|
+
if (comp) {
|
|
323
|
+
try {
|
|
324
|
+
body = await this.renderToString(comp, props, renderOptions);
|
|
325
|
+
} catch (err) {
|
|
326
|
+
reportError(ErrorCode.SSR_PAGE, `renderShell failed for component "${comp}"`, { component: comp }, err);
|
|
327
|
+
body = `<!-- SSR error: ${_escapeHtml(err.message)} -->`;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
let html = shell;
|
|
332
|
+
|
|
333
|
+
// Inject SSR body into <z-outlet>
|
|
334
|
+
// Use a replacer function to avoid $ substitution patterns in body
|
|
335
|
+
html = html.replace(/(<z-outlet[^>]*>)([\s\S]*?)(<\/z-outlet>)?/, (_, open) => `${open}${body}</z-outlet>`);
|
|
336
|
+
|
|
337
|
+
// Replace <title>
|
|
338
|
+
if (title != null) {
|
|
339
|
+
const safeTitle = _escapeHtml(title);
|
|
340
|
+
html = html.replace(/<title>[^<]*<\/title>/, () => `<title>${safeTitle}</title>`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Replace <meta name="description">
|
|
344
|
+
if (description != null) {
|
|
345
|
+
const safeDesc = _escapeHtml(description);
|
|
346
|
+
html = html.replace(
|
|
347
|
+
/<meta\s+name="description"\s+content="[^"]*">/,
|
|
348
|
+
() => `<meta name="description" content="${safeDesc}">`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Replace Open Graph meta tags
|
|
353
|
+
if (og) {
|
|
354
|
+
for (const [key, value] of Object.entries(og)) {
|
|
355
|
+
// Sanitize key: allow only safe OG property characters (alphanumeric, hyphens, underscores, colons)
|
|
356
|
+
const safeKey = key.replace(/[^a-zA-Z0-9_:\-]/g, '');
|
|
357
|
+
if (!safeKey) continue;
|
|
358
|
+
const escaped = _escapeHtml(String(value));
|
|
359
|
+
// Escape key for use in RegExp to prevent ReDoS
|
|
360
|
+
const escapedKey = safeKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
361
|
+
const pattern = new RegExp(`<meta\\s+property="og:${escapedKey}"\\s+content="[^"]*">`);
|
|
362
|
+
if (pattern.test(html)) {
|
|
363
|
+
html = html.replace(pattern, () => `<meta property="og:${safeKey}" content="${escaped}">`);
|
|
364
|
+
} else {
|
|
365
|
+
// Tag doesn't exist — inject before </head>
|
|
366
|
+
html = html.replace('</head>', () => `<meta property="og:${safeKey}" content="${escaped}">\n</head>`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Inject hydration data as window.__SSR_DATA__
|
|
372
|
+
if (ssrData !== undefined) {
|
|
373
|
+
// Escape </script> and <!-- sequences to prevent breaking out of the script tag
|
|
374
|
+
const json = JSON.stringify(ssrData).replace(/<\//g, '<\\/').replace(/<!--/g, '<\\!--');
|
|
375
|
+
html = html.replace('</head>', () => `<script>window.__SSR_DATA__=${json};</script>\n</head>`);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return html;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
// Public API
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Create an SSR application instance.
|
|
388
|
+
* @returns {SSRApp}
|
|
389
|
+
*/
|
|
390
|
+
export function createSSRApp() {
|
|
391
|
+
return new SSRApp();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Quick one-shot render of a component definition to string.
|
|
396
|
+
* @param {object} definition - component definition
|
|
397
|
+
* @param {object} [props] - props
|
|
398
|
+
* @returns {string}
|
|
399
|
+
*/
|
|
400
|
+
export function renderToString(definition, props = {}) {
|
|
401
|
+
if (!definition || typeof definition !== 'object') {
|
|
402
|
+
throw new ZQueryError(ErrorCode.SSR_COMPONENT, 'renderToString requires a component definition object');
|
|
403
|
+
}
|
|
404
|
+
const instance = new SSRComponent(definition, props);
|
|
405
|
+
return instance.render();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Escape HTML entities - exposed for use in SSR templates.
|
|
410
|
+
* @param {string} str
|
|
411
|
+
* @returns {string}
|
|
412
|
+
*/
|
|
413
|
+
export function escapeHtml(str) {
|
|
414
|
+
return _escapeHtml(String(str));
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Re-export matchRoute so SSR servers can import from 'zero-query/ssr'
|
|
418
|
+
export { matchRoute } from './router.js';
|