Skip to content

Commit 1ff20fb

Browse files
authored
fix: lazy slices in static build (#1648)
1 parent eef9e90 commit 1ff20fb

6 files changed

Lines changed: 202 additions & 115 deletions

File tree

e2e/create-pages.spec.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,10 @@ test.describe(`create-pages`, () => {
347347
).toBeVisible();
348348
});
349349

350-
test('slices basic', async ({ page }) => {
351-
await page.goto(`http://localhost:${port}/slices`);
352-
await waitForHydration(page);
353-
const sliceText = await page.getByTestId('slice001').textContent();
354-
expect(sliceText?.startsWith('Slice 001')).toBeTruthy();
355-
const sliceText2 = await page.getByTestId('slice002').textContent();
356-
expect(sliceText2?.startsWith('Slice 002')).toBeTruthy();
357-
});
358-
359-
test('slices with static page', async ({ page }) => {
350+
test('slices with render=dynamic', async ({ page }) => {
360351
await page.goto(`http://localhost:${port}/slices`);
361352
await waitForHydration(page);
353+
// basic test
362354
const staticSliceText = await page.getByTestId('slice001').textContent();
363355
expect(staticSliceText?.startsWith('Slice 001')).toBeTruthy();
364356
const dynamicSliceText = await page.getByTestId('slice002').textContent();
@@ -368,6 +360,7 @@ test.describe(`create-pages`, () => {
368360
await page.waitForTimeout(1000);
369361
await page.getByRole('link', { name: 'Slices' }).click();
370362

363+
// test dynamic and static slices behavior after soft navigation
371364
const staticSliceText2 = await page.getByTestId('slice001').textContent();
372365
expect(staticSliceText2).toBe(staticSliceText);
373366
const dynamicSliceText2 = await page.getByTestId('slice002').textContent();
@@ -420,4 +413,16 @@ test.describe(`create-pages STATIC`, () => {
420413
await page.close();
421414
await context.close();
422415
});
416+
417+
test('slices with render=static', async ({ page }) => {
418+
await page.route(/.*\/RSC\/.*/, async (route) => {
419+
await new Promise((r) => setTimeout(r, 100));
420+
await route.continue();
421+
});
422+
await page.goto(`http://localhost:${port}/static-slices`);
423+
await waitForHydration(page);
424+
await expect(page.getByTestId('slice001-loading')).toBeVisible();
425+
const sliceText = await page.getByTestId('slice001').textContent();
426+
expect(sliceText?.startsWith('Slice 001')).toBeTruthy();
427+
});
423428
});

e2e/fixtures/create-pages/src/server-entry.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ const pages: ReturnType<typeof createPages> = createPages(
326326
),
327327
}),
328328

329+
createPage({
330+
render: 'static',
331+
path: '/static-slices',
332+
component: () => (
333+
<>
334+
<h2>Slices</h2>
335+
<Slice
336+
id="slice001"
337+
lazy
338+
fallback={<p data-testid="slice001-loading">Loading...</p>}
339+
/>
340+
</>
341+
),
342+
}),
343+
329344
createSlice({
330345
render: 'static',
331346
component: Slice001,

e2e/fixtures/define-router/src/server-entry.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ const router: ReturnType<typeof defineRouter> = defineRouter({
6666
],
6767
isStatic: true,
6868
},
69+
{
70+
type: 'slice',
71+
id: 'slice001',
72+
isStatic: true,
73+
},
74+
{
75+
type: 'slice',
76+
id: 'slice002',
77+
isStatic: false,
78+
},
6979
],
7080
handleRoute: async (path) => {
7181
if (!(path in PATH_PAGE)) {
@@ -143,15 +153,6 @@ const router: ReturnType<typeof defineRouter> = defineRouter({
143153
status: 404,
144154
});
145155
},
146-
getSliceConfig: async (sliceId) => {
147-
if (sliceId === 'slice001') {
148-
return { isStatic: true };
149-
}
150-
if (sliceId === 'slice002') {
151-
return { isStatic: false };
152-
}
153-
return null;
154-
},
155156
handleSlice: async (sliceId) => {
156157
if (sliceId === 'slice001') {
157158
return { element: <Slice001 /> };

packages/waku/src/router/create-pages.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,16 @@ export const createPages = <
762762
},
763763
);
764764

765-
return (
766-
[...routeConfigs, ...apiConfigs]
767-
// Sort routes by priority: "standard routes" -> api routes -> api wildcard routes -> standard wildcard routes
768-
.sort((configA, configB) => routePriorityComparator(configA, configB))
769-
);
765+
const sliceConfigs = Array.from(sliceIdMap).map(([id, { isStatic }]) => ({
766+
type: 'slice' as const,
767+
id,
768+
isStatic,
769+
}));
770+
771+
const pathConfigs = [...routeConfigs, ...apiConfigs]
772+
// Sort routes by priority: "standard routes" -> api routes -> api wildcard routes -> standard wildcard routes
773+
.sort((configA, configB) => routePriorityComparator(configA, configB));
774+
return [...pathConfigs, ...sliceConfigs];
770775
},
771776
handleRoute: async (path, { query }) => {
772777
await configure();
@@ -891,15 +896,6 @@ export const createPages = <
891896
}
892897
return handler(req);
893898
},
894-
getSliceConfig: async (sliceId) => {
895-
await configure();
896-
const slice = sliceIdMap.get(sliceId);
897-
if (!slice) {
898-
throw new Error('Slice not found: ' + sliceId);
899-
}
900-
const { isStatic } = slice;
901-
return { isStatic };
902-
},
903899
handleSlice: async (sliceId) => {
904900
await configure();
905901
const slice = sliceIdMap.get(sliceId);

0 commit comments

Comments
 (0)