From a98b73ef737410d43ec20ac6e158fd3b03922ce6 Mon Sep 17 00:00:00 2001 From: Tyler <26290074+thegitduck@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:35:42 -0700 Subject: [PATCH] chore: saving progress --- examples/22_define-router/src/entries.tsx | 12 +- packages/waku/src/router/create-pages.ts | 173 +++++++++++++--------- packages/waku/src/router/define-router.ts | 5 +- 3 files changed, 116 insertions(+), 74 deletions(-) diff --git a/examples/22_define-router/src/entries.tsx b/examples/22_define-router/src/entries.tsx index a03f1d799..def30fb5d 100644 --- a/examples/22_define-router/src/entries.tsx +++ b/examples/22_define-router/src/entries.tsx @@ -57,12 +57,14 @@ export default new_defineRouter({ ]; }, renderRoute: async (path, options) => { - const processSkip = (elements: Record) => - Object.fromEntries( - Object.entries(elements).filter( - ([k]) => !options.skip || !options.skip.includes(k), - ), + const processSkip = (elements: Record) => { + if (!options.skip) { + return elements; + } + return Object.fromEntries( + Object.entries(elements).filter(([k]) => !options.skip!.includes(k)), ); + }; if (path === '/') { return processSkip({ 'route:/': ( diff --git a/packages/waku/src/router/create-pages.ts b/packages/waku/src/router/create-pages.ts index 417169cc1..6e3165147 100644 --- a/packages/waku/src/router/create-pages.ts +++ b/packages/waku/src/router/create-pages.ts @@ -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++) { @@ -469,8 +470,10 @@ export const new_createPages = < ) => { let configured = false; + const routeMap = new Map>(); + // 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] @@ -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 + + + + + + ), + [`page:${page.path}`]: page.component, + root: rootItem ? ( + rootItem.component({ children: }) + ) : ( + + + + ), + // TODO add layout for path + }); + const pathSpec = parsePathWithSlug(page.path); if (page.unstable_disableSSR) { noSsrSet.add(pathSpec); @@ -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 ( @@ -557,7 +582,7 @@ export const new_createPages = < break; } }); - staticPathSet.add([ + fixedPathSet.add([ page.path, pathItems.map((name) => ({ type: 'literal', name })), ]); @@ -633,7 +658,7 @@ export const new_createPages = < components: Record; 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) { @@ -671,70 +696,82 @@ export const new_createPages = < } return paths; }, - renderRoute: async (id, options) => { - const processSkip = (elements: Record) => - 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) => - 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) => - 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': ( + // + // + // + // + // + // ), + // root: ( + // + // + // + // ), + // 'layout:/': ( + // + // + // + // ), + // 'page:/bar': , + // }); + + // 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) => + // 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) => + // 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; + // } + // } }, }); @@ -748,4 +785,4 @@ export const new_createPages = < }; // Questions: -// 1. Should skip be a Set? https://github.com/dai-shi/waku/blob/main/examples/22_define-router/src/entries.tsx#L63 +// 1. Should skip be a Set? https://github.com/dai-shi/waku/blob/main/examples/22_define-router/src/entries.tsx#L63 \ No newline at end of file diff --git a/packages/waku/src/router/define-router.ts b/packages/waku/src/router/define-router.ts index 8516f9642..67cf5c0a8 100644 --- a/packages/waku/src/router/define-router.ts +++ b/packages/waku/src/router/define-router.ts @@ -302,7 +302,7 @@ export function new_defineRouter(fns: { query?: string; skip?: string[]; }, - ) => Promise>; + ) => Promise | null>; }): ReturnType { const platformObject = unstable_getPlatformObject(); type MyPathConfig = { @@ -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) {