From 56e1ab830823e8610765fc9b3c8c73963c1c8002 Mon Sep 17 00:00:00 2001 From: Anton Platonov Date: Tue, 10 Dec 2024 20:44:40 +0200 Subject: [PATCH] fix(file-router): enable file routes + layout + fallback combination (#2989) --- .../src/runtime/RouterConfigurationBuilder.ts | 12 +++---- .../RouterConfigurationBuilder.spec.tsx | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts b/packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts index d89856ffdd..85f1a8f049 100644 --- a/packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts +++ b/packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts @@ -43,8 +43,8 @@ export type RouteTransformer = (opts: RouteTransformerOptions) => RouteObj type RoutesModifier = (routes: RouteList | undefined) => RouteList | undefined; -function createRouteEntry(route: T): readonly [key: string, value: T] { - return [`${route.path ?? ''}-${route.children ? 'n' : 'i'}`, route]; +function createRouteKey(route: T): string { + return `${route.path ?? ''}-${route.children ? 'n' : 'i'}`; } enum RouteHandleFlags { @@ -314,14 +314,14 @@ export class RouterConfigurationBuilder { if (original && added) { // If we have both original and added routes, we have to merge them. const final: Array = []; - const paths = new Set([...original.map(({ path }) => path), ...added.map(({ path }) => path)]); + const pathKeys = new Set([...original.map(createRouteKey), ...added.map(createRouteKey)]); - for (const path of paths) { + for (const pathKey of pathKeys) { // We can have multiple routes with the same path, so we have to // consider all of them. - const originalRoutes = original.filter((r) => r.path === path); + const originalRoutes = original.filter((r) => createRouteKey(r) === pathKey); // We can have only one route with the same path in the added list. - const addedRoutes = added.filter((r) => r.path === path); + const addedRoutes = added.filter((r) => createRouteKey(r) === pathKey); if (addedRoutes.length > 1) { throw new Error('Adding multiple routes with the same path is not allowed'); diff --git a/packages/ts/file-router/test/runtime/RouterConfigurationBuilder.spec.tsx b/packages/ts/file-router/test/runtime/RouterConfigurationBuilder.spec.tsx index aa99823fec..f25d34c6fb 100644 --- a/packages/ts/file-router/test/runtime/RouterConfigurationBuilder.spec.tsx +++ b/packages/ts/file-router/test/runtime/RouterConfigurationBuilder.spec.tsx @@ -178,6 +178,9 @@ describe('RouterBuilder', () => { { path: '/test', element:
Test
, + }, + { + path: '/test', children: [ { path: '/child-test', @@ -880,4 +883,35 @@ describe('RouterBuilder', () => { ]); }); }); + + describe('combinations', () => { + it('should support file routes with server layout and fallback', () => { + const { routes } = new RouterConfigurationBuilder() + .withFileRoutes([ + { + path: '/next-test', + module: { + default: NextTest, + config: { + flowLayout: true, + }, + }, + }, + ]) + .withFallback(Server) + .build(); + + expect(routes).to.be.like([ + { + element: , + handle: { + ignoreFallback: true, + }, + children: [{ path: '/next-test', element: }], + }, + { path: '*', element: }, + { index: true, element: }, + ]); + }); + }); });