Skip to content

Commit

Permalink
fix: render type test issues with new_createPages
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Nov 12, 2024
1 parent ddc6a80 commit 745d701
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
7 changes: 4 additions & 3 deletions e2e/fixtures/render-type/src/Echo.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export function Echo({ echo, timestamp }: { echo: string; timestamp: number }) {
export function Echo(props: any) {
console.log('Echo', props); // FIXME echo is undefined
return (
<div>
<p data-testid="echo">{echo}</p>
<p data-testid="timestamp">{timestamp}</p>
<p data-testid="echo">{props.echo}</p>
<p data-testid="timestamp">{props.timestamp}</p>
</div>
);
}
5 changes: 3 additions & 2 deletions e2e/fixtures/render-type/src/ServerEcho.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Echo } from './Echo.js';

export function ServerEcho({ echo }: { echo: string }) {
return <Echo echo={echo} timestamp={Date.now()} />;
export function ServerEcho(props: any) {
console.log('ServerEcho', props); // FIXME echo is undefined
return <Echo echo={props.echo} timestamp={Date.now()} />;
}
2 changes: 1 addition & 1 deletion e2e/fixtures/render-type/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPages } from 'waku/router/server';
import { new_createPages as createPages } from 'waku/router/server';

import { ServerEcho } from './ServerEcho.js';
import { ClientEcho } from './ClientEcho.js';
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/render-type/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
40 changes: 34 additions & 6 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,22 @@ export const new_createPages = <
let rootItem: RootItem | undefined = undefined;
const noSsrSet = new WeakSet<PathSpec>();

/** helper to find dynamic path when slugs are used */
const getRoutePath: (path: string) => string | undefined = (path) => {
if (staticComponentMap.has(joinPath(path, 'page').slice(1))) {
return path;
}
const allPaths = [
...dynamicPagePathMap.keys(),
...wildcardPagePathMap.keys(),
];
for (const p of allPaths) {
if (new RegExp(path2regexp(parsePathWithSlug(p))).test(path)) {
return p;
}
}
};

const registerStaticComponent = (
id: string,
component: FunctionComponent<any>,
Expand Down Expand Up @@ -644,7 +660,11 @@ export const new_createPages = <
if (segment === '') {
return acc;
}
acc.push(acc[index - 1] + '/' + segment);
if (acc[index - 1] === '/') {
acc.push('/' + segment);
} else {
acc.push(acc[index - 1] + '/' + segment);
}
return acc;
},
['/'],
Expand Down Expand Up @@ -755,29 +775,37 @@ export const new_createPages = <
renderRoute: async (path) => {
await configure();

// path without slugs
const routePath = getRoutePath(path);
if (!routePath) {
throw new Error('Route not found: ' + path);
}

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

const result: Record<string, ReactNode> = {
root: createElement(
rootItem ? rootItem.component : DefaultRoot,
null,
createElement(Children),
),
[`page:${path}`]: createElement(
[`page:${routePath}`]: createElement(
pageComponent,
null,
createElement(Children),
),
};

const layoutPaths = getLayouts(path);
// this is wrong because getLayouts assumes normal path and routePath is a regex
const layoutPaths = getLayouts(routePath);

for (const segment of layoutPaths) {
const layout =
dynamicLayoutPathMap.get(segment)?.[1] ??
staticComponentMap.get(joinPath(segment, 'layout').slice(1)); // feels like a hack
// console.log({ layout });
// always true
if (layout) {
const id = 'layout:' + segment;
Expand All @@ -791,7 +819,7 @@ export const new_createPages = <
component: Slot,
props: { id: `layout:${lPath}` },
})),
{ component: Slot, props: { id: `page:${path}` } },
{ component: Slot, props: { id: `page:${routePath}` } },
];

return {
Expand Down

0 comments on commit 745d701

Please sign in to comment.