Skip to content

Commit

Permalink
chore: saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Oct 24, 2024
1 parent b4f4036 commit a98b73e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 74 deletions.
12 changes: 7 additions & 5 deletions examples/22_define-router/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ export default new_defineRouter({
];
},
renderRoute: async (path, options) => {
const processSkip = <T,>(elements: Record<string, T>) =>
Object.fromEntries(
Object.entries(elements).filter(
([k]) => !options.skip || !options.skip.includes(k),
),
const processSkip = <T,>(elements: Record<string, T>) => {
if (!options.skip) {
return elements;
}
return Object.fromEntries(
Object.entries(elements).filter(([k]) => !options.skip!.includes(k)),
);
};
if (path === '/') {
return processSkip({
'route:/': (
Expand Down
173 changes: 105 additions & 68 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
GetSlugs,
PropsForPages,
} from './create-pages-utils/inferred-path-types.js';
import { Children, Slot } from '../minimal/client.js';

const hasPathSpecPrefix = (prefix: PathSpec, path: PathSpec) => {
for (let i = 0; i < prefix.length; i++) {
Expand Down Expand Up @@ -469,8 +470,10 @@ export const new_createPages = <
) => {
let configured = false;

const routeMap = new Map<string, Record<string, ReactNode>>();

// TODO I think there's room for improvement to refactor these structures
const staticPathSet = new Set<[string, PathSpec]>();
const fixedPathSet = new Set<[string, PathSpec]>();
const dynamicPagePathMap = new Map<
string,
[PathSpec, FunctionComponent<any>]
Expand Down Expand Up @@ -504,6 +507,28 @@ export const new_createPages = <
if (configured) {
throw new Error('createPage no longer available');
}

routeMap.set(page.path, {
[`route:${page.path}`]: (
// layout adding is different at each level
// TODO loop over the path spec to add layout slots
<Slot id="root">
<Slot id={`layout:${page.path}`}>
<Slot id={`page:${page.path}`} />
</Slot>
</Slot>
),
[`page:${page.path}`]: page.component,
root: rootItem ? (
rootItem.component({ children: <Children /> })
) : (
<DefaultRoot>
<Children />
</DefaultRoot>
),
// TODO add layout for path
});

const pathSpec = parsePathWithSlug(page.path);
if (page.unstable_disableSSR) {
noSsrSet.add(pathSpec);
Expand All @@ -522,7 +547,7 @@ export const new_createPages = <
return { numSlugs, numWildcards };
})();
if (page.render === 'static' && numSlugs === 0) {
staticPathSet.add([page.path, pathSpec]);
fixedPathSet.add([page.path, pathSpec]);
const id = joinPath(page.path, 'page').replace(/^\//, '');
registerStaticComponent(id, page.component);
} else if (
Expand Down Expand Up @@ -557,7 +582,7 @@ export const new_createPages = <
break;
}
});
staticPathSet.add([
fixedPathSet.add([
page.path,
pathItems.map((name) => ({ type: 'literal', name })),
]);
Expand Down Expand Up @@ -633,7 +658,7 @@ export const new_createPages = <
components: Record<string, { isStatic: boolean }>;
noSsr: boolean;
}[] = [];
for (const [path, pathSpec] of staticPathSet) {
for (const [path, pathSpec] of fixedPathSet) {
const noSsr = noSsrSet.has(pathSpec);
const isStatic = (() => {
for (const [_, [layoutPathSpec]] of dynamicLayoutPathMap) {
Expand Down Expand Up @@ -671,70 +696,82 @@ export const new_createPages = <
}
return paths;
},
renderRoute: async (id, options) => {
const processSkip = <T>(elements: Record<string, T>) =>
Object.fromEntries(
Object.entries(elements).filter(
([k]) => !options.skip || !options.skip.includes(k),
),
);
renderRoute: async (path, _options) => {
await configure();
if (id === 'root') {
if (rootItem?.render === 'dynamic') {
unstable_setShouldSkip();
} else {
unstable_setShouldSkip([]);
}
return rootItem?.component ?? DefaultRoot;
}
const staticComponent = staticComponentMap.get(id);
if (staticComponent) {
unstable_setShouldSkip([]);
return staticComponent;
}
for (const [_, [pathSpec, Component]] of dynamicPagePathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'page' }],
id,
);
if (mapping) {
if (Object.keys(mapping).length === 0) {
unstable_setShouldSkip();
return Component;
}
const WrappedComponent = (props: Record<string, unknown>) =>
createElement(Component, { ...props, ...mapping });
unstable_setShouldSkip();
return WrappedComponent;
}
}
for (const [_, [pathSpec, Component]] of wildcardPagePathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'page' }],
id,
);
if (mapping) {
const WrappedComponent = (props: Record<string, unknown>) =>
createElement(Component, { ...props, ...mapping });
unstable_setShouldSkip();
return WrappedComponent;
}
}
for (const [_, [pathSpec, Component]] of dynamicLayoutPathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'layout' }],
id,
);
if (mapping) {
if (Object.keys(mapping).length) {
throw new Error('[Bug] layout should not have slugs');
}
unstable_setShouldSkip();
return Component;
}
}
unstable_setShouldSkip([]); // negative cache
return null; // not found
// TODO handle skip
return routeMap.get(path) ?? null;
// return processSkip({
// 'route:/bar': (
// <Slot id="root">
// <Slot id="layout:/">
// <Slot id="page:/bar" />
// </Slot>
// </Slot>
// ),
// root: (
// <Root>
// <Children />
// </Root>
// ),
// 'layout:/': (
// <HomeLayout>
// <Children />
// </HomeLayout>
// ),
// 'page:/bar': <BarPage />,
// });

// if (id === 'root') {
// if (options.skip?.includes('root')) {
// return null;
// }
// return rootItem?.component ?? DefaultRoot;
// }
// const staticComponent = staticComponentMap.get(id);
// if (staticComponent && !options.skip?.includes(id)) {
// return staticComponent;
// }
// for (const [_, [pathSpec, Component]] of dynamicPagePathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'page' }],
// id,
// );

// if (mapping) {
// if (Object.keys(mapping).length === 0) {
// return Component;
// }
// const WrappedComponent = (props: Record<string, unknown>) =>
// createElement(Component, { ...props, ...mapping });
// // unstable_setShouldSkip();
// return WrappedComponent;
// }
// }
// for (const [_, [pathSpec, Component]] of wildcardPagePathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'page' }],
// id,
// );
// if (mapping) {
// const WrappedComponent = (props: Record<string, unknown>) =>
// createElement(Component, { ...props, ...mapping });
// // unstable_setShouldSkip();
// return WrappedComponent;
// }
// }
// for (const [_, [pathSpec, Component]] of dynamicLayoutPathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'layout' }],
// id,
// );
// if (mapping) {
// if (Object.keys(mapping).length) {
// throw new Error('[Bug] layout should not have slugs');
// }
// // unstable_setShouldSkip();
// return Component;
// }
// }
},
});

Expand All @@ -748,4 +785,4 @@ export const new_createPages = <
};

// Questions:
// 1. Should skip be a Set<string>? https://github.com/dai-shi/waku/blob/main/examples/22_define-router/src/entries.tsx#L63
// 1. Should skip be a Set<string>? https://github.com/dai-shi/waku/blob/main/examples/22_define-router/src/entries.tsx#L63
5 changes: 4 additions & 1 deletion packages/waku/src/router/define-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function new_defineRouter(fns: {
query?: string;
skip?: string[];
},
) => Promise<Record<ComponentId, ReactNode>>;
) => Promise<Record<ComponentId, ReactNode> | null>;
}): ReturnType<typeof defineEntries> {
const platformObject = unstable_getPlatformObject();
type MyPathConfig = {
Expand Down Expand Up @@ -379,6 +379,9 @@ export function new_defineRouter(fns: {
pathname,
pathStatus.isStatic ? {} : { query, skip },
);
if (entries === null) {
return null;
}
entries[ROUTE_ID] = [pathname, query];
entries[IS_STATIC_ID] = pathStatus.isStatic;
if (pathStatus.has404) {
Expand Down

0 comments on commit a98b73e

Please sign in to comment.