Skip to content

Commit bb20466

Browse files
committed
fix(nextjs): Support automatic instrumentation for app directory with page extensions (#12858)
1 parent 3549777 commit bb20466

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
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: 9 additions & 10 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

@@ -285,10 +285,9 @@ export function constructWebpackConfigFunction(
285285
}
286286

287287
if (appDirPath) {
288-
const hasGlobalErrorFile = ['global-error.js', 'global-error.jsx', 'global-error.ts', 'global-error.tsx'].some(
289-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
290-
globalErrorFile => fs.existsSync(path.join(appDirPath!, globalErrorFile)),
291-
);
288+
const hasGlobalErrorFile = fs
289+
.readdirSync(appDirPath)
290+
.some(file => file.match(/^global-error(?:\..*)?\.(?:js|ts|jsx|tsx)$/));
292291

293292
if (
294293
!hasGlobalErrorFile &&

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { materializeFinalWebpackConfig } from './testUtils';
1515

1616
const existsSyncSpy = jest.spyOn(fs, 'existsSync');
17+
jest.spyOn(fs, 'readdirSync').mockReturnValue([]);
1718
const lstatSyncSpy = jest.spyOn(fs, 'lstatSync');
1819

1920
type MatcherResult = { pass: boolean; message: () => string };
@@ -96,6 +97,10 @@ describe('webpack loaders', () => {
9697
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.tsx',
9798
expectedWrappingTargetKind: 'page',
9899
},
100+
{
101+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/testPage.custom.tsx',
102+
expectedWrappingTargetKind: 'page',
103+
},
99104
{
100105
resourcePath: './src/pages/testPage.tsx',
101106
expectedWrappingTargetKind: 'page',
@@ -133,6 +138,10 @@ describe('webpack loaders', () => {
133138
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.js',
134139
expectedWrappingTargetKind: 'middleware',
135140
},
141+
{
142+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/middleware.custom.js',
143+
expectedWrappingTargetKind: 'middleware',
144+
},
136145
{
137146
resourcePath: './src/middleware.js',
138147
expectedWrappingTargetKind: 'middleware',
@@ -162,10 +171,26 @@ describe('webpack loaders', () => {
162171
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.js',
163172
expectedWrappingTargetKind: 'api-route',
164173
},
174+
{
175+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/pages/api/nested/testApiRoute.custom.js',
176+
expectedWrappingTargetKind: 'api-route',
177+
},
178+
{
179+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.ts',
180+
expectedWrappingTargetKind: 'route-handler',
181+
},
182+
{
183+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/route.custom.ts',
184+
expectedWrappingTargetKind: 'route-handler',
185+
},
165186
{
166187
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.js',
167188
expectedWrappingTargetKind: 'server-component',
168189
},
190+
{
191+
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/page.custom.js',
192+
expectedWrappingTargetKind: 'server-component',
193+
},
169194
{
170195
resourcePath: '/Users/Maisey/projects/squirrelChasingSimulator/src/app/nested/page.js',
171196
expectedWrappingTargetKind: 'server-component',

0 commit comments

Comments
 (0)