Skip to content

Commit

Permalink
fix: issue with static path for a page
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Oct 31, 2024
1 parent 354ad0a commit 0f464e8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 81 deletions.
154 changes: 77 additions & 77 deletions examples/21_create-pages/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPages } from 'waku';
import { new_createPages } from 'waku';
import type { PathsForPages } from 'waku/router';

import FooPage from './components/FooPage';
Expand All @@ -9,93 +9,93 @@ import NestedBazPage from './components/NestedBazPage';
import NestedQuxPage from './components/NestedQuxPage';
import Root from './components/Root';

const pages = createPages(async ({ createPage, createLayout, createRoot }) => [
createRoot({
render: 'static',
component: Root,
}),
const pages = new_createPages(
async ({ createPage, createLayout, createRoot }) => [
createRoot({
render: 'static',
component: Root,
}),

createLayout({
render: 'static',
path: '/',
component: HomeLayout,
}),
createLayout({
render: 'static',
path: '/',
component: HomeLayout,
}),

createPage({
render: 'static',
// render: 'dynamic',
path: '/',
component: HomePage,
}),
createPage({
render: 'static',
path: '/',
component: HomePage,
}),

createPage({
render: 'static',
// render: 'dynamic',
path: '/foo',
component: FooPage,
}),
createPage({
render: 'static',
path: '/foo',
component: FooPage,
}),

createPage({
render: 'static',
path: '/bar',
component: BarPage,
}),
createPage({
render: 'static',
path: '/bar',
component: BarPage,
}),

createPage({
render: 'dynamic',
path: '/baz',
// Inline component is also possible.
component: () => <h2>Dynamic: Baz</h2>,
}),
createPage({
render: 'dynamic',
path: '/baz',
// Inline component is also possible.
component: () => <h2>Dynamic: Baz</h2>,
}),

createPage({
render: 'static',
path: '/nested/baz',
component: NestedBazPage,
}),
createPage({
render: 'static',
path: '/nested/baz',
component: NestedBazPage,
}),

createPage({
render: 'static',
path: '/nested/qux',
component: NestedQuxPage,
}),
createPage({
render: 'static',
path: '/nested/qux',
component: NestedQuxPage,
}),

createPage({
render: 'static',
path: '/nested/[id]',
staticPaths: ['foo', 'bar'],
component: ({ id }) => (
<>
<h2>Nested</h2>
<h3>Static: {id}</h3>
</>
),
}),
createPage({
render: 'static',
path: '/nested/[id]',
staticPaths: ['foo', 'bar'],
component: ({ id }) => (
<>
<h2>Nested</h2>
<h3>Static: {id}</h3>
</>
),
}),

createPage({
render: 'dynamic',
path: '/nested/[id]',
component: ({ id }) => (
<>
<h2>Nested</h2>
<h3>Dynamic: {id}</h3>
</>
),
}),
createPage({
render: 'dynamic',
path: '/nested/[id]',
component: ({ id }) => (
<>
<h2>Nested</h2>
<h3>Dynamic: {id}</h3>
</>
),
}),

createPage({
render: 'dynamic',
path: '/any/[...all]',
component: ({ all }) => <h2>Catch-all: {all.join('/')}</h2>,
}),
createPage({
render: 'dynamic',
path: '/any/[...all]',
component: ({ all }) => <h2>Catch-all: {all.join('/')}</h2>,
}),

// Custom Not Found page
createPage({
render: 'static',
path: '/404',
component: () => <h2>Not Found</h2>,
}),
]);
// Custom Not Found page
createPage({
render: 'static',
path: '/404',
component: () => <h2>Not Found</h2>,
}),
],
);

declare module 'waku/router' {
interface RouteConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/main.react-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Link, useRouter_UNSTABLE } from 'waku/router/client';

export { createPages } from 'waku/router/server';
export { createPages, new_createPages } from 'waku/router/server';

export { getEnv } from 'waku/server';
7 changes: 7 additions & 0 deletions packages/waku/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { Link, useRouter_UNSTABLE } from 'waku/router/client';

import type {
createPages as createPagesType,
new_createPages as new_createPagesType,
getEnv as getEnvType,
} from './main.react-server.js';

Expand All @@ -11,6 +12,12 @@ export const createPages: typeof createPagesType = () => {
);
};

export const new_createPages: typeof new_createPagesType = () => {
throw new Error(
'`new_createPagesType` is only available in react-server environment',
);
};

export const getEnv: typeof getEnvType = () => {
throw new Error('`getEnv` is only available in react-server environment');
};
7 changes: 5 additions & 2 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,9 @@ export const new_createPages = <
return elementsCopy;
};

const pageComponent = (staticComponentMap.get(joinPath(path, 'page')) ??
dynamicPagePathMap.get(path)?.[1])!;
const pageComponent = (staticComponentMap.get(
joinPath(path, 'page').slice(1), // feels like a hack
) ?? dynamicPagePathMap.get(path)?.[1])!;

const result: Record<string, ReactNode> = {
root: createElement(
Expand Down Expand Up @@ -816,6 +817,8 @@ export const new_createPages = <
createNestedElements(routeChildren),
);

console.log(result);

return processSkip(result);
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export {
unstable_rerenderRoute,
new_defineRouter,
} from './define-router.js';
export { createPages } from './create-pages.js';
export { createPages, new_createPages } from './create-pages.js';
export { fsRouter } from './fs-router.js';

0 comments on commit 0f464e8

Please sign in to comment.