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

refactor: fs-router to use new_createPages & fix staticPaths #1003

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion e2e/fixtures/ssg-wildcard/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "waku-example",
"name": "ssg-wildcard",
"version": "0.1.0",
"type": "module",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion examples/11_fs-router/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StrictMode } from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import { Router } from 'waku/router/client';
import { NewRouter as Router } from 'waku/router/client';

const rootElement = (
<StrictMode>
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-managed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default fsRouter(
const getManagedMain = () => `
import { StrictMode } from 'react';
import { createRoot, hydrateRoot } from 'react-dom/client';
import { Router } from 'waku/router/client';
import { NewRouter as Router } from 'waku/router/client';

const rootElement = (
<StrictMode>
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export const new_createPages = <
}
});
staticPathMap.set(
page.path,
'/' + pathItems.join('/'),
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
pathItems.map((name) => ({ type: 'literal', name })),
);
const id = joinPath(...pathItems, 'page');
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unstable_getPlatformObject } from '../server.js';
import { createPages } from './create-pages.js';
import { new_createPages as createPages } from './create-pages.js';

import { EXTENSIONS } from '../lib/constants.js';

Expand Down
50 changes: 50 additions & 0 deletions packages/waku/tests/create-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,56 @@ describe('createPages', () => {
expect(TestPage).toHaveBeenCalledWith({ path: ['a', 'b'] }, undefined);
});

it('creates multiple static pages with wildcards', async () => {
const TestPage = vi.fn();
createPages(async ({ createPage }) => [
createPage({
render: 'static',
path: '/test/[...path]',
staticPaths: [[], ['hi'], ['a', 'b']],
component: TestPage,
}),
]);
const { getPathConfig, getComponent } = injectedFunctions();
expect(await getPathConfig()).toEqual([
{
pattern: '^/test/(.*)$',
path: [{ type: 'literal', name: 'test' }],
isStatic: true,
noSsr: false,
},
{
pattern: '^/test/(.*)$',
path: [
{ type: 'literal', name: 'test' },
{ type: 'literal', name: 'hi' },
],
isStatic: true,
noSsr: false,
},
{
pattern: '^/test/(.*)$',
path: [
{ type: 'literal', name: 'test' },
{ type: 'literal', name: 'a' },
{ type: 'literal', name: 'b' },
],
isStatic: true,
noSsr: false,
},
]);
const setShouldSkip = vi.fn();
const WrappedComponent = await getComponent('test/a/b/page', {
unstable_setShouldSkip: setShouldSkip,
});
assert(WrappedComponent);
expect(setShouldSkip).toHaveBeenCalledTimes(1);
expect(setShouldSkip).toHaveBeenCalledWith([]);
renderToString(createElement(WrappedComponent as any));
expect(TestPage).toHaveBeenCalledTimes(1);
expect(TestPage).toHaveBeenCalledWith({ path: ['a', 'b'] }, undefined);
});

it('creates a dynamic page with wildcards', async () => {
const TestPage = vi.fn();
createPages(async ({ createPage }) => [
Expand Down
Loading