Skip to content

Commit 432ecb0

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

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

packages/nextjs/src/config/loaders/wrappingLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export default function wrappingLoader(
182182

183183
const componentTypeMatch = path.posix
184184
.normalize(path.relative(appDir, this.resourcePath))
185-
.match(/\/?([^/]+)\.(?:js|ts|jsx|tsx)$/);
185+
.match(/\/?([^/.]+)\..*(?:js|ts|jsx|tsx)$/);
186186

187187
if (componentTypeMatch && componentTypeMatch[1]) {
188188
let componentType: ServerComponentContext['componentType'];

packages/nextjs/src/config/webpack.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ 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 (
153+
normalizedAbsoluteResourcePath.startsWith(middlewareLocationFolder) &&
154+
!!normalizedAbsoluteResourcePath.match(/[\\/]middleware(\..*)?\.(js|jsx|ts|tsx)$/)
155+
);
156156
};
157157

158158
const isServerComponentResource = (resourcePath: string): boolean => {
@@ -163,7 +163,7 @@ export function constructWebpackConfigFunction(
163163
return (
164164
appDirPath !== undefined &&
165165
normalizedAbsoluteResourcePath.startsWith(appDirPath + path.sep) &&
166-
!!normalizedAbsoluteResourcePath.match(/[\\/](page|layout|loading|head|not-found)\.(js|jsx|tsx)$/)
166+
!!normalizedAbsoluteResourcePath.match(/[\\/](page|layout|loading|head|not-found)(\..*)?\.(js|jsx|tsx)$/)
167167
);
168168
};
169169

@@ -172,7 +172,7 @@ export function constructWebpackConfigFunction(
172172
return (
173173
appDirPath !== undefined &&
174174
normalizedAbsoluteResourcePath.startsWith(appDirPath + path.sep) &&
175-
!!normalizedAbsoluteResourcePath.match(/[\\/]route\.(js|jsx|ts|tsx)$/)
175+
!!normalizedAbsoluteResourcePath.match(/[\\/]route(\..*)?\.(js|jsx|ts|tsx)$/)
176176
);
177177
};
178178

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

Lines changed: 24 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,10 +170,26 @@ 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',
168188
},
189+
{
190+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.custom.js',
191+
expectedWrappingTargetKind: 'server-component',
192+
},
169193
{
170194
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/page.js',
171195
expectedWrappingTargetKind: 'server-component',

0 commit comments

Comments
 (0)