Skip to content

Commit

Permalink
refactor(file-router): attempt to consider all routes with the same path
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodin committed Dec 9, 2024
1 parent 2f4bf98 commit 3b536b8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 33 deletions.
36 changes: 15 additions & 21 deletions packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export type RouteTransformer<T> = (
children?: RouteList,
) => RouteObject | undefined;

export type RouteListSplittingRule = (route: RouteObject) => boolean;

type RoutesModifier = (routes: RouteList | undefined) => RouteList | undefined;

function createRouteEntry<T extends RouteBase>(route: T): readonly [key: string, value: T] {
Expand Down Expand Up @@ -308,30 +306,26 @@ export class RouterConfigurationBuilder {
null,
([original, added], next) => {
if (original && added) {
const originalMap = new Map(original.map((route) => createRouteEntry(route)));
const addedMap = new Map(added.map((route) => createRouteEntry(route)));

const paths = new Set([...originalMap.keys(), ...addedMap.keys()]);

const final: Array<RouteObject | undefined> = [...original];
const paths = new Set([...original.map(({ path }) => path), ...added.map(({ path }) => path)]);
for (const path of paths) {
const originalRoute = originalMap.get(path);
const addedRoute = addedMap.get(path);

let route: RouteObject | undefined;
if (originalRoute && addedRoute) {
route = callback(originalRoute, addedRoute, next([originalRoute.children, addedRoute.children]));
} else if (originalRoute) {
route = callback(originalRoute, undefined, next([originalRoute.children, undefined]));
const originalRoutes = original.filter((r) => r.path === path);
const addedRoute = added.find((r) => r.path === path);

if (originalRoutes.length > 0 && addedRoute) {
for (const originalRoute of originalRoutes) {
final.push(callback(originalRoute, addedRoute, next([originalRoute.children, addedRoute.children])));
}
} else if (originalRoutes.length > 0) {
for (const originalRoute of originalRoutes) {
final.push(callback(originalRoute, undefined, next([originalRoute.children, undefined])));
}
} else {
route = callback(undefined, addedRoute, next([undefined, addedRoute!.children]));
}

if (route) {
originalMap.set(path, route);
final.push(callback(undefined, addedRoute, next([undefined, addedRoute!.children])));
}
}

return [...originalMap.values()];
return final.filter((r) => r != null);
} else if (original) {
return original
.map((route) => callback(route, undefined, next([route.children, undefined])))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, use } from '@esm-bundle/chai';
import chaiDeepEqualIgnoreUndefined from 'chai-deep-equal-ignore-undefined';
import chaiLike from 'chai-like';
import { createElement } from 'react';
import sinonChai from 'sinon-chai';
Expand All @@ -7,7 +8,7 @@ import { mockDocumentBaseURI } from '../mocks/dom.js';
import { browserRouter, createBrowserRouter } from '../mocks/react-router-dom.js';
import { protectRoute } from '../mocks/vaadin-hilla-react-auth.js';

use(chaiLike);
use(chaiDeepEqualIgnoreUndefined);
use(sinonChai);

describe('RouterBuilder', () => {
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('RouterBuilder', () => {
])
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
path: '',
children: [
Expand Down Expand Up @@ -161,16 +162,13 @@ describe('RouterBuilder', () => {

const serverRoutes = [serverWildcard, serverIndex];

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
path: '',
children: [
{
path: '/test',
element: <div>Test</div>,
},
{
path: '/test',
children: [
{
path: '/child-test',
Expand Down Expand Up @@ -273,7 +271,7 @@ describe('RouterBuilder', () => {
])
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
path: '',
children: [
Expand Down Expand Up @@ -348,7 +346,7 @@ describe('RouterBuilder', () => {
.withLayout(Server)
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
element: createElement(Server),
handle: {
Expand Down Expand Up @@ -407,7 +405,7 @@ describe('RouterBuilder', () => {
.withLayout(Server)
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
path: '',
},
Expand Down Expand Up @@ -508,7 +506,7 @@ describe('RouterBuilder', () => {
.withLayout(Server)
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
element: createElement(Server),
handle: {
Expand Down Expand Up @@ -641,7 +639,7 @@ describe('RouterBuilder', () => {
it('should not throw when no routes', () => {
const { routes } = new RouterConfigurationBuilder().withLayout(Server).build();

expect(routes).to.be.like([]);
expect(routes).to.be.deepEqualIgnoreUndefined([]);
});
});

Expand Down Expand Up @@ -698,7 +696,7 @@ describe('RouterBuilder', () => {
.withLayout(Server)
.build();

expect(routes).to.be.like([
expect(routes).to.be.deepEqualIgnoreUndefined([
{
handle: {
ignoreFallback: true,
Expand Down Expand Up @@ -830,4 +828,90 @@ describe('RouterBuilder', () => {
reset();
});
});

describe('issues', () => {
it('#2954', () => {
const { routes } = new RouterConfigurationBuilder()
.withFileRoutes([
{
path: '',
children: [
{
path: '',
module: {
config: {
menu: { order: 0 },
title: 'Public view',
},
default: NextTest,
},
},
{
path: 'login',
module: {
config: {
menu: { exclude: true },
flowLayout: false,
},
},
},
],
},
])
.withFallback(Server)
.protect()
.build();

expect(routes).to.be.deepEqualIgnoreUndefined([
{
path: '',
handle: {
title: 'undefined',
},
children: [
{
path: 'login',
handle: {
title: 'undefined',
menu: { exclude: true },
flowLayout: false,
},
},
{
path: '*',
element: <Server />,
},
{
index: true,
element: <Index />,
},
],
},
{
path: '',
handle: {
title: 'undefined',
},
children: [
{
path: '',
handle: {
menu: { order: 0 },
title: 'Public view',
element: NextTest,
},
},
{
path: '*',
element: <Server />,
},
{
index: true,
element: <Index />,
},
],
},
]);
});
});
});

0 comments on commit 3b536b8

Please sign in to comment.