Skip to content

Commit 4280807

Browse files
authored
fix(create-pages): nested layouts missing (#1063)
fix for #1055 Co-authored-by: Tyler <[email protected]>
1 parent 421fc62 commit 4280807

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

e2e/21_create-pages_standalone.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ async function testRouterExample(page: Page, port: number) {
3333
await page.goto(`http://localhost:${port}/foo`);
3434
await expect(page.getByRole('heading', { name: 'Foo' })).toBeVisible();
3535

36+
await page.goto(`http://localhost:${port}/nested/baz`);
37+
await expect(
38+
page.getByRole('heading', { name: 'Nested Layout' }),
39+
).toBeVisible();
40+
3641
const backgroundColor = await page.evaluate(() =>
3742
window.getComputedStyle(document.body).getPropertyValue('background-color'),
3843
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const NestedLayout = ({ children }: { children: React.ReactNode }) => {
2+
return (
3+
<div>
4+
<h3>Nested Layout</h3>
5+
{children}
6+
</div>
7+
);
8+
};
9+
10+
export default NestedLayout;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const NestedLayout = ({ children }: { children: React.ReactNode }) => {
2+
return (
3+
<div>
4+
<h3>Nested Layout</h3>
5+
{children}
6+
</div>
7+
);
8+
};
9+
10+
export default NestedLayout;

examples/21_create-pages/src/entries.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import BarPage from './components/BarPage';
88
import NestedBazPage from './components/NestedBazPage';
99
import NestedQuxPage from './components/NestedQuxPage';
1010
import Root from './components/Root';
11+
import NestedLayout from './components/NestedLayout';
1112

1213
const pages = createPages(async ({ createPage, createLayout, createRoot }) => [
1314
createRoot({
@@ -58,6 +59,12 @@ const pages = createPages(async ({ createPage, createLayout, createRoot }) => [
5859
component: NestedQuxPage,
5960
}),
6061

62+
createLayout({
63+
render: 'static',
64+
path: '/nested',
65+
component: NestedLayout,
66+
}),
67+
6168
createPage({
6269
render: 'static',
6370
path: '/nested/[id]',

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ export const createPages = <
372372
const getLayouts = (spec: PathSpec): string[] => {
373373
const pathSegments = spec.reduce<string[]>(
374374
(acc, segment, index) => {
375-
if (acc[index - 1] === '/') {
376-
acc.push('/' + segment);
375+
if (index === 0) {
376+
acc.push('/' + segment.name);
377377
} else {
378-
acc.push(acc[index - 1] + '/' + segment);
378+
acc.push(acc[index - 1] + '/' + segment.name);
379379
}
380380
return acc;
381381
},

packages/waku/tests/create-pages.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,50 @@ describe('createPages', () => {
627627
expect(Object.keys(route.elements)).toEqual(['root', 'page:/test/nested']);
628628
});
629629

630+
it('creates a nested static page with nested layout', async () => {
631+
const TestPage = () => null;
632+
createPages(async ({ createPage, createLayout }) => [
633+
createPage({
634+
render: 'static',
635+
path: '/test/nested',
636+
component: TestPage,
637+
}),
638+
createLayout({
639+
render: 'static',
640+
path: '/test/nested',
641+
component: () => null,
642+
}),
643+
]);
644+
const { getPathConfig, renderRoute } = injectedFunctions();
645+
expect(await getPathConfig()).toEqual([
646+
{
647+
elements: {
648+
root: { isStatic: true },
649+
'page:/test/nested': { isStatic: true },
650+
},
651+
routeElement: { isStatic: true },
652+
noSsr: false,
653+
path: [
654+
{
655+
name: 'test',
656+
type: 'literal',
657+
},
658+
{
659+
name: 'nested',
660+
type: 'literal',
661+
},
662+
],
663+
pattern: '^/test/nested$',
664+
},
665+
]);
666+
const route = await renderRoute('/test/nested', {
667+
query: '?skip=[]',
668+
});
669+
expect(route).toBeDefined();
670+
expect(route.routeElement).toBeDefined();
671+
expect(Object.keys(route.elements)).toEqual(['root', 'page:/test/nested']);
672+
});
673+
630674
it('creates a nested dynamic page', async () => {
631675
const TestPage = () => null;
632676
createPages(async ({ createPage }) => [

0 commit comments

Comments
 (0)