Skip to content

Commit 1e93746

Browse files
committed
inject route as global variable
1 parent fbb44d5 commit 1e93746

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const DATA_FETCHING_FUNCTIONS = {
3737

3838
type LoaderOptions = {
3939
projectDir: string;
40+
pageRegex: RegExp;
4041
};
4142

4243
/**
@@ -108,7 +109,7 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
108109
}
109110

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

113114
// In the following branch we will proxy the user's file. This means we return code (basically an entirely new file)
114115
// that re - exports all the user file's originial export, but with a "sentry-proxy-loader" query in the module
@@ -170,12 +171,23 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
170171
path.relative(projectDir, this.resourcePath),
171172
);
172173

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+
173181
// Fill in template placeholders
174182
let injectedCode = modifiedTemplateCode;
175183
for (const { placeholder, alias } of Object.values(DATA_FETCHING_FUNCTIONS)) {
176184
injectedCode = injectedCode.replace(new RegExp(placeholder, 'g'), alias);
177185
}
178186

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);
190+
179191
return `${modifiedUserCode}\n${injectedCode}`;
180192
}
181193
}

packages/nextjs/src/config/templates/dataFetchersLoaderTemplate.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ declare const __ORIG_GSPROPS__: GetStaticPropsFunction;
1717
// eslint-disable-next-line import/no-extraneous-dependencies, import/no-unresolved
1818
import * as ServerSideSentryNextjsSDK from '@sentry/nextjs';
1919

20+
const PARAMETERIZED_ROUTE = '__FILEPATH__';
21+
2022
export const getServerSideProps =
2123
typeof __ORIG_GSSP__ === 'function'
2224
? ServerSideSentryNextjsSDK.withSentryGetServerSideProps(__ORIG_GSSP__)

packages/nextjs/src/config/webpack.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export function constructWebpackConfigFunction(
5656
newConfig = userNextConfig.webpack(newConfig, buildContext);
5757
}
5858

59-
const pageRegex = new RegExp(`${escapeStringForRegex(projectDir)}(/src)?/pages(/.+)\\.(jsx?|tsx?)`);
60-
6159
if (isServer) {
6260
newConfig.module = {
6361
...newConfig.module,
@@ -80,13 +78,25 @@ export function constructWebpackConfigFunction(
8078
],
8179
};
8280

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+
8393
if (userSentryOptions.experiments?.autoWrapDataFetchers) {
8494
newConfig.module.rules.push({
8595
test: pageRegex,
8696
use: [
8797
{
8898
loader: path.resolve(__dirname, 'loaders/dataFetchersLoader.js'),
89-
options: { projectDir },
99+
options: { projectDir, pageRegex },
90100
},
91101
],
92102
});

0 commit comments

Comments
 (0)