-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-ssr.mjs
More file actions
374 lines (329 loc) · 13.8 KB
/
verify-ssr.mjs
File metadata and controls
374 lines (329 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* eslint-disable */
/**
* Manual SSR verification script.
*
* Run with: node verify-ssr.mjs
*
* Checks:
* 1. DSD output — <template shadowrootmode="open"> with CSS injected
* 2. JIT CSS inside shadow root
* 3. cer-suspense in SSR (pending=false and pending=true)
* 4. cer-error-boundary in SSR
* 5. cer-keep-alive in SSR (opaque shell — no DSD wrapping)
* 6. matchRouteSSR query string + fragment stripping
* 7. matchRouteSSR dynamic params
* 8. createSSRHandler error handling
*/
import {
component,
html,
registerBuiltinComponents,
} from './dist/custom-elements-runtime.es.js';
import {
renderToStringWithJITCSSDSD,
renderToStream,
} from './dist/custom-elements-runtime.ssr.es.js';
import { matchRouteSSR } from './dist/custom-elements-runtime.router.es.js';
import { createSSRHandler } from './dist/custom-elements-runtime.ssr-middleware.es.js';
// ─────────────────────────────────────────────────────────────
// Setup
// ─────────────────────────────────────────────────────────────
registerBuiltinComponents();
component(
'my-card',
() => html`<div class="p-4 flex gap-2"><slot></slot></div>`,
);
component(
'themed-box',
() => html`<div class="bg-primary-500 text-white p-8"><slot></slot></div>`,
);
// ─────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────
let passed = 0;
let failed = 0;
function check(label, value, expectFn) {
try {
expectFn(value);
console.log(` ✅ ${label}`);
passed++;
} catch (e) {
console.log(` ❌ ${label}`);
console.log(` ${e.message}`);
failed++;
}
}
function contains(str, substring) {
if (!str.includes(substring)) {
throw new Error(
`Expected output to contain: ${JSON.stringify(substring)}\n Got: ${str.slice(0, 300)}`,
);
}
}
function notContains(str, substring) {
if (str.includes(substring)) {
throw new Error(
`Expected output NOT to contain: ${JSON.stringify(substring)}`,
);
}
}
function eq(actual, expected) {
if (actual !== expected) {
throw new Error(
`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`,
);
}
}
// ─────────────────────────────────────────────────────────────
// 1. DSD output
// ─────────────────────────────────────────────────────────────
console.log('\n1. Declarative Shadow DOM output');
{
const { htmlWithStyles } = renderToStringWithJITCSSDSD(
html`<my-card>Hello SSR</my-card>`,
);
check('emits <template shadowrootmode="open">', htmlWithStyles, (v) =>
contains(v, 'shadowrootmode="open"'),
);
check('shadow content rendered inside template', htmlWithStyles, (v) =>
contains(v, '<div class="p-4 flex gap-2">'),
);
check('light DOM children outside template', htmlWithStyles, (v) =>
contains(v, 'Hello SSR'),
);
check('DSD polyfill injected', htmlWithStyles, (v) =>
contains(v, 'shadowRootMode'),
);
}
// ─────────────────────────────────────────────────────────────
// 2. JIT CSS inside shadow root
// ─────────────────────────────────────────────────────────────
console.log('\n2. JIT CSS inside shadow root');
{
const { htmlWithStyles } = renderToStringWithJITCSSDSD(
html`<themed-box>Hi</themed-box>`,
);
check('<style> block inside <template>', htmlWithStyles, (v) => {
const templateStart = v.indexOf('shadowrootmode');
const styleStart = v.indexOf('<style>', templateStart);
if (styleStart === -1) throw new Error('No <style> found inside template');
});
check('baseReset CSS present (box-sizing)', htmlWithStyles, (v) =>
contains(v, 'box-sizing'),
);
check('JIT utility class CSS generated (p-8)', htmlWithStyles, (v) =>
contains(v, '.p-8'),
);
}
// ─────────────────────────────────────────────────────────────
// 3. cer-suspense in SSR
// ─────────────────────────────────────────────────────────────
console.log('\n3. cer-suspense in SSR');
{
const { htmlWithStyles: defaultSlot } = renderToStringWithJITCSSDSD(
html`<cer-suspense><span>Content</span></cer-suspense>`,
);
check('DSD output present (pending=false)', defaultSlot, (v) =>
contains(v, 'shadowrootmode="open"'),
);
check('default <slot> rendered when pending=false', defaultSlot, (v) =>
contains(v, '<slot></slot>'),
);
check('light DOM child outside template', defaultSlot, (v) =>
contains(v, '<span>Content</span>'),
);
const { htmlWithStyles: fallbackSlot } = renderToStringWithJITCSSDSD(
html`<cer-suspense pending="true"
><span slot="fallback">Loading…</span></cer-suspense
>`,
);
check(
'fallback <slot name="fallback"> when pending=true',
fallbackSlot,
(v) => contains(v, 'name="fallback"'),
);
}
// ─────────────────────────────────────────────────────────────
// 4. cer-error-boundary in SSR
// ─────────────────────────────────────────────────────────────
console.log('\n4. cer-error-boundary in SSR');
{
const { htmlWithStyles } = renderToStringWithJITCSSDSD(
html`<cer-error-boundary><my-card>Safe</my-card></cer-error-boundary>`,
);
check('DSD output present', htmlWithStyles, (v) =>
contains(v, 'shadowrootmode="open"'),
);
check('default <slot> always rendered in SSR', htmlWithStyles, (v) =>
contains(v, '<slot></slot>'),
);
check('light DOM children present', htmlWithStyles, (v) =>
contains(v, '<my-card>'),
);
}
// ─────────────────────────────────────────────────────────────
// 5. cer-keep-alive in SSR (opaque shell)
// ─────────────────────────────────────────────────────────────
console.log('\n5. cer-keep-alive in SSR (opaque shell)');
{
const { htmlWithStyles } = renderToStringWithJITCSSDSD(
html`<cer-keep-alive><my-card>Kept</my-card></cer-keep-alive>`,
);
check('<cer-keep-alive> tag present in output', htmlWithStyles, (v) =>
contains(v, '<cer-keep-alive>'),
);
check('light DOM children rendered', htmlWithStyles, (v) =>
contains(v, '<my-card>'),
);
check(
'NO direct <template> for cer-keep-alive itself',
htmlWithStyles,
(v) => {
// The first <template> should belong to my-card, not cer-keep-alive
const kaStart = v.indexOf('<cer-keep-alive>');
const firstTemplate = v.indexOf('<template', kaStart);
const myCardStart = v.indexOf('<my-card', kaStart);
if (firstTemplate !== -1 && firstTemplate < myCardStart) {
throw new Error('cer-keep-alive incorrectly emitted a DSD <template>');
}
},
);
}
// ─────────────────────────────────────────────────────────────
// 6. matchRouteSSR — query string + fragment stripping
// ─────────────────────────────────────────────────────────────
console.log('\n6. matchRouteSSR query string / fragment stripping');
{
const routes = [
{ path: '/', component: 'home-page' },
{ path: '/about', component: 'about-page' },
{ path: '/blog/:slug', component: 'blog-post' },
{ path: '/docs/*', component: 'docs-page' },
];
const r1 = matchRouteSSR(routes, '/about?ref=email&page=2');
check('strips query string', r1.route?.component, (v) => eq(v, 'about-page'));
const r2 = matchRouteSSR(routes, '/about#section');
check('strips URL fragment', r2.route?.component, (v) => eq(v, 'about-page'));
const r3 = matchRouteSSR(routes, '/blog/hello-world?ref=email#comments');
check('strips both query + fragment', r3.route?.component, (v) =>
eq(v, 'blog-post'),
);
check('extracts dynamic param correctly', r3.params.slug, (v) =>
eq(v, 'hello-world'),
);
const r4 = matchRouteSSR(routes, '/docs/getting-started/intro');
check('splat route matches', r4.route?.component, (v) => eq(v, 'docs-page'));
const r5 = matchRouteSSR(routes, '/missing');
check('returns null for unmatched routes', r5.route, (v) => {
if (v !== null) throw new Error(`Expected null, got ${JSON.stringify(v)}`);
});
}
// ─────────────────────────────────────────────────────────────
// 7. createSSRHandler — error handling
// ─────────────────────────────────────────────────────────────
console.log('\n7. createSSRHandler error handling');
{
const handler = createSSRHandler(() => {
throw new Error('factory exploded');
});
let ended = '';
const res = {
setHeader() {},
end(data) {
ended = data ?? '';
},
};
let threw = false;
try {
await handler({ url: '/', method: 'GET', headers: {} }, res);
} catch (e) {
threw = e.message === 'factory exploded';
}
check('re-throws the original error', threw, (v) => {
if (!v) throw new Error('Handler did not re-throw');
});
check('still closes the response (no hanging request)', ended, (v) =>
contains(v, 'Internal Server Error'),
);
}
// ─────────────────────────────────────────────────────────────
// 8. renderToStream — incremental async component streaming
// ─────────────────────────────────────────────────────────────
console.log('\n8. renderToStream — incremental async component streaming');
{
// Component whose render function returns a Promise.
// In standard renderToStringWithJITCSSDSD this would produce an empty shell.
// With renderToStream it should resolve and appear as a swap-script chunk.
component('async-widget', async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
return html`<div class="p-4 bg-green-100">Async widget loaded</div>`;
});
const stream = renderToStream(
html`<div><async-widget></async-widget></div>`,
{ dsd: true },
);
// Drain the stream and collect all chunks
const chunks = [];
const reader = stream.getReader();
let done = false;
while (!done) {
const { value, done: d } = await reader.read();
if (value) chunks.push(value);
done = d;
}
check(
'stream produces ≥ 2 chunks (sync shell + async swap)',
chunks.length,
(v) => {
if (v < 2)
throw new Error(
`Expected ≥ 2 chunks, got ${v}. Async component was not deferred.`,
);
},
);
const syncChunk = chunks[0];
const asyncChunks = chunks.slice(1).join('');
check(
'chunk 1 — async placeholder has unique cer-stream-* id',
syncChunk,
(v) => contains(v, 'id="cer-stream-'),
);
check(
'chunk 1 — placeholder shadow template is empty (not yet resolved)',
syncChunk,
(v) => {
if (!v.includes('<template shadowrootmode="open"></template>')) {
throw new Error('Expected empty DSD placeholder in sync chunk');
}
},
);
check(
'chunk 2+ — contains shadowRoot.innerHTML swap script',
asyncChunks,
(v) => contains(v, 's.innerHTML'),
);
check(
'chunk 2+ — swap script carries resolved shadow content',
asyncChunks,
(v) => contains(v, 'Async widget loaded'),
);
check(
'chunk 2+ — swap script targets the correct placeholder id',
asyncChunks,
(v) => {
const placeholderId = syncChunk.match(/id="(cer-stream-\d+)"/)?.[1];
if (!placeholderId)
throw new Error('Could not find placeholder id in sync chunk');
if (!v.includes(placeholderId))
throw new Error(
`Swap script does not reference placeholder id "${placeholderId}"`,
);
},
);
}
// ─────────────────────────────────────────────────────────────
// Summary
// ─────────────────────────────────────────────────────────────
console.log(`\n${'─'.repeat(50)}`);
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) process.exit(1);