Skip to content

Commit 2928d9c

Browse files
feat(nextjs): Add Next.js docs (#3319)
1 parent cd643b7 commit 2928d9c

File tree

10 files changed

+200
-2
lines changed

10 files changed

+200
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
```Npx
2+
npx @sentry/wizard -i nextjs
3+
```
4+
5+
<Note>
6+
7+
The call to the [Sentry Wizard](https://github.com/getsentry/sentry-wizard) will automatically patch your project with the necessary configuration, though you can [setup manually](/platforms/javascript/guides/nextjs/manual-setup/) if you prefer.
8+
It will perform the following changes:
9+
10+
- create `sentry.client.config.js` and `sentry.server.config.js` with the default `Sentry.init` (if those files already exist, it will create `_sentry.client.config.js` and `_sentry.server.config.js` instead).
11+
- create `next.config.js` with the default configuration (if it already exists it will create `_next.config.js` instead, which will need to be merged with the existing file manually).
12+
- create `sentry.properties` with Sentry’s project-specific data (used when uploading source maps).
13+
14+
</Note>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```npm
2+
npm install --save @sentry/nextjs
3+
```
4+
5+
```yarn
6+
yarn add @sentry/nextjs
7+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Note>
2+
3+
_Sentry's Next.js SDK enables automatic reporting of errors and exceptions._
4+
5+
</Note>
6+
7+
Features:
8+
9+
- Automatic [Error Tracking](/product/issues/) with source maps for both JavaScript and TypeScript
10+
- Events [enriched](/platforms/javascript/enriching-events/context/) with device data
11+
- [Breadcrumbs](/platforms/javascript/enriching-events/breadcrumbs/) created for outgoing HTTP request with XHR and Fetch, and console logs
12+
- [Release Health](/product/releases/health/) for tracking crash-free users and sessions
13+
14+
Under the hood the SDK relies on our [React SDK](/platforms/javascript/guides/react/) on the frontend and [Node SDK](/platforms/node) on the backend, which makes all features available in those SDKs also available in this SDK.
15+
16+
<Note>
17+
18+
The SDK relies on the experimental plugin system of Next.js. This restricts the SDK to running on the production server (it runs in the client in both dev and production), and means changes to the plugin system may prevent the SDK from working as expected. We recognize the irony.
19+
20+
</Note>

src/platforms/common/enriching-events/transaction-name.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ supported:
99
- native
1010
notSupported:
1111
- javascript.cordova
12+
- javascript.nextjs
1213
description: "Learn how to set or override the transaction name to capture the user and gain critical pieces of information that construct a unique identity in Sentry."
1314
---
1415

src/platforms/common/performance/index.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ supported:
1616
- unity
1717
notSupported:
1818
- javascript.cordova
19+
- javascript.nextjs
1920
description: "Learn how to enable performance monitoring in your app if it is not already set up."
2021
redirect_from:
2122
- /platforms/javascript/performance/apm-to-tracing/
@@ -72,7 +73,7 @@ Test out tracing by adding our <PlatformLink to="/performance/included-instrumen
7273

7374
<PlatformSection supported={["dotnet", "go", "java", "android", "ruby"]} notSupported={["javascript", "react-native", "java.spring", "java.spring-boot"]}>
7475

75-
Test out tracing by starting and finishing a transaction, which you *must* do so transactions can be sent to Sentry. Learn how in our <PlatformLink to="/performance/custom-instrumentation/">Custom Instrumentation</PlatformLink> content.
76+
Test out tracing by starting and finishing a transaction, which you _must_ do so transactions can be sent to Sentry. Learn how in our <PlatformLink to="/performance/custom-instrumentation/">Custom Instrumentation</PlatformLink> content.
7677

7778
</PlatformSection>
7879

src/platforms/javascript/common/sourcemaps/index.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Most modern JavaScript transpilers support source maps. Below you'll find our re
2626
- [UglifyJS](tools/uglifyjs/)
2727
- [SystemJS](tools/systemjs/)
2828

29-
<PlatformSection notSupported={["javascript.angular"]}>
29+
<PlatformSection notSupported={["javascript.angular", "javascript.nextjs"]}>
3030

3131
We recommend using [Sentry's Webpack plugin](https://github.com/getsentry/sentry-webpack-plugin) to configure source maps and upload them automatically during the build:
3232

@@ -134,6 +134,20 @@ From here, you can [upload your source maps](uploading/) to Sentry.
134134

135135
</PlatformSection>
136136

137+
<PlatformSection supported={["javascript.nextjs"]}>
138+
139+
You need to generate an access token for our API. Within your organization's settings, navigate to Developer Settings, [create a new internal integration](/product/integrations/integration-platform/#internal-integrations), and provide a name appropriate to your organization. **Important:** Select **Releases -> Admin** for Permissions.
140+
141+
<Note>
142+
143+
The Releases -> Admin permission is also known as 'project:releases' in other API documentation.
144+
145+
</Note>
146+
147+
You may configure [sentry-cli](/product/cli/configuration/) through its documented mechanisms, or follow the [recommended instructions for webpack](/platforms/javascript/guides/nextjs/sourcemaps/tools/webpack/) for Next.js.
148+
149+
</PlatformSection>
150+
137151
## Additional Resources
138152

139153
- [Using sentry-cli to Upload Source Maps](/product/cli/releases/#sentry-cli-sourcemaps)

src/platforms/javascript/common/sourcemaps/tools/webpack.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Webpack is a powerful build tool that resolves, bundles, and compresses your Jav
77

88
Sentry provides a convenient [Webpack plugin](https://github.com/getsentry/sentry-webpack-plugin) that configures source maps and uploads them to Sentry during the build. This process is the recommended one for uploading sources to Sentry:
99

10+
<PlatformSection notSupported={["javascript.nextjs"]}>
11+
1012
```shell {tabTitle:npm}
1113
npm install --save-dev @sentry/webpack-plugin
1214
```
@@ -15,6 +17,18 @@ npm install --save-dev @sentry/webpack-plugin
1517
yarn add --dev @sentry/webpack-plugin
1618
```
1719

20+
</PlatformSection>
21+
22+
<PlatformSection supported={["javascript.nextjs"]}>
23+
<Note>
24+
25+
If you’ve installed the Next.js SDK, you don’t have to install the plugin.
26+
27+
</Note>
28+
</PlatformSection>
29+
30+
<PlatformSection notSupported={["javascript.nextjs"]}>
31+
1832
You may configure [sentry-cli](/product/cli/configuration/) through it's documented mechanisms, or instead simply bind required parameters when initializing the plugin:
1933

2034
```javascript {filename:webpack.config.js}
@@ -39,6 +53,14 @@ module.exports = {
3953
};
4054
```
4155

56+
</PlatformSection>
57+
58+
<PlatformSection supported={["javascript.nextjs"]}>
59+
60+
You may configure [sentry-cli](/product/cli/configuration/) through its documented mechanisms, or [extend the default usage of webpack](/platforms/javascript/guides/nextjs/manual-setup/manual-config/) of Next.js.
61+
62+
</PlatformSection>
63+
4264
<PlatformSection supported={["javascript.vue"]}>
4365

4466
In Vue 2.x, you should use `vue.config.js` instead of `webpack.config.js`, and use `include: "./dist"` instead of `include: "."`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: Next.js
2+
sdk: sentry.javascript.nextjs
3+
fallbackPlatform: javascript
4+
caseStyle: camelCase
5+
supportLevel: production
6+
categories:
7+
- browser
8+
- server
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Manual Setup
3+
description: "Learn more about manual configuration including webpack usage and managing your app's property file."
4+
---
5+
6+
<PageGrid />
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Manual Configuration
3+
sidebar_order: 1
4+
description: "Learn about manual configuration of the SDK."
5+
---
6+
7+
If you can’t (or prefer not to) run the [configuration step](/platforms/javascript/guides/nextjs/#configure), you can follow the instructions below to configure your application.
8+
9+
## Set Initialization Config Files
10+
11+
Create two files in the root directory of your project, `sentry.client.config.js` and `sentry.server.config.js`, and include in them your initialization code for the client-side SDK and server-side SDK, respectively.
12+
13+
For the client configuration:
14+
15+
```javascript {filename:sentry.client.config.js}
16+
import * as Sentry from "@sentry/nextjs";
17+
18+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
19+
20+
Sentry.init({
21+
dsn: SENTRY_DSN || "___PUBLIC_DSN___",
22+
// ...
23+
// Note: if you want to override the automatic release value, do not set a
24+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
25+
// that it will also get attached to your source maps
26+
});
27+
```
28+
29+
And the server configuration:
30+
31+
```javascript {filename:sentry.server.config.js}
32+
import * as Sentry from "@sentry/nextjs";
33+
34+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
35+
36+
Sentry.init({
37+
dsn: SENTRY_DSN || "___PUBLIC_DSN___",
38+
// ...
39+
// Note: if you want to override the automatic release value, do not set a
40+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
41+
// that it will also get attached to your source maps
42+
});
43+
```
44+
45+
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`.
46+
47+
## Extend Default Webpack Usage for Source Maps
48+
49+
Extend the default Next.js usage of Webpack to generate source maps and upload them to Sentry. Include the following in `next.config.js`:
50+
51+
```javascript {filename:next.config.js}
52+
// This file sets a custom webpack configuration to use your Next.js app
53+
// with Sentry.
54+
// https://nextjs.org/docs/api-reference/next.config.js/introduction
55+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
56+
57+
const { withSentryConfig } = require("@sentry/nextjs");
58+
59+
const moduleExports = {
60+
// your existing module.exports
61+
};
62+
63+
const SentryWebpackPluginOptions = {
64+
// Additional config options for the Sentry Webpack plugin. Keep in mind that
65+
// the following options are set automatically, and overriding them is not
66+
// recommended:
67+
// release, url, org, project, authToken, configFile, stripPrefix,
68+
// urlPrefix, include, ignore
69+
// For all available options, see:
70+
// https://github.com/getsentry/sentry-webpack-plugin#options.
71+
};
72+
73+
// Make sure adding Sentry options is the last code to run before exporting, to
74+
// ensure that your source maps include changes from all other Webpack plugins
75+
module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions);
76+
```
77+
78+
Make sure to add the Sentry config last; otherwise, the source maps the plugin receives may not be final.
79+
80+
## Configure sentry-cli
81+
82+
Under the hood, the `SentryWebpackPlugin` uses `sentry-cli`, which can be configured in one of two ways - using a properties file, or with environment variables - both of which are discussed below. For full details, see [the CLI configuration docs](https://docs.sentry.io/product/cli/configuration/). (If you choose to combine the two approaches, the environment variables will take precedence over values set in the properties file.)
83+
84+
### Properties File
85+
86+
You can create a `sentry.properties` file in the base directory of your project. (This is the approach taken by the wizard.) Here is an example:
87+
88+
```properties {filename:sentry.properties}
89+
defaults.url=https://sentry.io/
90+
defaults.org=___ORG_SLUG___
91+
defaults.project=___PROJECT_SLUG___
92+
auth.token=___TOKEN___
93+
# cli.executable=../path/to/bin/sentry-cli
94+
```
95+
96+
### Environment Variables
97+
98+
Alternatively, the cli can be configured using environment variables.
99+
100+
| Property name | Environment variable |
101+
| ------------------ | -------------------- |
102+
| `defaults.url` | `SENTRY_URL` |
103+
| `defaults.org` | `SENTRY_ORG` |
104+
| `defaults.project` | `SENTRY_PROJECT` |
105+
| `auth.token` | `SENTRY_AUTH_TOKEN` |

0 commit comments

Comments
 (0)