Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(file-router): enable server fallback with client layout and empty client routes (#3280) (CP: 24.6) #3281

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions packages/ts/file-router/src/runtime/RouterConfigurationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ interface RouteBase {
children?: readonly this[];
}

function isReactRouteModule(module?: Module): module is RouteModule<ComponentType> | undefined {
if (!module) {
return true;
}

function isReactRouteModule(module: Module): module is RouteModule<ComponentType> {
return (
('default' in module && typeof module.default === 'function') ||
('config' in module && typeof module.config === 'object')
Expand Down Expand Up @@ -94,7 +90,7 @@ export class RouterConfigurationBuilder {
return this.update(routes, ({ original, overriding: added, children }) => {
if (added) {
const { module, path, flowLayout } = added;
if (!isReactRouteModule(module)) {
if (module && !isReactRouteModule(module)) {
throw new Error(
`The module for the "${path}" section doesn't have the React component exported by default or a ViewConfig object exported as "config"`,
);
Expand Down Expand Up @@ -210,14 +206,14 @@ export class RouterConfigurationBuilder {
if (flag === true) {
lists.server.push({
...route,
children: server.length + ambivalent.length > 0 ? [...server, ...ambivalent] : undefined,
children: route.children ? [...server, ...ambivalent] : undefined,
} as RouteObject);
} else if (server.length > 0) {
// Even if the route doesn't have the flag, it goes to the server
// list if any of the children has the flag enabled.
lists.server.push({
...route,
children: server,
children: route.children ? server : undefined,
} as RouteObject);
}

Expand All @@ -227,7 +223,7 @@ export class RouterConfigurationBuilder {
if (flag === false || client.length > 0) {
lists.client.push({
...route,
children: client.length > 0 ? client : undefined,
children: route.children ? client : undefined,
} as RouteObject);
}

Expand All @@ -240,7 +236,7 @@ export class RouterConfigurationBuilder {
) {
lists.ambivalent.push({
...route,
children: ambivalent.length > 0 ? ambivalent : undefined,
children: route.children ? ambivalent : undefined,
} as RouteObject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe('RouterBuilder', () => {
return <></>;
}

function Layout() {
return <></>;
}

beforeEach(() => {
builder = new RouterConfigurationBuilder().withReactRoutes([
{
Expand Down Expand Up @@ -944,4 +948,34 @@ describe('RouterBuilder', () => {
]);
});
});

it('should support file routes with only client layout and server fallback', () => {
const { routes } = new RouterConfigurationBuilder()
.withFileRoutes([
{
path: '',
module: {
default: Layout,
},
children: [],
},
])
.withFallback(Server)
.build();

expect(routes).to.be.like([
{
path: '',
handle: {
title: 'Layout',
},
children: [
{ path: '*', element: <Server /> },
{ index: true, element: <Server /> },
],
},
{ path: '*', element: <Server /> },
{ index: true, element: <Server /> },
]);
});
});