Skip to content

Commit 80f586e

Browse files
authored
fix(nextjs): Correctly apply auto-instrumentation to pages in src folder (#5984)
1 parent 3a12ba5 commit 80f586e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/nextjs/src/config/webpack.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,22 @@ export function constructWebpackConfigFunction(
8282
if (userSentryOptions.autoInstrumentServerFunctions !== false) {
8383
const pagesDir = newConfig.resolve?.alias?.['private-next-pages'] as string;
8484

85+
// Next.js allows users to have the `pages` folder inside a `src` folder, however "`src/pages` will be ignored
86+
// if `pages` is present in the root directory"
87+
// - https://nextjs.org/docs/advanced-features/src-directory
88+
const shouldIncludeSrcDirectory = !fs.existsSync(path.resolve(projectDir, 'pages'));
89+
8590
// Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
8691
const pageExtensions = userNextConfig.pageExtensions || ['tsx', 'ts', 'jsx', 'js'];
8792
const pageExtensionRegex = pageExtensions.map(escapeStringForRegex).join('|');
8893

8994
newConfig.module.rules.push({
9095
// Nextjs allows the `pages` folder to optionally live inside a `src` folder
91-
test: new RegExp(`${escapeStringForRegex(projectDir)}(/src)?/pages/.*\\.(${pageExtensionRegex})`),
96+
test: new RegExp(
97+
`${escapeStringForRegex(projectDir)}${
98+
shouldIncludeSrcDirectory ? '/src' : ''
99+
}/pages/.*\\.(${pageExtensionRegex})`,
100+
),
92101
use: [
93102
{
94103
loader: path.resolve(__dirname, 'loaders/proxyLoader.js'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const MyNamedPage = () => (
2+
<h1>
3+
This page exists to test the compatibility of our auto-wrapper with the option of having the `pages` directory
4+
inside a `src` directory (https://nextjs.org/docs/advanced-features/src-directory)
5+
</h1>
6+
);

0 commit comments

Comments
 (0)