Skip to content
Open
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
6 changes: 5 additions & 1 deletion packages/nextjs/src/config/manifest/createRouteManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export type CreateRouteManifestOptions = {
* By default, route groups are stripped from paths following Next.js convention.
*/
includeRouteGroups?: boolean;
/**
* Base path for the application, if any. This will be prefixed to all routes.
*/
basePath?: string;
};

let manifestCache: RouteManifest | null = null;
Expand Down Expand Up @@ -192,7 +196,7 @@ export function createRouteManifest(options?: CreateRouteManifestOptions): Route
return manifestCache;
}

const { dynamicRoutes, staticRoutes } = scanAppDirectory(targetDir, '', options?.includeRouteGroups);
const { dynamicRoutes, staticRoutes } = scanAppDirectory(targetDir, options?.basePath, options?.includeRouteGroups);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Route Manifest Cache and Base Path Handling

The createRouteManifest cache doesn't invalidate when the basePath option changes, leading to stale manifests with incorrect route prefixes. Also, passing options?.basePath directly to scanAppDirectory can prefix routes with 'undefined/' when basePath is not provided.

Fix in Cursor Fix in Web


const manifest: RouteManifest = {
dynamicRoutes,
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ function getFinalConfigObject(

let routeManifest: RouteManifest | undefined;
if (!userSentryOptions.disableManifestInjection) {
routeManifest = createRouteManifest();
routeManifest = createRouteManifest({
basePath: incomingUserNextConfigObject.basePath,
});
}

setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions, releaseName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// about page
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// API test page
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// root page
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// users id dynamic page
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import path from 'path';
import { describe, expect, test } from 'vitest';
import { createRouteManifest } from '../../../../../src/config/manifest/createRouteManifest';

describe('basePath', () => {
test('should generate routes with base path prefix', () => {
const manifest = createRouteManifest({
basePath: '/my-app',
appDirPath: path.join(__dirname, 'app')
});

expect(manifest).toEqual({
staticRoutes: [
{ path: '/my-app' },
{ path: '/my-app/about' },
{ path: '/my-app/api/test' }
],
dynamicRoutes: [
{
path: '/my-app/users/:id',
regex: '^/my-app/users/([^/]+)$',
paramNames: ['id'],
}
],
});
});

test('should validate dynamic route regex with base path', () => {
const manifest = createRouteManifest({
basePath: '/my-app',
appDirPath: path.join(__dirname, 'app')
});

const dynamicRoute = manifest.dynamicRoutes.find(route => route.path === '/my-app/users/:id');
const regex = new RegExp(dynamicRoute?.regex ?? '');

// Should match valid paths with base path
expect(regex.test('/my-app/users/123')).toBe(true);
expect(regex.test('/my-app/users/john-doe')).toBe(true);

// Should not match paths without base path
expect(regex.test('/users/123')).toBe(false);

// Should not match invalid paths
expect(regex.test('/my-app/users/')).toBe(false);
expect(regex.test('/my-app/users/123/extra')).toBe(false);
expect(regex.test('/my-app/user/123')).toBe(false);
});
});
Loading