Skip to content

Commit

Permalink
refactor: define-router (#1090)
Browse files Browse the repository at this point in the history
remove `pattern`
  • Loading branch information
dai-shi authored Dec 25, 2024
1 parent ff0c8c2 commit 80ecd60
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 356 deletions.
14 changes: 3 additions & 11 deletions packages/waku/src/router/create-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
joinPath,
parsePathWithSlug,
getPathMapping,
path2regexp,
pathSpecAsString,
} from '../lib/utils/path.js';
import type { PathSpec } from '../lib/utils/path.js';
Expand Down Expand Up @@ -226,7 +225,7 @@ export const createPages = <
...wildcardPagePathMap.keys(),
];
for (const p of allPaths) {
if (new RegExp(path2regexp(parsePathWithSlug(p))).test(path)) {
if (getPathMapping(parsePathWithSlug(p), path)) {
return p;
}
}
Expand Down Expand Up @@ -398,8 +397,8 @@ export const createPages = <
getPathConfig: async () => {
await configure();
const paths: {
pattern: string;
path: PathSpec;
pathPattern?: PathSpec;
routeElement: { isStatic?: boolean };
elements: Record<string, { isStatic?: boolean }>;
noSsr: boolean;
Expand All @@ -409,10 +408,6 @@ export const createPages = <
for (const [path, { literalSpec, originalSpec }] of staticPathMap) {
const noSsr = noSsrSet.has(literalSpec);

const pattern = originalSpec
? path2regexp(originalSpec)
: path2regexp(literalSpec);

const layoutPaths = getLayouts(originalSpec ?? literalSpec);

const elements = {
Expand All @@ -430,8 +425,8 @@ export const createPages = <
};

paths.push({
pattern,
path: literalSpec,
...(originalSpec && { pathPattern: originalSpec }),
routeElement: {
isStatic: true,
},
Expand All @@ -441,7 +436,6 @@ export const createPages = <
}
for (const [path, [pathSpec]] of dynamicPagePathMap) {
const noSsr = noSsrSet.has(pathSpec);
const pattern = path2regexp(parsePathWithSlug(path));
const layoutPaths = getLayouts(pathSpec);
const elements = {
...layoutPaths.reduce<Record<string, { isStatic: boolean }>>(
Expand All @@ -457,7 +451,6 @@ export const createPages = <
[`page:${path}`]: { isStatic: false },
};
paths.push({
pattern,
path: pathSpec,
routeElement: { isStatic: true },
elements,
Expand All @@ -481,7 +474,6 @@ export const createPages = <
[`page:${path}`]: { isStatic: false },
};
paths.push({
pattern: path2regexp(parsePathWithSlug(path)),
path: pathSpec,
routeElement: { isStatic: true },
elements,
Expand Down
20 changes: 10 additions & 10 deletions packages/waku/src/router/define-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
HAS404_ID,
SKIP_HEADER,
} from './common.js';
import { getPathMapping } from '../lib/utils/path.js';
import { getPathMapping, path2regexp } from '../lib/utils/path.js';
import type { PathSpec } from '../lib/utils/path.js';
import { ServerRouter } from './client.js';
import { getContext } from '../middleware/context.js';
Expand Down Expand Up @@ -64,8 +64,8 @@ const ROUTE_SLOT_ID_PREFIX = 'route:';
export function unstable_defineRouter(fns: {
getPathConfig: () => Promise<
Iterable<{
pattern: string; // TODO we should probably remove this and use path2regexp internally
path: PathSpec;
pathPattern?: PathSpec;
routeElement: { isStatic?: boolean };
elements: Record<SlotId, { isStatic?: boolean }>;
noSsr?: boolean;
Expand All @@ -84,8 +84,8 @@ export function unstable_defineRouter(fns: {
}) {
const platformObject = unstable_getPlatformObject();
type MyPathConfig = {
pathSpec: PathSpec;
pattern: string;
pathname: PathSpec;
staticElementIds: SlotId[];
isStatic?: boolean | undefined;
specs: { noSsr?: boolean; is404: boolean };
Expand All @@ -103,8 +103,8 @@ export function unstable_defineRouter(fns: {
item.path[0]!.type === 'literal' &&
item.path[0]!.name === '404';
return {
pattern: item.pattern,
pathname: item.path,
pathSpec: item.path,
pattern: path2regexp(item.pathPattern || item.path),
staticElementIds: Object.entries(item.elements).flatMap(
([id, { isStatic }]) => (isStatic ? [id] : []),
),
Expand Down Expand Up @@ -134,7 +134,7 @@ export function unstable_defineRouter(fns: {
}
> => {
const pathConfig = await getMyPathConfig();
const found = pathConfig.find(({ pathname: pathSpec }) =>
const found = pathConfig.find(({ pathSpec }) =>
getPathMapping(pathSpec, pathname),
);
const has404 = pathConfig.some(({ specs: { is404 } }) => is404);
Expand All @@ -156,7 +156,7 @@ export function unstable_defineRouter(fns: {
): Promise<string[]> => {
const pathConfig = await getMyPathConfig();
return skip.filter((slotId) => {
const found = pathConfig.find(({ pathname: pathSpec }) =>
const found = pathConfig.find(({ pathSpec }) =>
getPathMapping(pathSpec, pathname),
);
return !!found && found.staticElementIds.includes(slotId);
Expand Down Expand Up @@ -217,7 +217,7 @@ export function unstable_defineRouter(fns: {
const path2moduleIds: Record<string, string[]> = {};

await Promise.all(
pathConfig.map(async ({ pathname: pathSpec, pattern }) => {
pathConfig.map(async ({ pathSpec, pattern }) => {
if (pathSpec.some(({ type }) => type !== 'literal')) {
return;
}
Expand All @@ -243,15 +243,15 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
};`;
type BuildConfig = Awaited<ReturnType<GetBuildConfig>>;
const buildConfig: BuildConfig = [];
for (const { pathname: pathSpec, isStatic, specs } of pathConfig) {
for (const { pathSpec, isStatic, specs } of pathConfig) {
const entries: BuildConfig[number]['entries'] = [];
if (pathSpec.every(({ type }) => type === 'literal')) {
const pathname = '/' + pathSpec.map(({ name }) => name).join('/');
const rscPath = encodeRoutePath(pathname);
entries.push({ rscPath, isStatic });
}
buildConfig.push({
pathSpec: pathSpec,
pathSpec,
isStatic,
entries,
customCode:
Expand Down
Loading

0 comments on commit 80ecd60

Please sign in to comment.