Skip to content
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
14 changes: 10 additions & 4 deletions packages/nextjs/src/config/manifest/createRouteManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ function buildRegexForDynamicRoute(routePath: string): { regex: string; paramNam

let pattern: string;
if (hasOptionalCatchall) {
// For optional catchall, make the trailing slash and segments optional
// This allows matching both /catchall and /catchall/anything
const staticParts = regexSegments.join('/');
pattern = `^/${staticParts}(?:/(.*))?$`;
if (regexSegments.length === 0) {
// If the optional catchall happens at the root, accept any path starting
// with a slash. Need capturing group for parameter extraction.
pattern = '^/(.*)$';
} else {
// For optional catchall, make the trailing slash and segments optional
// This allows matching both /catchall and /catchall/anything
const staticParts = regexSegments.join('/');
pattern = `^/${staticParts}(?:/(.*))?$`;
}
} else {
pattern = `^/${regexSegments.join('/')}$`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// beep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Ciao
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from 'path';
import { describe, expect, test } from 'vitest';
import { createRouteManifest } from '../../../../../src/config/manifest/createRouteManifest';

describe('catchall', () => {
const manifest = createRouteManifest({ appDirPath: path.join(__dirname, 'app') });

test('should generate a manifest with catchall route', () => {
expect(manifest).toEqual({
staticRoutes: [{ path: '/' }],
dynamicRoutes: [
{
path: '/:path*?',
regex: '^/(.*)$',
paramNames: ['path'],
},
],
});
});

test('should generate correct pattern for catchall route', () => {
const catchallRoute = manifest.dynamicRoutes.find(route => route.path === '/:path*?');
const regex = new RegExp(catchallRoute?.regex ?? '');
expect(regex.test('/123')).toBe(true);
expect(regex.test('/abc')).toBe(true);
expect(regex.test('/123/456')).toBe(true);
expect(regex.test('/123/abc/789')).toBe(true);
expect(regex.test('/')).toBe(true);
});
});
Loading