Skip to content

feat: Next.js SDK + Plugin #3301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e6b6435
feat: Add initial files for NextJS SDK
iker-barriocanal Feb 18, 2021
7123cf5
docs: Update README
iker-barriocanal Feb 18, 2021
8cd3228
test: Add sample test
iker-barriocanal Feb 18, 2021
cbacaa9
feat: NextJS client and server sides (#3277)
iker-barriocanal Feb 25, 2021
7f70aff
feat: NextJS initialization config (#3285)
iker-barriocanal Mar 1, 2021
23d5c28
build: Update Sentry dependencies to 6.2.1 (#3296)
iker-barriocanal Mar 2, 2021
a14c513
docs: Add docs for sourcemaps in NextJS (#3295)
iker-barriocanal Mar 3, 2021
a9c0762
docs: Update README with general description and examples (#3302)
iker-barriocanal Mar 3, 2021
7404654
feat: Add next plugin (#3306)
HazAT Mar 3, 2021
f603014
feat: handled false for onServerError
HazAT Mar 3, 2021
2daad9a
ref: Removes frameContextLines
HazAT Mar 4, 2021
1110fdb
ref: Bump wizard
HazAT Mar 4, 2021
9109494
Merge branch 'master' into feat/next-js
HazAT Mar 4, 2021
b6b8748
feat: Remove index and console
HazAT Mar 4, 2021
3db3cce
feat: Export ErrorBoundary
HazAT Mar 5, 2021
61e401d
feat: Add runtime tags to errors (#3327)
iker-barriocanal Mar 24, 2021
60743c5
Merge branch master into feat/next-js
rhcarvalho Apr 2, 2021
dd5cf8c
Merge branch master into feat/next-js
rhcarvalho Apr 2, 2021
072d777
ref: Rename entry points to `index.*`
iker-barriocanal Apr 2, 2021
e9547ce
build: Update Next.js SDK's dependencies
iker-barriocanal Apr 2, 2021
f71c3c9
ref: Remove return type of `init` and make it void
iker-barriocanal Apr 2, 2021
15e4baa
ref: Move RewriteFrames from the plugin to the SDK
iker-barriocanal Apr 2, 2021
4973585
build: Fix Next.js plugin version
iker-barriocanal Apr 2, 2021
536472d
docs: Update README
iker-barriocanal Apr 6, 2021
ac97a45
docs: Fix typo and rename to Next.js
iker-barriocanal Apr 6, 2021
2d32c8e
build: Update version of @sentry/wizard
iker-barriocanal Apr 6, 2021
a41b299
build: Update dependencies of the plugin
iker-barriocanal Apr 6, 2021
3c66220
docs: Update README of the plugin
iker-barriocanal Apr 6, 2021
c11f20f
docs: Add shield badges to SDK's README
iker-barriocanal Apr 6, 2021
9d96e00
docs: Add Next.js SDK as the supported platform
iker-barriocanal Apr 6, 2021
db74d74
ref(nextjs): Remove `initDecider` (#3359)
iker-barriocanal Apr 7, 2021
6fa9feb
feat(nextjs): Add support for integrations on the server-side (#3375)
iker-barriocanal Apr 13, 2021
49831b9
test: Remove unnecessary sample test
iker-barriocanal Apr 13, 2021
3877c10
ref: Remove logs from client and server initializations on the plugin
iker-barriocanal Apr 13, 2021
fece18c
build(nextjs): Remove Wizard (#3399)
iker-barriocanal Apr 16, 2021
8ee1012
build: Bump Next.js package versions, update yarn.lock
iker-barriocanal Apr 16, 2021
b17c5cf
docs: Update link to docs and remove requirement of importing the SDK
iker-barriocanal Apr 16, 2021
81a7d91
fix: Update `.next/` directory regex
iker-barriocanal Apr 16, 2021
d31a5c7
ref: Convert NextjsOptions to union type
iker-barriocanal Apr 16, 2021
2a31171
Merge branch 'master' into feat/next-js
iker-barriocanal Apr 16, 2021
2c482be
Merge branch 'master' into feat/next-js
iker-barriocanal Apr 16, 2021
3a82621
test: Use more specific methods in tests
iker-barriocanal Apr 16, 2021
da52d6c
build: Enable `esModuleInterop` globally
iker-barriocanal Apr 16, 2021
0a6924a
test: Use `instanceOf` instead of `typeof` for functions
iker-barriocanal Apr 19, 2021
c8796fd
meta: Add more descriptive nextjs plugin description
iker-barriocanal Apr 19, 2021
cefdeb5
docs: Improve NOOP message on both init files of the plugin
iker-barriocanal Apr 19, 2021
653289d
Merge branch 'master' into feat/next-js
iker-barriocanal Apr 19, 2021
14d5261
build: Remove esModuleInterop
iker-barriocanal Apr 19, 2021
6c17b01
build: Enable `allowSyntheticDefaultImports`
iker-barriocanal Apr 19, 2021
bd0dda5
fix: Update types file for the SDK
iker-barriocanal Apr 19, 2021
2f3355e
ref(nextjs): Move config code to SDK and fix `next` version bug (#3418)
lobsterkatie Apr 20, 2021
a2d4d38
Update `yarn.lock`
iker-barriocanal Apr 20, 2021
68ad73d
ref: Check for undefined
iker-barriocanal Apr 20, 2021
1be7ffe
Merge branch 'master' into feat/next-js
iker-barriocanal Apr 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ package. Please refer to the README and instructions of those SDKs for more deta
- [`@sentry/ember`](https://github.com/getsentry/sentry-javascript/tree/master/packages/ember): browser SDK with Ember integration enabled
- [`@sentry/vue`](https://github.com/getsentry/sentry-javascript/tree/master/packages/vue): browser SDK with Vue integration enabled
- [`@sentry/gatsby`](https://github.com/getsentry/sentry-javascript/tree/master/packages/gatsby): SDK for Gatsby
- [`@sentry/nextjs`](https://github.com/getsentry/sentry-javascript/tree/master/packages/nextjs): SDK for Next.js
- [`@sentry/react-native`](https://github.com/getsentry/sentry-react-native): SDK for React Native with support for native crashes
- [`@sentry/integrations`](https://github.com/getsentry/sentry-javascript/tree/master/packages/integrations): Pluggable
integrations that can be used to enhance JS SDKs
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"packages/hub",
"packages/integrations",
"packages/minimal",
"packages/nextjs",
"packages/next-plugin-sentry",
"packages/node",
"packages/react",
"packages/serverless",
Expand Down
29 changes: 29 additions & 0 deletions packages/next-plugin-sentry/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
MIT License

Copyright (c) 2021, Sentry
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 changes: 17 additions & 0 deletions packages/next-plugin-sentry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<p align="center">
<a href="https://sentry.io" target="_blank" align="center">
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
</a>
<br />
</p>

# Official Sentry plugin for Next.js

## Links

- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/guides/nextjs/)
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)

## Usage

You don't have to interact with this plugin to use Sentry with your application, see [`@sentry/nextjs`](https://github.com/getsentry/sentry-javascript/tree/master/packages/nextjs) instead.
9 changes: 9 additions & 0 deletions packages/next-plugin-sentry/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const getDsn = () =>
process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

export const getRelease = () =>
process.env.SENTRY_RELEASE ||
process.env.NEXT_PUBLIC_SENTRY_RELEASE ||
process.env.VERCEL_GITHUB_COMMIT_SHA ||
process.env.VERCEL_GITLAB_COMMIT_SHA ||
process.env.VERCEL_BITBUCKET_COMMIT_SHA;
38 changes: 38 additions & 0 deletions packages/next-plugin-sentry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@sentry/next-plugin-sentry",
"version": "6.2.5",
"description": "Sentry plugin for Next.js, part of the @sentry/nextjs SDK",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/next-plugin-sentry",
"author": "Sentry",
"license": "MIT",
"engines": {
"node": ">=6"
},
"main": "index.js",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/nextjs": "6.2.5",
"@sentry/integrations": "6.2.5"
},
"devDependencies": {
"eslint": "7.20.0",
"rimraf": "3.0.2"
},
"nextjs": {
"name": "sentry",
"required-env": []
},
"peerDependencies": {
"next": "*"
},
"scripts": {
"link:yarn": "yarn link",
"pack": "npm pack"
},
"volta": {
"extends": "../../package.json"
}
}
11 changes: 11 additions & 0 deletions packages/next-plugin-sentry/src/on-error-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { withScope, captureException, captureMessage } from '@sentry/nextjs';

export default async function onErrorClient({ err }) {
withScope(scope => {
if (err instanceof Error) {
captureException(err);
} else {
captureMessage(err.message);
}
});
}
36 changes: 36 additions & 0 deletions packages/next-plugin-sentry/src/on-error-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { captureException, flush, Handlers, withScope } from '@sentry/nextjs';
import getConfig from 'next/config';

const { parseRequest } = Handlers;

export default async function onErrorServer(err) {
const { serverRuntimeConfig = {}, publicRuntimeConfig = {} } = getConfig() || {};
const sentryTimeout = serverRuntimeConfig.sentryTimeout || publicRuntimeConfig.sentryTimeout || 2000;

withScope(scope => {
if (err.req) {
scope.addEventProcessor(event => {
return parseRequest(event, err.req, {
// 'cookies' and 'query_string' use `dynamicRequire` which has a bug in SSR envs right now — Kamil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we supposed to do with this comment?

request: ['data', 'headers', 'method', 'url'],
});
});
}

const toCapture = err instanceof Error ? err : err.err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we assume that err.err is a thing if we don't know the type of err?


scope.addEventProcessor((event, hint) => {
if (hint.originalException === toCapture) {
event.exception.values[0].mechanism = {
handled: false,
type: 'onErrorServer',
};
}
return event;
});

captureException(toCapture);
});

await flush(sentryTimeout);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine, but has a caveat: it possibly slows down requests on the server when there are errors, even if it is not necessary.

Flushing on every error is most of the time not required. In certain platforms, in particular serverless platforms, we should flush once per request.

Is there an obvious better place we could put it in? Per-request flush?

}
6 changes: 6 additions & 0 deletions packages/next-plugin-sentry/src/on-init-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import '../../../../sentry.client.config.js';

export default async function initClient() {
// There is no initialization code. The goal of this file is the
// side-effect of importing the`sentry.client.config.js` file.
}
6 changes: 6 additions & 0 deletions packages/next-plugin-sentry/src/on-init-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import '../../../../sentry.server.config.js';

export default async function initServer() {
// There is no initialization code. The goal of this file is the
// side-effect of importing the`sentry.server.config.js` file.
}
33 changes: 33 additions & 0 deletions packages/nextjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
root: true,
env: {
es6: true,
browser: true,
node: true,
},
parserOptions: {
ecmaVersion: 2018,
},
extends: ['@sentry-internal/sdk'],
ignorePatterns: ['build/**', 'dist/**', 'esm/**', 'examples/**', 'scripts/**'],
overrides: [
{
files: ['*.ts', '*.tsx', '*.d.ts'],
parserOptions: {
project: './tsconfig.json',
},
},
{
files: ['test/**'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
},
},
],
rules: {
'max-lines': 'off',
'@sentry-internal/sdk/no-async-await': 'off',
'jsdoc/require-jsdoc': 0,
},
};
29 changes: 29 additions & 0 deletions packages/nextjs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
MIT License

Copyright (c) 2021, Sentry
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 changes: 76 additions & 0 deletions packages/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<p align="center">
<a href="https://sentry.io" target="_blank" align="center">
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
</a>
<br />
</p>

# Official Sentry SDK for Next.js

[![npm version](https://img.shields.io/npm/v/@sentry/nextjs.svg)](https://www.npmjs.com/package/@sentry/nextjs)
[![npm dm](https://img.shields.io/npm/dm/@sentry/nextjs.svg)](https://www.npmjs.com/package/@sentry/nextjs)
[![npm dt](https://img.shields.io/npm/dt/@sentry/nextjs.svg)](https://www.npmjs.com/package/@sentry/nextjs)
[![typedoc](https://img.shields.io/badge/docs-typedoc-blue.svg)](http://getsentry.github.io/sentry-javascript/)

## Links

- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/guides/nextjs/)
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)

## General

This package is a wrapper around `@sentry/node` for the server and `@sentry/react` for the client, with added functionality related to Next.js.

To use this SDK, init it in the Sentry config files.

```javascript
// sentry.client.config.js

import * as Sentry from '@sentry/nextjs';

Sentry.init({
dsn: "__DSN__",
// ...
});
```

```javascript
// sentry.server.config.js

import * as Sentry from '@sentry/nextjs';

Sentry.init({
dsn: "__DSN__",
// ...
});
```

To set context information or send manual events, use the exported functions of `@sentry/nextjs`.

```javascript
import * as Sentry from '@sentry/nextjs';

// Set user information, as well as tags and further extras
Sentry.configureScope(scope => {
scope.setExtra('battery', 0.7);
scope.setTag('user_mode', 'admin');
scope.setUser({ id: '4711' });
// scope.clear();
});

// Add a breadcrumb for future events
Sentry.addBreadcrumb({
message: 'My Breadcrumb',
// ...
});

// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Good bye'));
Sentry.captureEvent({
message: 'Manual',
stacktrace: [
// ...
],
});
```
76 changes: 76 additions & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "@sentry/nextjs",
"version": "6.2.5",
"description": "Official Sentry SDK for Next.js",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/nextjs",
"author": "Sentry",
"license": "MIT",
"engines": {
"node": ">=6"
},
"main": "./dist/index.server.js",
"module": "./esm/index.server.js",
"browser": "./esm/index.client.js",
"types": "./esm/index.client.d.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/core": "6.2.5",
"@sentry/integrations": "6.2.5",
"@sentry/next-plugin-sentry": "6.2.5",
"@sentry/node": "6.2.5",
"@sentry/react": "6.2.5",
"@sentry/utils": "6.2.5",
"@sentry/webpack-plugin": "1.15.0"
},
"devDependencies": {
"@sentry/types": "6.2.5",
"@types/webpack": "^5.28.0",
"eslint": "7.20.0",
"rimraf": "3.0.2"
},
"scripts": {
"build": "run-p build:esm build:es5",
"build:esm": "tsc -p tsconfig.esm.json",
"build:es5": "tsc -p tsconfig.build.json",
"build:watch": "run-p build:watch:esm build:watch:es5",
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
"clean": "rimraf dist esm coverage *.js *.js.map *.d.ts",
"link:yarn": "yarn link",
"lint": "run-s lint:prettier lint:eslint",
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
"fix": "run-s fix:eslint fix:prettier",
"fix:eslint": "eslint . --format stylish --fix",
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
"test": "jest",
"test:watch": "jest --watch",
"pack": "npm pack"
},
"volta": {
"extends": "../../package.json"
},
"jest": {
"collectCoverage": true,
"transform": {
"^.+\\.ts$": "ts-jest"
},
"moduleFileExtensions": [
"js",
"ts"
],
"testEnvironment": "node",
"testMatch": [
"**/*.test.ts"
],
"globals": {
"ts-jest": {
"tsConfig": "./tsconfig.json",
"diagnostics": false
}
}
}
}
Loading