-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdefine-router.tsx
More file actions
823 lines (774 loc) · 24.4 KB
/
define-router.tsx
File metadata and controls
823 lines (774 loc) · 24.4 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import type { ReactNode } from 'react';
import { createCustomError, getErrorInfo } from '../lib/utils/custom-errors.js';
import { getPathMapping, path2regexp } from '../lib/utils/path.js';
import type { PathSpec } from '../lib/utils/path.js';
import { base64ToStream, streamToBase64 } from '../lib/utils/stream.js';
import { createTaskRunner } from '../lib/utils/task-runner.js';
import { unstable_defineHandlers as defineHandlers } from '../minimal/server.js';
import { unstable_getContext as getContext } from '../server.js';
import { INTERNAL_ServerRouter } from './client.js';
import {
HAS404_ID,
IS_STATIC_ID,
ROUTE_ID,
SKIP_HEADER,
decodeRoutePath,
decodeSliceId,
encodeRoutePath,
encodeSliceId,
} from './common.js';
export type ApiHandler = (
req: Request,
apiContext: { params: Record<string, string | string[]> },
) => Promise<Response>;
const isStringArray = (x: unknown): x is string[] =>
Array.isArray(x) && x.every((y) => typeof y === 'string');
const parseRscParams = (
rscParams: unknown,
): {
query: string;
} => {
if (rscParams instanceof URLSearchParams) {
return { query: rscParams.get('query') || '' };
}
if (
typeof (rscParams as { query?: undefined } | undefined)?.query === 'string'
) {
return { query: (rscParams as { query: string }).query };
}
return { query: '' };
};
const RSC_PATH_SYMBOL = Symbol('RSC_PATH');
const RSC_PARAMS_SYMBOL = Symbol('RSC_PARAMS');
const setRscPath = (rscPath: string) => {
try {
const context = getContext();
(context as unknown as Record<typeof RSC_PATH_SYMBOL, unknown>)[
RSC_PATH_SYMBOL
] = rscPath;
} catch {
// ignore
}
};
const setRscParams = (rscParams: unknown) => {
try {
const context = getContext();
(context as unknown as Record<typeof RSC_PARAMS_SYMBOL, unknown>)[
RSC_PARAMS_SYMBOL
] = rscParams;
} catch {
// ignore
}
};
export function unstable_getRscPath(): string | undefined {
try {
const context = getContext();
return (context as unknown as Record<typeof RSC_PATH_SYMBOL, string>)[
RSC_PATH_SYMBOL
];
} catch {
return undefined;
}
}
export function unstable_getRscParams(): unknown {
try {
const context = getContext();
return (context as unknown as Record<typeof RSC_PARAMS_SYMBOL, unknown>)[
RSC_PARAMS_SYMBOL
];
} catch {
return undefined;
}
}
const getNonce = () => {
try {
const context = getContext();
return context.nonce;
} catch {
return undefined;
}
};
const RERENDER_SYMBOL = Symbol('RERENDER');
type Rerender = (rscPath: string, rscParams?: unknown) => void;
const setRerender = (rerender: Rerender) => {
try {
const context = getContext();
(context as unknown as Record<typeof RERENDER_SYMBOL, Rerender>)[
RERENDER_SYMBOL
] = rerender;
} catch {
// ignore
}
};
const getRerender = (): Rerender => {
const context = getContext();
return (context as unknown as Record<typeof RERENDER_SYMBOL, Rerender>)[
RERENDER_SYMBOL
];
};
const is404 = (pathSpec: PathSpec) =>
pathSpec.length === 1 &&
pathSpec[0]!.type === 'literal' &&
pathSpec[0]!.name === '404';
const pathSpec2pathname = (pathSpec: PathSpec) => {
if (pathSpec.some(({ type }) => type !== 'literal')) {
return undefined;
}
return '/' + pathSpec.map(({ name }) => name!).join('/');
};
const htmlPath2pathname = (htmlPath: string): string =>
htmlPath === '/404' ? '404.html' : htmlPath + '/index.html';
export function unstable_rerenderRoute(pathname: string, query?: string) {
const rscPath = encodeRoutePath(pathname);
getRerender()(rscPath, query && new URLSearchParams({ query }));
}
export function unstable_notFound(): never {
throw createCustomError('Not Found', { status: 404 });
}
export function unstable_redirect(
location: string,
status: 303 | 307 | 308 = 307,
): never {
throw createCustomError('Redirect', { status, location });
}
type SlotId = string;
const ROOT_SLOT_ID = 'root';
const ROUTE_SLOT_ID_PREFIX = 'route:';
const SLICE_SLOT_ID_PREFIX = 'slice:';
const assertNonReservedSlotId = (slotId: SlotId) => {
if (
slotId === ROOT_SLOT_ID ||
slotId.startsWith(ROUTE_SLOT_ID_PREFIX) ||
slotId.startsWith(SLICE_SLOT_ID_PREFIX)
) {
throw new Error('Element ID cannot be "root", "route:*" or "slice:*"');
}
};
type RendererOption = { pathname: string; query: string | undefined };
type RouteConfig = {
type: 'route';
path: PathSpec;
isStatic: boolean;
pathPattern?: PathSpec;
rootElement: {
isStatic: boolean;
renderer: (option: RendererOption) => ReactNode;
};
routeElement: {
isStatic: boolean;
renderer: (option: RendererOption) => ReactNode;
};
elements: Record<
SlotId,
{
isStatic: boolean;
renderer: (option: RendererOption) => ReactNode;
}
>;
noSsr?: boolean;
slices?: string[];
};
type ApiConfig = {
type: 'api';
path: PathSpec;
isStatic: boolean;
handler: ApiHandler;
};
type SliceConfig = {
type: 'slice';
id: string;
isStatic: boolean;
renderer: () => Promise<ReactNode>;
};
const getRouterPrefetchCode = (path2moduleIds: Record<string, string[]>) => {
const moduleIdSet = new Set<string>();
Object.values(path2moduleIds).forEach((ids) =>
ids.forEach((id) => moduleIdSet.add(id)),
);
const ids = Array.from(moduleIdSet);
const path2idxs: Record<string, number[]> = {};
Object.entries(path2moduleIds).forEach(([path, ids]) => {
path2idxs[path] = ids.map((id) => ids.indexOf(id));
});
return `
globalThis.__WAKU_ROUTER_PREFETCH__ = (path, callback) => {
const ids = ${JSON.stringify(ids)};
const path2idxs = ${JSON.stringify(path2idxs)};
const key = Object.keys(path2idxs).find((key) => new RegExp(key).test(path));
for (const idx of path2idxs[key] || []) {
callback(ids[idx]);
}
};
`;
};
export function unstable_defineRouter(fns: {
getConfigs: () => Promise<Iterable<RouteConfig | ApiConfig | SliceConfig>>;
}) {
type RouterMode = 'runtime' | 'build';
// This is an internal type for caching
type MyConfig = {
configs: (RouteConfig | ApiConfig | SliceConfig)[];
has404: boolean;
};
const cachedMyConfigByMode = new Map<RouterMode, MyConfig>();
const getMyConfig = async (mode: RouterMode = 'runtime'): Promise<MyConfig> => {
let cachedMyConfig = cachedMyConfigByMode.get(mode);
if (!cachedMyConfig) {
const allConfigs = Array.from(await fns.getConfigs());
const configs =
mode === 'runtime'
? allConfigs.filter(
(item) =>
!(
(item.type === 'route' || item.type === 'api') &&
item.isStatic
),
)
: allConfigs;
let has404 = false;
configs.forEach((item) => {
if (item.type === 'route') {
Object.keys(item.elements).forEach(assertNonReservedSlotId);
if (!has404 && is404(item.path)) {
has404 = true;
}
}
});
cachedMyConfig = { configs, has404 };
cachedMyConfigByMode.set(mode, cachedMyConfig);
}
return cachedMyConfig;
};
const getPathConfigItem = async (
pathname: string,
mode: RouterMode = 'runtime',
) => {
const myConfig = await getMyConfig(mode);
const found = myConfig.configs.find(
(item): item is typeof item & { type: 'route' | 'api' } =>
(item.type === 'route' || item.type === 'api') &&
!!getPathMapping(item.path, pathname),
);
return found;
};
const getSliceElement = async (
sliceConfig: {
id: string;
isStatic: boolean;
renderer: () => Promise<ReactNode>;
},
getCachedElement: (id: SlotId) => Promise<ReactNode> | undefined,
setCachedElement: (id: SlotId, element: ReactNode) => Promise<ReactNode>,
): Promise<ReactNode> => {
const id = SLICE_SLOT_ID_PREFIX + sliceConfig.id;
const cached = getCachedElement(id);
if (cached) {
return cached;
}
let element = await sliceConfig.renderer();
if (sliceConfig.isStatic) {
element = await setCachedElement(id, element);
}
return element;
};
const getEntriesForRoute = async (
rscPath: string,
rscParams: unknown,
headers: Readonly<Record<string, string>>,
getCachedElement: (id: SlotId) => Promise<ReactNode> | undefined,
setCachedElement: (id: SlotId, element: ReactNode) => Promise<ReactNode>,
mode: RouterMode = 'runtime',
) => {
setRscPath(rscPath);
setRscParams(rscParams);
const pathname = decodeRoutePath(rscPath);
const pathConfigItem = await getPathConfigItem(pathname, mode);
if (pathConfigItem?.type !== 'route') {
return null;
}
let skipParam: unknown;
try {
skipParam = JSON.parse(headers[SKIP_HEADER.toLowerCase()] || '');
} catch {
// ignore
}
const skipIdSet = new Set(isStringArray(skipParam) ? skipParam : []);
const { query } = parseRscParams(rscParams);
const routeId = ROUTE_SLOT_ID_PREFIX + pathname;
const option: RendererOption = {
pathname,
query: pathConfigItem.isStatic ? undefined : query,
};
const myConfig = await getMyConfig(mode);
const slices = pathConfigItem.slices || [];
const sliceConfigMap = new Map<
string,
{ id: string; isStatic: boolean; renderer: () => Promise<ReactNode> }
>();
slices.forEach((sliceId) => {
const sliceConfig = myConfig.configs.find(
(item): item is typeof item & { type: 'slice' } =>
item.type === 'slice' && item.id === sliceId,
);
if (sliceConfig) {
sliceConfigMap.set(sliceId, sliceConfig);
}
});
const entries: Record<SlotId, unknown> = {};
await Promise.all([
(async () => {
if (!pathConfigItem.rootElement.isStatic) {
entries[ROOT_SLOT_ID] = pathConfigItem.rootElement.renderer(option);
} else if (!skipIdSet.has(ROOT_SLOT_ID)) {
const cached = getCachedElement(ROOT_SLOT_ID);
entries[ROOT_SLOT_ID] = cached
? await cached
: await setCachedElement(
ROOT_SLOT_ID,
pathConfigItem.rootElement.renderer(option),
);
}
})(),
(async () => {
if (!pathConfigItem.routeElement.isStatic) {
entries[routeId] = pathConfigItem.routeElement.renderer(option);
} else if (!skipIdSet.has(routeId)) {
const cached = getCachedElement(routeId);
entries[routeId] = cached
? await cached
: await setCachedElement(
routeId,
pathConfigItem.routeElement.renderer(option),
);
}
})(),
...Object.entries(pathConfigItem.elements).map(
async ([id, { isStatic }]) => {
const renderer = pathConfigItem.elements[id]?.renderer;
if (!isStatic) {
entries[id] = renderer?.(option);
} else if (!skipIdSet.has(id)) {
const cached = getCachedElement(id);
entries[id] = cached
? await cached
: await setCachedElement(id, renderer?.(option));
}
},
),
...slices.map(async (sliceId) => {
const id = SLICE_SLOT_ID_PREFIX + sliceId;
const sliceConfig = sliceConfigMap.get(sliceId);
if (!sliceConfig) {
throw new Error(`Slice not found: ${sliceId}`);
}
if (sliceConfig.isStatic && skipIdSet.has(id)) {
return null;
}
const sliceElement = await getSliceElement(
sliceConfig,
getCachedElement,
setCachedElement,
);
entries[id] = sliceElement;
}),
]);
entries[ROUTE_ID] = [pathname, query];
entries[IS_STATIC_ID] = pathConfigItem.isStatic;
sliceConfigMap.forEach((sliceConfig, sliceId) => {
if (sliceConfig.isStatic) {
// FIXME: hard-coded for now
entries[IS_STATIC_ID + ':' + SLICE_SLOT_ID_PREFIX + sliceId] = true;
}
});
if (myConfig.has404) {
entries[HAS404_ID] = true;
}
return entries;
};
type HandleRequest = Parameters<typeof defineHandlers>[0]['handleRequest'];
type HandleBuild = Parameters<typeof defineHandlers>[0]['handleBuild'];
const cachedElementsForRequest = new Map<SlotId, Promise<ReactNode>>();
let cachedElementsForRequestInitialized = false;
let cachedPath2moduleIds: Record<string, string[]> | undefined;
const handleRequest: HandleRequest = async (
input,
{ renderRsc, parseRsc, renderHtml, loadBuildMetadata },
): Promise<ReadableStream | Response | 'fallback' | null | undefined> => {
const getCachedElement = (id: SlotId) => cachedElementsForRequest.get(id);
const setCachedElement = (id: SlotId, element: ReactNode) => {
const cached = cachedElementsForRequest.get(id);
if (cached) {
return cached;
}
const copied = renderRsc({ [id]: element }).then((rscStream) =>
parseRsc(rscStream).then((parsed) => parsed[id]),
) as Promise<ReactNode>;
cachedElementsForRequest.set(id, copied);
return copied;
};
if (!cachedElementsForRequestInitialized) {
cachedElementsForRequestInitialized = true;
const cachedElementsMetadata = await loadBuildMetadata(
'defineRouter:cachedElements',
);
if (cachedElementsMetadata) {
Object.entries(JSON.parse(cachedElementsMetadata)).forEach(
([id, str]) => {
cachedElementsForRequest.set(
id,
parseRsc(base64ToStream(str as string)).then(
(parsed) => parsed[id],
) as Promise<ReactNode>,
);
},
);
}
}
const getPath2moduleIds = async () => {
if (!cachedPath2moduleIds) {
cachedPath2moduleIds = JSON.parse(
(await loadBuildMetadata('defineRouter:path2moduleIds')) || '{}',
);
}
return cachedPath2moduleIds!;
};
const pathConfigItem = await getPathConfigItem(input.pathname, 'runtime');
if (pathConfigItem?.type === 'api') {
const url = new URL(input.req.url);
url.pathname = input.pathname;
const req = new Request(url, input.req);
const params = getPathMapping(pathConfigItem.path, input.pathname) ?? {};
return pathConfigItem.handler(req, { params });
}
const url = new URL(input.req.url);
const headers = Object.fromEntries(input.req.headers.entries());
if (input.type === 'component') {
const sliceId = decodeSliceId(input.rscPath);
if (sliceId !== null) {
// LIMITATION: This is a signle slice request.
// Ideally, we should be able to respond with multiple slices in one request.
const sliceConfig = await getMyConfig('runtime').then((myConfig) =>
myConfig.configs.find(
(item): item is typeof item & { type: 'slice' } =>
item.type === 'slice' && item.id === sliceId,
),
);
if (!sliceConfig) {
return null;
}
const sliceElement = await getSliceElement(
sliceConfig,
getCachedElement,
setCachedElement,
);
return renderRsc({
[SLICE_SLOT_ID_PREFIX + sliceId]: sliceElement,
...(sliceConfig.isStatic
? {
// FIXME: hard-coded for now
[IS_STATIC_ID + ':' + SLICE_SLOT_ID_PREFIX + sliceId]: true,
}
: {}),
});
}
const entries = await getEntriesForRoute(
input.rscPath,
input.rscParams,
headers,
getCachedElement,
setCachedElement,
'runtime',
);
if (!entries) {
return null;
}
return renderRsc(entries);
}
if (input.type === 'function') {
let elementsPromise: Promise<Record<string, unknown>> = Promise.resolve(
{},
);
let rendered = false;
const rerender = (rscPath: string, rscParams?: unknown) => {
if (rendered) {
throw new Error('already rendered');
}
elementsPromise = Promise.all([
elementsPromise,
getEntriesForRoute(
rscPath,
rscParams,
headers,
getCachedElement,
setCachedElement,
'runtime',
),
]).then(([oldElements, newElements]) => {
if (newElements === null) {
console.warn('getEntries returned null');
}
return {
...oldElements,
...newElements,
};
});
};
setRerender(rerender);
try {
const value = await input.fn(...input.args);
return renderRsc({ ...(await elementsPromise), _value: value });
} catch (e) {
const info = getErrorInfo(e);
if (info?.location) {
const rscPath = encodeRoutePath(info.location);
const entries = await getEntriesForRoute(
rscPath,
undefined,
headers,
getCachedElement,
setCachedElement,
'runtime',
);
if (!entries) {
unstable_notFound();
}
return renderRsc(entries);
}
throw e;
} finally {
rendered = true;
}
}
if (input.type === 'action' || input.type === 'custom') {
const renderIt = async (
pathname: string,
query: string,
httpstatus = 200,
) => {
const rscPath = encodeRoutePath(pathname);
const rscParams = new URLSearchParams({ query });
const entries = await getEntriesForRoute(
rscPath,
rscParams,
headers,
getCachedElement,
setCachedElement,
'runtime',
);
if (!entries) {
return null;
}
const path2moduleIds = await getPath2moduleIds();
const html = (
<INTERNAL_ServerRouter
route={{ path: pathname, query, hash: '' }}
httpstatus={httpstatus}
/>
);
const formState =
input.type === 'action' ? await input.fn() : undefined;
const nonce = getNonce();
return renderHtml(await renderRsc(entries), html, {
rscPath,
formState,
status: httpstatus,
...(nonce ? { nonce } : {}),
unstable_extraScriptContent: getRouterPrefetchCode(path2moduleIds),
});
};
const query = url.searchParams.toString();
if (pathConfigItem?.type === 'route' && pathConfigItem.noSsr) {
return 'fallback';
}
try {
if (pathConfigItem) {
return await renderIt(input.pathname, query);
}
} catch (e) {
const info = getErrorInfo(e);
if (info?.status !== 404) {
throw e;
}
}
if ((await getMyConfig('runtime')).has404) {
return renderIt('/404', '', 404);
} else {
return null;
}
}
};
const handleBuild: HandleBuild = async ({
renderRsc,
parseRsc,
renderHtml,
rscPath2pathname,
saveBuildMetadata,
withRequest,
generateFile,
generateDefaultHtml,
}) => {
const myConfig = await getMyConfig('build');
const cachedElementsForBuild = new Map<SlotId, Promise<ReactNode>>();
const serializedCachedElements = new Map<SlotId, string>();
const getCachedElement = (id: SlotId) => cachedElementsForBuild.get(id);
const setCachedElement = async (id: SlotId, element: ReactNode) => {
const cached = cachedElementsForBuild.get(id);
if (cached) {
return cached;
}
const teedStream = renderRsc({ [id]: element }).then((rscStream) =>
rscStream.tee(),
);
const stream1 = teedStream.then(([s1]) => s1);
const stream2 = teedStream.then(([, s2]) => s2);
const copied = stream1.then(
(rscStream) =>
parseRsc(rscStream).then(
(parsed) => parsed[id],
) as Promise<ReactNode>,
);
cachedElementsForBuild.set(id, copied);
serializedCachedElements.set(id, await streamToBase64(await stream2));
return copied;
};
// hard-coded concurrency limit
const { runTask, waitForTasks } = createTaskRunner(500);
// static api
for (const item of myConfig.configs) {
if (item.type !== 'api') {
continue;
}
if (!item.isStatic) {
continue;
}
const pathname = pathSpec2pathname(item.path);
if (!pathname) {
continue;
}
const req = new Request(new URL(pathname, 'http://localhost:3000'));
runTask(async () => {
await withRequest(req, async () => {
const res = await item.handler(req, { params: {} });
await generateFile(pathname, res.body || '');
});
});
}
const path2moduleIds: Record<string, string[]> = {};
const htmlRenderTasks = new Set<() => Promise<void>>();
// static route
for (const item of myConfig.configs) {
if (item.type !== 'route') {
continue;
}
if (!item.isStatic) {
continue;
}
const pathname = pathSpec2pathname(item.path);
if (!pathname) {
continue;
}
const rscPath = encodeRoutePath(pathname);
const req = new Request(new URL(pathname, 'http://localhost:3000'));
runTask(async () => {
await withRequest(req, async () => {
const entries = await getEntriesForRoute(
rscPath,
undefined,
{},
getCachedElement,
setCachedElement,
'build',
);
if (!entries) {
return;
}
for (const id of Object.keys(entries)) {
const cached = getCachedElement(id);
entries[id] = cached ? await cached : entries[id];
}
const moduleIds = new Set<string>();
const stream = await renderRsc(entries, {
unstable_clientModuleCallback: (ids) =>
ids.forEach((id) => moduleIds.add(id)),
});
const [stream1, stream2] = stream.tee();
await generateFile(rscPath2pathname(rscPath), stream1);
path2moduleIds[path2regexp(item.pathPattern || item.path)] =
Array.from(moduleIds);
htmlRenderTasks.add(async () => {
const html = (
<INTERNAL_ServerRouter
route={{ path: pathname, query: '', hash: '' }}
httpstatus={is404(item.path) ? 404 : 200}
/>
);
const res = await renderHtml(stream2, html, {
rscPath,
unstable_extraScriptContent:
getRouterPrefetchCode(path2moduleIds),
});
await generateFile(htmlPath2pathname(pathname), res.body || '');
});
});
});
}
// HACK hopefully there is a better way than this
await waitForTasks();
htmlRenderTasks.forEach(runTask);
// default html
for (const item of myConfig.configs) {
if (item.type !== 'route') {
continue;
}
if (item.noSsr) {
const pathname = pathSpec2pathname(item.path);
if (!pathname) {
throw new Error('Pathname is required for noSsr routes on build');
}
runTask(async () => {
await generateDefaultHtml(htmlPath2pathname(pathname));
});
}
}
// static slice
for (const item of myConfig.configs) {
if (item.type !== 'slice') {
continue;
}
if (!item.isStatic) {
continue;
}
const rscPath = encodeSliceId(item.id);
// dummy req for slice which is not determined at build time
const req = new Request(new URL('http://localhost:3000'));
runTask(async () => {
await withRequest(req, async () => {
const sliceElement = await getSliceElement(
item,
getCachedElement,
setCachedElement,
);
const body = await renderRsc({
[SLICE_SLOT_ID_PREFIX + item.id]: sliceElement,
// FIXME: hard-coded for now
[IS_STATIC_ID + ':' + SLICE_SLOT_ID_PREFIX + item.id]: true,
});
await generateFile(rscPath2pathname(rscPath), body);
});
});
}
await waitForTasks();
// TODO should we save serialized cached elements separately?
await saveBuildMetadata(
'defineRouter:cachedElements',
JSON.stringify(Object.fromEntries(serializedCachedElements)),
);
await saveBuildMetadata(
'defineRouter:path2moduleIds',
JSON.stringify(path2moduleIds),
);
};
return Object.assign(defineHandlers({ handleRequest, handleBuild }), {
unstable_getRouterConfigs: () => getMyConfig('build').then((c) => c.configs),
});
}