Skip to content

Commit 9eae648

Browse files
authored
feat(nextjs): Add option to have TS sentry config files, other small fixes (#3960)
This documents the change in getsentry/sentry-javascript#3847, which allows for TypeScript versions of `sentry.server.config.js` and `sentry.client.config.js`, by adding TypeScript versions of those files to the code blocks. It also includes a few other small fixes: - Also add a Typescript version of the `withSentry` code snippet, both on the manual setup page and the getting started page. - Move the alert about not supporting the `serverless` target into the section about `next.config.js`, which is where it's relevant. - Remove the split between the snippet for `sentry.server.config.js` and `sentry.client.config.js`, as they were the exact same code. Instead, both filenames are listed on the snippet. - Add filenames to the two `next.config.js` snippets which were missing them. - Let the autoformatter do its thing.
1 parent 5636bdc commit 9eae648

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ Once you're set up, the SDK will automatically capture unhandled errors and prom
1818

1919
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:
2020

21-
```javascript
22-
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
23-
import { withSentry } from '@sentry/nextjs';
21+
```javascript {filename:pages/api/*}
22+
import { withSentry } from "@sentry/nextjs";
2423

2524
const handler = async (req, res) => {
26-
res.status(200).json({ name: 'John Doe' })
27-
}
25+
res.status(200).json({ name: "John Doe" });
26+
};
27+
28+
export default withSentry(handler);
29+
```
30+
31+
```typescript {filename:pages/api/*}
32+
import type { NextApiRequest, NextApiResponse } from "next"
33+
import { withSentry } from "@sentry/nextjs";
34+
35+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
36+
res.status(200).json({ name: "John Doe" });
37+
};
2838

2939
export default withSentry(handler);
3040
```

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
Add a button to a frontend component that throws an error:
22

33
```javascript {filename:pages/index.js}
4-
<button type="button" onClick={() => {
4+
<button
5+
type="button"
6+
onClick={() => {
57
throw new Error("Sentry Frontend Error");
6-
}}>
7-
Throw error
8+
}}
9+
>
10+
Throw error
811
</button>
912
```
1013

1114
And throw an error in an API route:
1215

13-
```javascript {filename:pages/api/hello.js}
14-
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
15-
import { withSentry } from '@sentry/nextjs';
16+
```javascript {filename:pages/api/error.js}
17+
import { withSentry } from "@sentry/nextjs";
1618

1719
const handler = async (req, res) => {
18-
throw new Error('API throw error test')
19-
res.status(200).json({ name: 'John Doe' })
20-
}
20+
throw new Error("API throw error test");
21+
res.status(200).json({ name: "John Doe" });
22+
};
2123

2224
export default withSentry(handler);
2325
```
26+
27+
```typescript {filename:pages/api/error.ts}
28+
import type { NextApiRequest, NextApiResponse } from "next"
29+
import { withSentry } from "@sentry/nextjs";
30+
31+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
32+
throw new Error("API throw error test")
33+
res.status(200).json({ name: "John Doe" });
34+
};
35+
36+
export default withSentry(handler);
37+
```
38+
2439
<Note>
2540

2641
Errors triggered from within Browser DevTools are sandboxed, so will not trigger an error handler. Place the snippet directly in your code instead.

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

+23-13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ If you can’t (or prefer not to) run the [configuration step](/platforms/javasc
1010

1111
Create two files in the root directory of your project, `sentry.client.config.js` and `sentry.server.config.js`. In these files, add your initialization code for the client-side SDK and server-side SDK, respectively. We've included some examples below.
1212

13-
For the client configuration:
13+
For each configuration:
1414

15-
```javascript {filename:sentry.client.config.js}
15+
```javascript {filename:sentry.server.config.js/sentry.client.config.js}
1616
import * as Sentry from "@sentry/nextjs";
1717

1818
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
@@ -29,12 +29,11 @@ Sentry.init({
2929
});
3030
```
3131

32-
And the server configuration:
33-
34-
```javascript {filename:sentry.server.config.js}
32+
```typescript {filename:sentry.server.config.ts/sentry.client.config.ts}
3533
import * as Sentry from "@sentry/nextjs";
3634

37-
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
35+
const SENTRY_DSN: string =
36+
process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
3837

3938
Sentry.init({
4039
dsn: SENTRY_DSN || "___PUBLIC_DSN___",
@@ -50,7 +49,7 @@ Sentry.init({
5049

5150
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:
5251

53-
```javascript {filename:pages/api*}
52+
```javascript {filename:pages/api/*}
5453
import { withSentry } from "@sentry/nextjs";
5554

5655
const handler = async (req, res) => {
@@ -60,13 +59,18 @@ const handler = async (req, res) => {
6059
export default withSentry(handler);
6160
```
6261

63-
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`.
62+
```typescript {filename:pages/api/*}
63+
import type { NextApiRequest, NextApiResponse } from "next"
64+
import { withSentry } from "@sentry/nextjs";
6465

65-
<Alert level="warning" title="Serverless Environments">
66+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
67+
res.status(200).json({ name: "John Doe" });
68+
};
6669

67-
`@sentry/nextjs` does not support configurations with the `serverless` target. To use the SDK in serverless environments, switch to using the `experimental-serverless-trace` target, which is [recommended by the Next.js maintainers](https://github.com/vercel/next.js/issues/20487#issuecomment-753884085).
70+
export default withSentry(handler);
71+
```
6872

69-
</Alert>
73+
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`.
7074

7175
## Extend Next.js Configuration
7276

@@ -106,6 +110,12 @@ const SentryWebpackPluginOptions = {
106110
module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions);
107111
```
108112

113+
<Alert level="warning" title="Serverless Environments">
114+
115+
`@sentry/nextjs` does not support configurations with the `serverless` target. To use the SDK in serverless environments, switch to using the `experimental-serverless-trace` target, which is [recommended by the Next.js maintainers](https://github.com/vercel/next.js/issues/20487#issuecomment-753884085).
116+
117+
</Alert>
118+
109119
Make sure to add the Sentry config last; otherwise, the source maps the plugin receives may not be final.
110120

111121
## Configure Source Maps
@@ -116,7 +126,7 @@ To configure the plugin, pass a `SentryWebpackPluginOptions` argument to `withSe
116126

117127
If you want or need to handle source map uploading separately, the plugin can be disabled for either the server or client build process. To do this, add a `sentry` object to `moduleExports` above, and set the relevant options there:
118128

119-
```javascript
129+
```javascript {filename:next.config.js}
120130
const moduleExports = {
121131
sentry: {
122132
disableServerWebpackPlugin: true,
@@ -127,7 +137,7 @@ const moduleExports = {
127137

128138
If you disable the plugin for both server and client builds, it's safe to omit the `SentryWebpackPluginOptions` parameter from your `withSentryConfig` call:
129139

130-
```javascript
140+
```javascript {filename:next.config.js}
131141
module.exports = withSentryConfig(moduleExports);
132142
```
133143

0 commit comments

Comments
 (0)