Skip to content

Commit 6ac67a8

Browse files
committed
simplify page regex and getting of route
1 parent 6025395 commit 6ac67a8

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const DATA_FETCHING_FUNCTIONS = {
3737

3838
type LoaderOptions = {
3939
projectDir: string;
40-
pageRegex: RegExp;
40+
pagesDir: string;
4141
};
4242

4343
/**
@@ -109,7 +109,7 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
109109
}
110110

111111
// We know one or the other will be defined, depending on the version of webpack being used
112-
const { projectDir, pageRegex } = 'getOptions' in this ? this.getOptions() : this.query;
112+
const { projectDir, pagesDir } = 'getOptions' in this ? this.getOptions() : this.query;
113113

114114
// In the following branch we will proxy the user's file. This means we return code (basically an entirely new file)
115115
// that re - exports all the user file's originial export, but with a "sentry-proxy-loader" query in the module
@@ -171,22 +171,26 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
171171
path.relative(projectDir, this.resourcePath),
172172
);
173173

174-
// Get the route name from this page's filepath
175-
let route = this.resourcePath.match(pageRegex)?.[2];
176-
if (!route) {
177-
logger.warn(`Unable to wrap code from page ${this.resourcePath}, because the route regex has no matches.`);
178-
return userCode;
179-
}
180-
181174
// Fill in template placeholders
182175
let injectedCode = modifiedTemplateCode;
176+
const route = path
177+
// Get the path of the file insde of the pages directory
178+
.relative(pagesDir, this.resourcePath)
179+
// Add a slash at the beginning
180+
.replace(/(.*)/, '/$1')
181+
// Pull off the file extension
182+
.replace(/\.(jsx?|tsx?)/, '')
183+
// Any page file named `index` corresponds to root of the directory its in, URL-wise, so turn `/xyz/index` into
184+
// just `/xyz`
185+
.replace(/\/index$/, '')
186+
// In case all of the above have left us with an empty string (which will happen if we're dealing with the
187+
// homepage), sub back in the root route
188+
.replace(/^$/, '/');
189+
injectedCode = injectedCode.replace('__FILEPATH__', route);
183190
for (const { placeholder, alias } of Object.values(DATA_FETCHING_FUNCTIONS)) {
184191
injectedCode = injectedCode.replace(new RegExp(placeholder, 'g'), alias);
185192
}
186193

187-
// Any route ending in '/index' will correspond to the root of that directory, '/'.
188-
route = route.replace(/index$/, '');
189-
injectedCode = injectedCode.replace('__FILEPATH__', route);
190194

191195
return `${modifiedUserCode}\n${injectedCode}`;
192196
}

packages/nextjs/src/config/webpack.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,16 @@ export function constructWebpackConfigFunction(
7878
],
7979
};
8080

81-
// There are a few different things going on in this regex:
82-
// - It is being used here as a matcher for which files to process with the loader, and will be used in the
83-
// loader itself to isolate the route at which the page will be served (represented here by the `/.*` between
84-
// `pages` and `index`)
85-
// - `/src` is optional because the `pages` directory can either be at the root level of the project or in a
86-
// `src` directory
87-
// - `/?index` isn't included in the capturing group for the route because `a/b/c/index.js` will be served at
88-
// `a/b/c/`
89-
// - In `/?index`, the slash is optional because the route `/` will already have had its single slash consumed
90-
// by the `/.*`
91-
const pageRegex = new RegExp(`${escapeStringForRegex(projectDir)}(/src)?/pages(/.*)(/?index)?\\.(jsx?|tsx?)`);
92-
9381
if (userSentryOptions.experiments?.autoWrapDataFetchers) {
82+
const pagesDir = newConfig.resolve?.alias?.['private-next-pages'] as string;
83+
9484
newConfig.module.rules.push({
95-
test: pageRegex,
85+
// Nextjs allows the `pages` folder to optionally live inside a `src` folder
86+
test: new RegExp(`${escapeStringForRegex(projectDir)}(/src)?/pages/.*\\.(jsx?|tsx?)`),
9687
use: [
9788
{
9889
loader: path.resolve(__dirname, 'loaders/dataFetchersLoader.js'),
99-
options: { projectDir, pageRegex },
90+
options: { projectDir, pagesDir },
10091
},
10192
],
10293
});

0 commit comments

Comments
 (0)