Skip to content

Commit

Permalink
fix(core): filter out duplicate routes and log warning
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Feb 8, 2022
1 parent 934c18c commit b221e51
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/core/src/node/app/create/resolvePages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export async function resolvePages(
}
}

app.pages = sortPages(Array.from(resolvedPages.values()));
app.pages = filterDuplicateRoutes(
sortPages(Array.from(resolvedPages.values())),
);

// `pagesResolved` hook
for (let i = 0; i < app.plugins.length; i += 1) {
Expand Down Expand Up @@ -179,6 +181,33 @@ export function loadPagesVirtualModule(app: App): string {
)}`;
}

export function filterDuplicateRoutes(pages: ServerPage[]): ServerPage[] {
const seen = new Map();
const filteredPages: ServerPage[] = [];

for (const page of pages) {
if (seen.has(page.route)) {
const routeOwner = seen.get(page.route);

const fileA = `${kleur.bold('Belongs To:')} ${routeOwner.rootPath}\n`;
const fileB = `${kleur.bold('Duplicate (ignored):')} ${page.rootPath}\n`;

logger.warn(
logger.formatWarnMsg(
`Found duplicate route: ${kleur.bold(
page.route,
)}\n\n${fileA}\n${fileB}`,
),
);
} else {
seen.set(page.route, page);
filteredPages.push(page);
}
}

return filteredPages;
}

// Splits route by `/` and retain splitter.
const splitRouteRE = /(.*?\/)/g;
const splitRoute = (route: string) => route.split(splitRouteRE).slice(3);
Expand Down

0 comments on commit b221e51

Please sign in to comment.