Skip to content

Commit

Permalink
fix(nextjs): Support automatic instrumentation for route handlers and…
Browse files Browse the repository at this point in the history
… middleware with page extensions
  • Loading branch information
ziyadkhalil committed Jul 10, 2024
1 parent 3549777 commit 317caa7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 2 additions & 5 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,9 @@ export function constructWebpackConfigFunction(
);
};

const possibleMiddlewareLocations = ['js', 'jsx', 'ts', 'tsx'].map(middlewareFileEnding => {
return path.join(middlewareLocationFolder, `middleware.${middlewareFileEnding}`);
});
const isMiddlewareResource = (resourcePath: string): boolean => {
const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath(resourcePath);
return possibleMiddlewareLocations.includes(normalizedAbsoluteResourcePath);
return normalizedAbsoluteResourcePath.startsWith(middlewareLocationFolder) && !!normalizedAbsoluteResourcePath.match(/[\\/]middleware.*\.(js|jsx|ts|tsx)$/)
};

const isServerComponentResource = (resourcePath: string): boolean => {
Expand All @@ -172,7 +169,7 @@ export function constructWebpackConfigFunction(
return (
appDirPath !== undefined &&
normalizedAbsoluteResourcePath.startsWith(appDirPath + path.sep) &&
!!normalizedAbsoluteResourcePath.match(/[\\/]route\.(js|jsx|ts|tsx)$/)
!!normalizedAbsoluteResourcePath.match(/[\\/]route.*\.(js|jsx|ts|tsx)$/)
);
};

Expand Down
20 changes: 20 additions & 0 deletions packages/nextjs/test/config/loaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ describe('webpack loaders', () => {
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.tsx',
expectedWrappingTargetKind: 'page',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.custom.tsx',
expectedWrappingTargetKind: 'page',
},
{
resourcePath: './src/pages/testPage.tsx',
expectedWrappingTargetKind: 'page',
Expand Down Expand Up @@ -133,6 +137,10 @@ describe('webpack loaders', () => {
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.js',
expectedWrappingTargetKind: 'middleware',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.custom.js',
expectedWrappingTargetKind: 'middleware',
},
{
resourcePath: './src/middleware.js',
expectedWrappingTargetKind: 'middleware',
Expand Down Expand Up @@ -162,6 +170,18 @@ describe('webpack loaders', () => {
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.js',
expectedWrappingTargetKind: 'api-route',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.custom.js',
expectedWrappingTargetKind: 'api-route',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.ts',
expectedWrappingTargetKind: 'route-handler',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.custom.ts',
expectedWrappingTargetKind: 'route-handler',
},
{
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.js',
expectedWrappingTargetKind: 'server-component',
Expand Down

0 comments on commit 317caa7

Please sign in to comment.