Skip to content

Commit 295e85b

Browse files
committed
fix(nextjs): Support automatic instrumentation for route handlers and middleware with page extensions (#12858)
1 parent 3549777 commit 295e85b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,9 @@ export function constructWebpackConfigFunction(
147147
);
148148
};
149149

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

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

packages/nextjs/test/config/loaders.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ describe('webpack loaders', () => {
9696
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.tsx',
9797
expectedWrappingTargetKind: 'page',
9898
},
99+
{
100+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.custom.tsx',
101+
expectedWrappingTargetKind: 'page',
102+
},
99103
{
100104
resourcePath: './src/pages/testPage.tsx',
101105
expectedWrappingTargetKind: 'page',
@@ -133,6 +137,10 @@ describe('webpack loaders', () => {
133137
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.js',
134138
expectedWrappingTargetKind: 'middleware',
135139
},
140+
{
141+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.custom.js',
142+
expectedWrappingTargetKind: 'middleware',
143+
},
136144
{
137145
resourcePath: './src/middleware.js',
138146
expectedWrappingTargetKind: 'middleware',
@@ -162,6 +170,18 @@ describe('webpack loaders', () => {
162170
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.js',
163171
expectedWrappingTargetKind: 'api-route',
164172
},
173+
{
174+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.custom.js',
175+
expectedWrappingTargetKind: 'api-route',
176+
},
177+
{
178+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.ts',
179+
expectedWrappingTargetKind: 'route-handler',
180+
},
181+
{
182+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.custom.ts',
183+
expectedWrappingTargetKind: 'route-handler',
184+
},
165185
{
166186
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.js',
167187
expectedWrappingTargetKind: 'server-component',

0 commit comments

Comments
 (0)