Skip to content

Commit d5f58ef

Browse files
committed
Remove references to manually use withSentry from Next.js docs
1 parent 4f6cc4d commit d5f58ef

File tree

7 files changed

+71
-91
lines changed

7 files changed

+71
-91
lines changed

src/platform-includes/getting-started-config/javascript.nextjs.mdx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,6 @@ To complete your configuration, add <PlatformLink to="/configuration/options/">o
1717

1818
Once you're set up, the SDK will automatically capture unhandled errors and promise rejections, and monitor performance in the client. You can also <PlatformLink to="/usage/">manually capture errors</PlatformLink>.
1919

20-
By default, the SDK sets the environment for events to `process.env.NODE_ENV`, although you can <PlatformLink to="/configuration/environments/">set your own</PlatformLink>.
21-
To capture [Next.js API Route](https://nextjs.org/docs/api-routes/introduction) errors and monitor server performance, you need to wrap your handlers with a Sentry function:
22-
23-
```javascript {filename:pages/api/*}
24-
import { withSentry } from "@sentry/nextjs";
25-
26-
const handler = async (req, res) => {
27-
res.status(200).json({ name: "John Doe" });
28-
};
29-
30-
export default withSentry(handler);
31-
```
32-
33-
```typescript {filename:pages/api/*}
34-
import type { NextApiRequest, NextApiResponse } from "next"
35-
import { withSentry } from "@sentry/nextjs";
36-
37-
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
38-
res.status(200).json({ name: "John Doe" });
39-
};
40-
41-
export default withSentry(handler);
42-
```
43-
4420
By default, the SDK sets the environment for events to `process.env.NODE_ENV`, although you can [set your own](/platforms/javascript/guides/nextjs/configuration/environments/).
4521

4622
To learn how to connect your app to Sentry and deploy it on Vercel, see the [Vercel integration](/product/integrations/deployment/vercel/).

src/platform-includes/getting-started-verify/javascript.nextjs.mdx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@ Add a button to a frontend component that throws an error:
1414
And throw an error in an API route:
1515

1616
```javascript {filename:pages/api/error.js}
17-
import { withSentry } from "@sentry/nextjs";
18-
19-
const handler = async (req, res) => {
17+
export default (req, res) => {
2018
throw new Error("API throw error test");
2119
res.status(200).json({ name: "John Doe" });
2220
};
23-
24-
export default withSentry(handler);
2521
```
2622

2723
```typescript {filename:pages/api/error.ts}
2824
import type { NextApiRequest, NextApiResponse } from "next"
29-
import { withSentry } from "@sentry/nextjs";
3025

31-
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
26+
export default (req: NextApiRequest, res: NextApiResponse) => {
3227
throw new Error("API throw error test")
3328
res.status(200).json({ name: "John Doe" });
3429
};
35-
36-
export default withSentry(handler);
3730
```
3831

3932
<Note>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
`@sentry/nextjs` provides a `BrowserTracing` integration to add automatic instrumentation for monitoring the performance of browser applications, which is enabled by default once you set up tracing in your app. Further, the same `withSentry` wrapper which enables error collection in your API routes also automatically measures their performance.
1+
`@sentry/nextjs` provides a `BrowserTracing` integration to add automatic instrumentation for monitoring the performance of browser applications, which is enabled by default once you set up tracing in your app.
2+
Further, the SDK will automatically enable error collection and performance monitoring in your API routes and [Next.js Data Fetchers](https://nextjs.org/docs/basic-features/data-fetching/overview).

src/platform-includes/performance/connect-services/javascript.mdx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ You'll need to configure your web server [CORS](https://developer.mozilla.org/en
1818

1919
## Pageload
2020

21+
<PlatformSection notSupported={["javascript.nextjs"]}>
22+
2123
For traces that begin in your backend, you can connect the automatically-generated `pageload` transaction on the frontend with the request transaction that serves the page on the backend. Because JavaScript code running in a browser cannot read the response headers of the current page, the trace data must be transmitted in the response itself, specifically in `<meta>` tags in the `<head>` of the HTML sent from your backend.
2224

2325
```html
@@ -35,25 +37,25 @@ For traces that begin in your backend, you can connect the automatically-generat
3537
Remix SDK attaches `sentry-trace` and `baggage` values from your `root` loader. You need to use [`meta`](https://remix.run/docs/en/v1/api/conventions#meta) function to attach the data from your `loader` as `<meta>` tags. The following code snippet shows how to do this:
3638

3739
```typescript {filename: root.tsx}
38-
export const meta: MetaFunction = ({data}) => {
39-
return {
40-
// ...
41-
'sentry-trace': data.sentryTrace,
42-
baggage: data.sentryBaggage,
43-
};
40+
export const meta: MetaFunction = ({ data }) => {
41+
return {
42+
// ...
43+
"sentry-trace": data.sentryTrace,
44+
baggage: data.sentryBaggage,
45+
};
4446
};
4547
```
4648

4749
<Alert level="warning" title="Support">
48-
50+
4951
This feature is available on Sentry Remix SDK version 7.9.0 and above.
5052

5153
</Alert>
5254

5355
</PlatformSection>
5456

55-
5657
The `name` attributes must be the strings `"sentry-trace"` and `"baggage"` and the `content` attributes must be generated by your backend Sentry SDK. For `sentry-trace`, use `span.toSentryTrace()` (or equivalent, depending on the backend platform). This guarantees that a new and unique value will be generated for each request. For `baggage`, use `serializeBaggage(span.getBaggage())`.
58+
5759
<Note>
5860

5961
The `span.toSentryTrace()` was previously called `span.toTraceparent()`, so that's what you may find depending on the SDK and version.
@@ -63,3 +65,13 @@ The `span.toSentryTrace()` was previously called `span.toTraceparent()`, so that
6365
The `span` reference is either the transaction that serves the HTML or any of its child spans. It defines the parent of the `pageload` transaction.
6466

6567
Once the data is included in the `<meta>` tags, our `BrowserTracing` integration will pick them up automatically and link them to the transaction generated on pageload. Note that they will not be linked to automatically-generated `navigation` transactions — that is, those that don't require a full page reload. Each of those will be the result of a different request transaction on the backend and, therefore, should have a unique `trace_id`.
68+
69+
</PlatformSection>
70+
71+
<PlatformSection supported={["javascript.nextjs"]} notSupported={["javascript"]}>
72+
73+
For traces that begin in your backend, the `@sentry/nextjs` SDK will automatically connect pageload navigations in the frontend with the request transaction that serves the page on the backend.
74+
75+
The SDK will return additional props from your configured [Next.js data fetchers](https://nextjs.org/docs/basic-features/data-fetching/overview) that the SDK picks up in to browser to connect the pageload transaction with the backend request.
76+
77+
</PlatformSection>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
The `BrowserTracing` integration creates a new transaction for each page load and navigation event, and creates a child span for every `XMLHttpRequest` or `fetch` request that occurs while those transactions are open. The `withSentry` wrapper creates a transaction for every API request. Learn more about [traces, transactions, and spans](/product/sentry-basics/tracing/distributed-tracing/).
1+
The `BrowserTracing` integration creates a new transaction for each pageload and navigation event, and creates a child span for every `XMLHttpRequest` or `fetch` request that occurs while those transactions are open.
2+
Additionally, the SDK creates transactions for all requests to API routes and [Next.js data fetchers](https://nextjs.org/docs/basic-features/data-fetching/overview).
3+
Learn more about [traces, transactions, and spans](/product/sentry-basics/tracing/distributed-tracing/).

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

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,6 @@ Sentry.init({
4848

4949
You can include your DSN directly in these two files, or provide it in either of two environment variables, `SENTRY_DSN` or `NEXT_PUBLIC_SENTRY_DSN`.
5050

51-
If you want to instrument [Next.js API Routes](https://nextjs.org/docs/api-routes/introduction), which run on serverless, you need to wrap your handler in our `withSentry` wrapper in order to be able to capture crashes:
52-
53-
```javascript {filename:pages/api/*}
54-
import { withSentry } from "@sentry/nextjs";
55-
56-
const handler = async (req, res) => {
57-
res.status(200).json({ name: "John Doe" });
58-
};
59-
60-
export default withSentry(handler);
61-
```
62-
63-
```typescript {filename:pages/api/*}
64-
import type { NextApiRequest, NextApiResponse } from "next"
65-
import { withSentry } from "@sentry/nextjs";
66-
67-
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
68-
res.status(200).json({ name: "John Doe" });
69-
};
70-
71-
export default withSentry(handler);
72-
```
73-
7451
## Create a Custom `_error` Page
7552

7653
In serverless deployment environments, including Vercel, the Next.js server runs in a "minimal" mode to reduce serverless function size. As a result, some of the auto-instrumentation done by `@sentry/nextjs` doesn't run, and therefore certain errors aren't caught. In addition, Next.js includes a custom error boundary which will catch certain errors before they bubble up to our handlers.
@@ -242,21 +219,53 @@ module.exports = withSentryConfig(moduleExports);
242219

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

245-
### Automatically Instrument API Routes and Data Fetching Methods
222+
### Disable Automatic Instrumentation of API Routes and Data Fetching Methods
246223

247224
_(New in version 7.14.0)_
248225

249-
The SDK provides an option to 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, removing the need to manually wrap API routes in `withSentry`:
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`.
250227

251228
```javascript {filename:next.config.js}
252229
const moduleExports = {
253230
sentry: {
254-
autoInstrumentServerFunctions: true,
231+
autoInstrumentServerFunctions: false,
255232
},
256233
};
257234
```
258235

259-
Under the hood, the SDK is using a Webpack loader to wrap all your API route handlers and data fetching methods.
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)
260269

261270
If the automatic instrumentation doesn't work for your use case, API routes can also be wrapped manually using the `withSentry` function:
262271

src/wizard/javascript/nextjs.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,19 @@ npx @sentry/wizard -i nextjs
2121
```
2222

2323
Sentry wizard will automatically patch your application:
24+
2425
- create `sentry.client.config.js` and `sentry.server.config.js` with the default `Sentry.init`.
2526
- create `next.config.js` with the default configuration.
2627
- create `sentry.properties` with configuration for sentry-cli (which is used when automatically uploading source maps).
2728

28-
2929
You can also [configure it manually](https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/).
3030

31-
32-
## Nextjs API routes
33-
While `@sentry/nextjs` will enable Sentry for your nextjs application, files under the `pages/api` require one additional installing step.
34-
35-
Wrap your API handlers with a `withSentry` function to capture [Next.js API route errors](https://nextjs.org/docs/api-routes/introduction):
36-
37-
```javascript
38-
import { withSentry } from '@sentry/nextjs';
39-
40-
const handler = async (req, res) => {
41-
// ...
42-
}
43-
44-
export default withSentry(handler);
45-
```
46-
4731
Configure the Sentry initialization:
4832

4933
```javascript
5034
Sentry.init({
5135
dsn: "___PUBLIC_DSN___",
52-
36+
5337
// Set tracesSampleRate to 1.0 to capture 100%
5438
// of transactions for performance monitoring.
5539
// We recommend adjusting this value in production
@@ -62,10 +46,13 @@ The above configuration has automatic error tracking with source maps for both J
6246
Then create an intentional error, so you can test that everything is working from your development environment. For example, a button whose `onClick` handler throws an error:
6347

6448
```javascript
65-
<button type="button" onClick={() => {
49+
<button
50+
type="button"
51+
onClick={() => {
6652
throw new Error("Sentry Frontend Error");
67-
}}>
68-
Throw error
53+
}}
54+
>
55+
Throw error
6956
</button>
7057
```
7158

0 commit comments

Comments
 (0)