Skip to content

Commit 74af05f

Browse files
lobsterkatielforst
authored andcommitted
feat(nextjs): Add excludeServerRoutes option to manual setup page (#5789)
This adds the new `excludeServerRoutes` option in the nextjs SDK (added in getsentry/sentry-javascript#6207) to the docs. It also pulls all of the auto-instrumentation config into its own section in the manual setup page. (Note: The only part of this which is actually new content is the `Opt Out of Auto-instrumentation on Specific Routes` section. Everything else is just stuff moving around.)
1 parent 09f9055 commit 74af05f

File tree

1 file changed

+90
-56
lines changed

1 file changed

+90
-56
lines changed

src/platforms/javascript/guides/nextjs/manual-setup.mdx

+90-56
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,17 @@ const moduleExports = {
156156

157157
// Optional build-time configuration options
158158
sentry: {
159-
// See the 'Configure Source Maps' and 'Configure Legacy Browser Support'
160-
// sections below for information on the following options:
161-
// - disableServerWebpackPlugin
162-
// - disableClientWebpackPlugin
163-
// - autoInstrumentServerFunctions
164-
// - hideSourceMaps
165-
// - widenClientFileUpload
166-
// - transpileClientSDK
159+
// See the sections below for information on the following options:
160+
// 'Configure Source Maps':
161+
// - disableServerWebpackPlugin
162+
// - disableClientWebpackPlugin
163+
// - hideSourceMaps
164+
// - widenClientFileUpload
165+
// 'Configure Legacy Browser Support':
166+
// - transpileClientSDK
167+
// 'Configure Serverside Auto-instrumentation':
168+
// - autoInstrumentServerFunctions
169+
// - excludeServerRoutes
167170
},
168171
};
169172

@@ -219,54 +222,6 @@ module.exports = withSentryConfig(moduleExports);
219222

220223
In that case you can also skip the `sentry-cli` configuration step below.
221224

222-
### Automatic Instrumentation of API Routes and Data Fetching Methods
223-
224-
_(New in version 7.14.0)_
225-
226-
The SDK will automatically instrument API routes and server-side [Next.js data fetching methods](https://nextjs.org/docs/basic-features/data-fetching/overview) with error and performance monitoring To disable it, set the `autoInstrumentServerFunctions` to `false`.
227-
228-
```javascript {filename:next.config.js}
229-
const moduleExports = {
230-
sentry: {
231-
autoInstrumentServerFunctions: false,
232-
},
233-
};
234-
```
235-
236-
Under the hood, when using this option, the SDK is using a Webpack loader to wrap all your API route handlers and data fetching methods.
237-
238-
If the automatic instrumentation doesn't work for your use case, API routes can also be wrapped manually using the `withSentry` function:
239-
240-
```javascript {filename:pages/api/*}
241-
import { withSentry } from "@sentry/nextjs";
242-
243-
const handler = (req, res) => {
244-
res.status(200).json({ name: "John Doe" });
245-
};
246-
247-
export default withSentry(handler);
248-
```
249-
250-
```typescript {filename:pages/api/*}
251-
import type { NextApiRequest, NextApiResponse } from "next"
252-
import { withSentry } from "@sentry/nextjs";
253-
254-
const handler = (req: NextApiRequest, res: NextApiResponse) => {
255-
res.status(200).json({ name: "John Doe" });
256-
};
257-
258-
export default withSentry(handler);
259-
```
260-
261-
Data fetching methods can also be manually wrapped using the following functions:
262-
263-
- `withSentryServerSideGetInitialProps` for `getInitialProps`
264-
- `withSentryGetServerSideProps` for `getServerSideProps`
265-
- `withSentryGetStaticProps` for `getStaticProps`
266-
- `withSentryServerSideErrorGetInitialProps` for `getInitialProps` in [custom Error pages](https://nextjs.org/docs/advanced-features/custom-error-page)
267-
- `withSentryServerSideAppGetInitialProps` for `getInitialProps` in [custom `App` components](https://nextjs.org/docs/advanced-features/custom-app)
268-
- `withSentryServerSideDocumentGetInitialProps` for `getInitialProps` in [custom `Document` components](https://nextjs.org/docs/advanced-features/custom-document)
269-
270225
### Use `hidden-source-map`
271226

272227
_(New in version 6.17.1, will default to `true` in 8.0.0 and beyond.)_
@@ -355,3 +310,82 @@ const moduleExports = {
355310
```
356311

357312
(This assumes you are using [the `next.config.js` setup shown above](#extend-nextjs-configuration).)
313+
314+
## Configure Server-side Auto-instrumentation
315+
316+
The SDK will automatically instrument API routes and server-side [Next.js data fetching methods](https://nextjs.org/docs/basic-features/data-fetching/overview) with error and performance monitoring.
317+
318+
### Disable API Route and Data Fetching Auto-instrumentation Entirely
319+
320+
_(New in version 7.14.0)_
321+
322+
To disable the automatic instrumentation of API route handlers and server-side data fetching functions, set the `autoInstrumentServerFunctions` to `false`.
323+
324+
```javascript {filename:next.config.js}
325+
const moduleExports = {
326+
sentry: {
327+
autoInstrumentServerFunctions: false,
328+
},
329+
};
330+
```
331+
332+
With this option, under the hood, the SDK is using a Webpack loader to wrap all your API route handlers and data fetching methods.
333+
334+
### Opt In to Auto-instrumentation on Specific Routes
335+
336+
_(New in version 7.14.0)_
337+
338+
If the automatic instrumentation doesn't work for your use case, you can turn it off globally and choose to only wrap specific API route handlers or data fetching functions instead.
339+
340+
For API routes, use the `withSentry` function:
341+
342+
```javascript {filename:pages/api/*}
343+
import { withSentry } from "@sentry/nextjs";
344+
345+
const handler = (req, res) => {
346+
res.status(200).json({ name: "John Doe" });
347+
};
348+
349+
export default withSentry(handler);
350+
```
351+
352+
```typescript {filename:pages/api/*}
353+
import type { NextApiRequest, NextApiResponse } from "next"
354+
import { withSentry } from "@sentry/nextjs";
355+
356+
const handler = (req: NextApiRequest, res: NextApiResponse) => {
357+
res.status(200).json({ name: "John Doe" });
358+
};
359+
360+
export default withSentry(handler);
361+
```
362+
363+
For data fetching methods, use the following functions:
364+
365+
- `withSentryServerSideGetInitialProps` for `getInitialProps`
366+
- `withSentryGetServerSideProps` for `getServerSideProps`
367+
- `withSentryGetStaticProps` for `getStaticProps`
368+
- `withSentryServerSideErrorGetInitialProps` for `getInitialProps` in [custom Error pages](https://nextjs.org/docs/advanced-features/custom-error-page)
369+
- `withSentryServerSideAppGetInitialProps` for `getInitialProps` in [custom `App` components](https://nextjs.org/docs/advanced-features/custom-app)
370+
- `withSentryServerSideDocumentGetInitialProps` for `getInitialProps` in [custom `Document` components](https://nextjs.org/docs/advanced-features/custom-document)
371+
372+
### Opt Out of Auto-instrumentation on Specific Routes
373+
374+
_(New in version 7.20.0)_
375+
376+
If you want auto-instrumenatation to apply by default, but want to exclude certain routes, use the `excludeServerRoutes` option in the `sentry` object in your `next.config.js`:
377+
378+
```javascript {filename:next.config.js}
379+
const moduleExports = {
380+
sentry: {
381+
excludeServerRoutes: [
382+
"/some/excluded/route",
383+
"/excluded/route/with/[parameter]",
384+
/^\/route\/beginning\/with\/some\/prefix/,
385+
/\/routeContainingASpecificPathSegment\/?/,
386+
],
387+
},
388+
};
389+
```
390+
391+
Excluded routes can be specified either as regexes or strings. When using a string, make sure that it matches the route exactly, and has a leading slash but no trailing one.

0 commit comments

Comments
 (0)