Skip to content

Commit 7404654

Browse files
authored
feat: Add next plugin (#3306)
1 parent a9c0762 commit 7404654

File tree

15 files changed

+210
-5
lines changed

15 files changed

+210
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"packages/integrations",
3131
"packages/minimal",
3232
"packages/nextjs",
33+
"packages/next-plugin-sentry",
3334
"packages/node",
3435
"packages/react",
3536
"packages/serverless",

packages/next-plugin-sentry/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
MIT License
2+
3+
Copyright (c) 2021, Sentry
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/next-plugin-sentry/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<p align="center">
2+
<a href="https://sentry.io" target="_blank" align="center">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
4+
</a>
5+
<br />
6+
</p>
7+
8+
# Official Sentry SDK for NextJS
9+
10+
TODO: npm version, npm dm, npm dt, typedoc
11+
12+
## Links
13+
14+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)
15+
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
16+
17+
## Usage
18+
19+
TODO

packages/next-plugin-sentry/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.serverConfig = {};
2+
exports.clientConfig = {};

packages/next-plugin-sentry/env.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const getDsn = () =>
2+
process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
3+
4+
export const getRelease = () =>
5+
process.env.SENTRY_RELEASE ||
6+
process.env.NEXT_PUBLIC_SENTRY_RELEASE ||
7+
process.env.VERCEL_GITHUB_COMMIT_SHA ||
8+
process.env.VERCEL_GITLAB_COMMIT_SHA ||
9+
process.env.VERCEL_BITBUCKET_COMMIT_SHA;

packages/next-plugin-sentry/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { serverConfig, clientConfig } = require('./config.js');
2+
3+
const Sentry = require('@sentry/nextjs');
4+
Sentry.showReportDialog = (...args) => {
5+
Sentry._callOnClient('showReportDialog', ...args);
6+
};
7+
8+
exports.Sentry = Sentry;
9+
exports.serverConfig = serverConfig;
10+
exports.clientConfig = clientConfig;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@sentry/next-plugin-sentry",
3+
"version": "6.2.1",
4+
"description": "Plugin for Next.js",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/next-plugin-sentry",
7+
"author": "Sentry",
8+
"license": "MIT",
9+
"engines": {
10+
"node": ">=6"
11+
},
12+
"main": "index.js",
13+
"publishConfig": {
14+
"access": "public"
15+
},
16+
"dependencies": {
17+
"@sentry/nextjs": "6.2.1",
18+
"@sentry/integrations": "6.2.1"
19+
},
20+
"devDependencies": {
21+
"eslint": "7.20.0",
22+
"rimraf": "3.0.2"
23+
},
24+
"nextjs": {
25+
"name": "sentry",
26+
"required-env": []
27+
},
28+
"peerDependencies": {
29+
"next": "*"
30+
},
31+
"scripts": {
32+
"link:yarn": "yarn link",
33+
"pack": "npm pack"
34+
},
35+
"volta": {
36+
"extends": "../../package.json"
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { withScope, captureException } from '@sentry/nextjs';
2+
3+
export default async function onErrorClient({ err, errorInfo, renderErrorProps, data, version }) {
4+
// TODO: Extract some useful metadata from the router and other arguments — Kamil
5+
6+
withScope(scope => {
7+
if (typeof errorInfo?.componentStack === 'string') {
8+
scope.setContext('react', {
9+
componentStack: errorInfo.componentStack.trim(),
10+
});
11+
}
12+
captureException(err);
13+
});
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { captureException, flush, Handlers, withScope } from '@sentry/nextjs';
2+
import getConfig from 'next/config';
3+
4+
const { parseRequest } = Handlers;
5+
6+
export default async function onErrorServer(err) {
7+
const { serverRuntimeConfig = {}, publicRuntimeConfig = {} } = getConfig() || {};
8+
const sentryTimeout = serverRuntimeConfig.sentryTimeout || publicRuntimeConfig.sentryTimeout || 2000;
9+
10+
withScope(scope => {
11+
if (typeof err.req !== 'undefined') {
12+
scope.addEventProcessor(event =>
13+
parseRequest(event, err.req, {
14+
// 'cookies' and 'query_string' use `dynamicRequire` which has a bug in SSR envs right now — Kamil
15+
request: ['data', 'headers', 'method', 'url'],
16+
}),
17+
);
18+
}
19+
captureException(err instanceof Error ? err : err.err);
20+
});
21+
22+
await flush(sentryTimeout);
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { init } from '@sentry/nextjs';
2+
import getConfig from 'next/config';
3+
4+
import { getDsn, getRelease } from '../env';
5+
import { clientConfig } from '../config';
6+
7+
export default async function initClient() {
8+
const { publicRuntimeConfig = {} } = getConfig() || {};
9+
const runtimeConfig = publicRuntimeConfig.sentry || {};
10+
11+
init({
12+
dsn: getDsn(),
13+
...(getRelease() && { release: getRelease() }),
14+
...runtimeConfig,
15+
...clientConfig,
16+
});
17+
}

0 commit comments

Comments
 (0)