Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new create pages to use new defineRouter #963

Merged
merged 7 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/21_create-pages/src/components/NestedBazPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Baz = () => (
<div>
<h2>Nested</h2>
<h3>Baz</h3>
<p>{new Date().toISOString()}</p>
</div>
);

Expand Down
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: 'dynamic',
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
4 changes: 2 additions & 2 deletions examples/21_create-pages/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { StrictMode } from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import { Router } from 'waku/router/client';
import { NewRouter } from 'waku/router/client';

const rootElement = (
<StrictMode>
<Router />
<NewRouter />
</StrictMode>
);

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');
};
8 changes: 5 additions & 3 deletions packages/waku/src/router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,11 @@ export function NewRouter({
if (has404) {
routerData[2] = true;
}
Object.keys(rest).forEach((id) => {
cachedIdSetRef.current.add(id);
});
if (isStatic) {
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
Object.keys(rest).forEach((id) => {
cachedIdSetRef.current.add(id);
});
}
}
})
.catch(() => {});
Expand Down
Loading
Loading