Skip to content

Commit 3dbdc45

Browse files
committed
Switch to injecting into API routes and _app for both server and client
1 parent 484aad5 commit 3dbdc45

File tree

3 files changed

+5
-114
lines changed

3 files changed

+5
-114
lines changed

packages/nextjs/src/config/utils.ts

-60
This file was deleted.

packages/nextjs/src/config/webpack.ts

+5-41
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as SentryWebpackPlugin from '@sentry/webpack-plugin';
44

55
import {
66
BuildContext,
7-
EntryPointObject,
87
EntryPointValue,
98
EntryPropertyObject,
109
NextConfigObject,
@@ -13,7 +12,6 @@ import {
1312
WebpackConfigObject,
1413
WebpackEntryProperty,
1514
} from './types';
16-
import { SERVER_SDK_INIT_PATH, storeServerConfigFileLocation } from './utils';
1715

1816
export { SentryWebpackPlugin };
1917

@@ -58,12 +56,6 @@ export function constructWebpackConfigFunction(
5856
const newWebpackFunction = (incomingConfig: WebpackConfigObject, buildContext: BuildContext): WebpackConfigObject => {
5957
let newConfig = { ...incomingConfig };
6058

61-
// if we're building server code, store the webpack output path as an env variable, so we know where to look for the
62-
// webpack-processed version of `sentry.server.config.js` when we need it
63-
if (newConfig.target === 'node') {
64-
storeServerConfigFileLocation(newConfig);
65-
}
66-
6759
// if user has custom webpack config (which always takes the form of a function), run it so we have actual values to
6860
// work with
6961
if ('webpack' in userNextConfig && typeof userNextConfig.webpack === 'function') {
@@ -140,39 +132,11 @@ async function addSentryToEntryProperty(
140132
const newEntryProperty =
141133
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
142134

143-
// Add a new element to the `entry` array, we force webpack to create a bundle out of the user's
144-
// `sentry.server.config.js` file and output it to `SERVER_INIT_LOCATION`. (See
145-
// https://webpack.js.org/guides/code-splitting/#entry-points.) We do this so that the user's config file is run
146-
// through babel (and any other processors through which next runs the rest of the user-provided code - pages, API
147-
// routes, etc.). Specifically, we need any ESM-style `import` code to get transpiled into ES5, so that we can call
148-
// `require()` on the resulting file when we're instrumenting the sesrver. (We can't use a dynamic import there
149-
// because that then forces the user into a particular TS config.)
150-
151-
// On the server, create a separate bundle, as there's no one entry point depended on by all the others
152-
if (buildContext.isServer) {
153-
// slice off the final `.js` since webpack is going to add it back in for us, and we don't want to end up with
154-
// `.js.js` as the extension
155-
newEntryProperty[SERVER_SDK_INIT_PATH.slice(0, -3)] = SERVER_SDK_CONFIG_FILE;
156-
}
157-
// On the client, it's sufficient to inject it into the `main` JS code, which is included in every browser page.
158-
else {
159-
addFileToExistingEntryPoint(newEntryProperty, 'main', CLIENT_SDK_CONFIG_FILE);
160-
161-
// To work around a bug in nextjs, we need to ensure that the `main.js` entry is empty (otherwise it'll choose that
162-
// over `main` and we'll lose the change we just made). In case some other library has put something into it, copy
163-
// its contents over before emptying it out. See
164-
// https://github.com/getsentry/sentry-javascript/pull/3696#issuecomment-863363803.)
165-
const mainjsValue = newEntryProperty['main.js'];
166-
if (Array.isArray(mainjsValue) && mainjsValue.length > 0) {
167-
const mainValue = newEntryProperty.main;
168-
169-
// copy the `main.js` entries over
170-
newEntryProperty.main = Array.isArray(mainValue)
171-
? [...mainjsValue, ...mainValue]
172-
: { ...(mainValue as EntryPointObject), import: [...mainjsValue, ...(mainValue as EntryPointObject).import] };
173-
174-
// nuke the entries
175-
newEntryProperty['main.js'] = [];
135+
const userConfigFile = buildContext.isServer ? SERVER_SDK_CONFIG_FILE : CLIENT_SDK_CONFIG_FILE;
136+
137+
for (const entryPointName in newEntryProperty) {
138+
if (entryPointName === 'pages/_app' || entryPointName.includes('pages/api')) {
139+
addFileToExistingEntryPoint(newEntryProperty, entryPointName, userConfigFile);
176140
}
177141
}
178142

packages/nextjs/src/utils/instrumentServer.ts

-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils'
55
import * as domain from 'domain';
66
import * as http from 'http';
77
import { default as createNextServer } from 'next';
8-
import * as path from 'path';
98
import * as querystring from 'querystring';
109
import * as url from 'url';
1110

@@ -111,18 +110,6 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand
111110
// Otherwise, it's just a pass-through to the original method.
112111
const wrappedHandlerGetter = async function(this: NextServer): Promise<ReqHandler> {
113112
if (!sdkSetupComplete) {
114-
try {
115-
// `SENTRY_SERVER_INIT_PATH` is set at build time, and points to a webpack-processed version of the user's
116-
// `sentry.server.config.js`. Requiring it starts the SDK.
117-
require(path.resolve(process.env.SENTRY_SERVER_INIT_PATH as string));
118-
} catch (err) {
119-
// Log the error but don't bail - we still want the wrapping to happen, in case the user is doing something weird
120-
// and manually calling `Sentry.init()` somewhere else. We log to console instead of using logger from utils
121-
// because Sentry is not initialized.
122-
// eslint-disable-next-line no-console
123-
console.error(`[Sentry] Could not initialize SDK. Received error:\n${err}`);
124-
}
125-
126113
// stash this in the closure so that `makeWrappedReqHandler` can use it
127114
liveServer = this.server;
128115
const serverPrototype = Object.getPrototypeOf(liveServer);

0 commit comments

Comments
 (0)