Skip to content

Commit 11721ab

Browse files
committed
inject route as global variable
1 parent c2cdf92 commit 11721ab

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const DATA_FETCHING_FUNCTIONS = {
3737

3838
type LoaderOptions = {
3939
projectDir: string;
40+
pagesDir: string;
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, pagesDir } = '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
@@ -172,6 +173,20 @@ export default function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>,
172173

173174
// Fill in template placeholders
174175
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);
175190
for (const { placeholder, alias } of Object.values(DATA_FETCHING_FUNCTIONS)) {
176191
injectedCode = injectedCode.replace(new RegExp(placeholder, 'g'), alias);
177192
}

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 4 deletions
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,
@@ -81,12 +79,15 @@ export function constructWebpackConfigFunction(
8179
};
8280

8381
if (userSentryOptions.experiments?.autoWrapDataFetchers) {
82+
const pagesDir = newConfig.resolve?.alias?.['private-next-pages'] as string;
83+
8484
newConfig.module.rules.push({
85-
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?)`),
8687
use: [
8788
{
8889
loader: path.resolve(__dirname, 'loaders/dataFetchersLoader.js'),
89-
options: { projectDir },
90+
options: { projectDir, pagesDir },
9091
},
9192
],
9293
});

0 commit comments

Comments
 (0)