-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
e6b6435
7123cf5
8cd3228
cbacaa9
7f70aff
23d5c28
a14c513
a9c0762
7404654
f603014
2daad9a
1110fdb
9109494
b6b8748
3db3cce
61e401d
60743c5
dd5cf8c
072d777
e9547ce
f71c3c9
15e4baa
4973585
536472d
ac97a45
2d32c8e
a41b299
3c66220
c11f20f
9d96e00
db74d74
6fa9feb
49831b9
3877c10
fece18c
8ee1012
b17c5cf
81a7d91
d31a5c7
2a31171
2c482be
3a82621
da52d6c
0a6924a
c8796fd
cefdeb5
653289d
14d5261
6c17b01
bd0dda5
2f3355e
a2d4d38
68ad73d
1be7ffe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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. |
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; |
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" | ||
} | ||
} |
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); | ||
} | ||
}); | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we assume that |
||
|
||
scope.addEventProcessor((event, hint) => { | ||
if (hint.originalException === toCapture) { | ||
event.exception.values[0].mechanism = { | ||
handled: false, | ||
type: 'onErrorServer', | ||
}; | ||
} | ||
return event; | ||
}); | ||
|
||
captureException(toCapture); | ||
}); | ||
|
||
await flush(sentryTimeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} |
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. | ||
} |
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. | ||
} |
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, | ||
}, | ||
}; |
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. |
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 | ||
|
||
[](https://www.npmjs.com/package/@sentry/nextjs) | ||
[](https://www.npmjs.com/package/@sentry/nextjs) | ||
[](https://www.npmjs.com/package/@sentry/nextjs) | ||
[](http://getsentry.github.io/sentry-javascript/) | ||
iker-barriocanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 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: [ | ||
// ... | ||
], | ||
}); | ||
``` |
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 | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.