Skip to content

Commit

Permalink
fix(nextjs): Correctly apply auto-instrumentation to pages in src f…
Browse files Browse the repository at this point in the history
…older (#5984)
  • Loading branch information
lforst committed Oct 18, 2022
1 parent 3a12ba5 commit 80f586e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/nextjs/src/config/webpack.ts
Expand Up @@ -82,13 +82,22 @@ export function constructWebpackConfigFunction(
if (userSentryOptions.autoInstrumentServerFunctions !== false) {
const pagesDir = newConfig.resolve?.alias?.['private-next-pages'] as string;

// Next.js allows users to have the `pages` folder inside a `src` folder, however "`src/pages` will be ignored
// if `pages` is present in the root directory"
// - https://nextjs.org/docs/advanced-features/src-directory
const shouldIncludeSrcDirectory = !fs.existsSync(path.resolve(projectDir, 'pages'));

// Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
const pageExtensions = userNextConfig.pageExtensions || ['tsx', 'ts', 'jsx', 'js'];
const pageExtensionRegex = pageExtensions.map(escapeStringForRegex).join('|');

newConfig.module.rules.push({
// Nextjs allows the `pages` folder to optionally live inside a `src` folder
test: new RegExp(`${escapeStringForRegex(projectDir)}(/src)?/pages/.*\\.(${pageExtensionRegex})`),
test: new RegExp(
`${escapeStringForRegex(projectDir)}${
shouldIncludeSrcDirectory ? '/src' : ''
}/pages/.*\\.(${pageExtensionRegex})`,
),
use: [
{
loader: path.resolve(__dirname, 'loaders/proxyLoader.js'),
Expand Down
@@ -0,0 +1,6 @@
export const MyNamedPage = () => (
<h1>
This page exists to test the compatibility of our auto-wrapper with the option of having the `pages` directory
inside a `src` directory (https://nextjs.org/docs/advanced-features/src-directory)
</h1>
);

0 comments on commit 80f586e

Please sign in to comment.