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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { init } from '@sentry/nextjs';
2+
import { RewriteFrames } from '@sentry/integrations';
3+
import getConfig from 'next/config';
4+
5+
import { getDsn, getRelease } from '../env';
6+
import { serverConfig } from '../config';
7+
8+
export default async function initServer() {
9+
const { serverRuntimeConfig = {}, publicRuntimeConfig = {} } = getConfig() || {};
10+
const runtimeConfig = serverRuntimeConfig.sentry || publicRuntimeConfig.sentry || {};
11+
12+
init({
13+
dsn: getDsn(),
14+
...(getRelease() && { release: getRelease() }),
15+
...runtimeConfig,
16+
...serverConfig,
17+
integrations: [
18+
new RewriteFrames({
19+
iteratee: frame => {
20+
try {
21+
const [_, path] = frame.filename.split('.next/');
22+
if (path) {
23+
frame.filename = `app:///_next/${path}`;
24+
}
25+
} catch {}
26+
return frame;
27+
},
28+
}),
29+
...(runtimeConfig.integrations || []),
30+
...(serverConfig.integrations || []),
31+
],
32+
});
33+
}

packages/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"engines": {
1010
"node": ">=6"
1111
},
12-
"main": "./dist/index.js",
1312
"module": "./esm/node.js",
1413
"browser": "./esm/browser.js",
1514
"types": "./dist/index.d.ts",
@@ -21,6 +20,7 @@
2120
"@sentry/minimal": "6.2.1",
2221
"@sentry/node": "6.2.1",
2322
"@sentry/react": "6.2.1",
23+
"@sentry/next-plugin-sentry": "6.2.1",
2424
"@sentry/wizard": "1.2.0",
2525
"@sentry/webpack-plugin": "1.14.1"
2626
},

packages/nextjs/src/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { InitDecider } from './utils/initDecider';
44
import { MetadataBuilder } from './utils/metadataBuilder';
55
import { NextjsOptions } from './utils/nextjsOptions';
66

7+
export * from '@sentry/react';
8+
79
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
810
export function init(options: NextjsOptions): any {
911
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']);
@@ -16,5 +18,3 @@ export function init(options: NextjsOptions): any {
1618
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
1719
}
1820
}
19-
20-
export * from '@sentry/minimal';

packages/nextjs/src/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { InitDecider } from './utils/initDecider';
44
import { MetadataBuilder } from './utils/metadataBuilder';
55
import { NextjsOptions } from './utils/nextjsOptions';
66

7+
export * from '@sentry/node';
8+
79
/** Inits the Sentry NextJS SDK on node. */
810
export function init(options: NextjsOptions): any {
911
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']);
@@ -16,5 +18,3 @@ export function init(options: NextjsOptions): any {
1618
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
1719
}
1820
}
19-
20-
export * from '@sentry/minimal';

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,6 +3317,16 @@
33173317
"@sentry/utils" "6.2.1"
33183318
tslib "^1.9.3"
33193319

3320+
3321+
version "6.2.1"
3322+
resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-6.2.1.tgz#caa9b49de29523698668d45827633be86b2268ff"
3323+
integrity sha512-UBvuil/b9M5HGH6aBDzTiIVRsmpC/wqwDKy28IO05XLdalmKgJ9C1EQhoyN6xw+1lINpXXFtfq4NhfgZgWbc7Q==
3324+
dependencies:
3325+
"@sentry/types" "6.2.1"
3326+
"@sentry/utils" "6.2.1"
3327+
localforage "^1.8.1"
3328+
tslib "^1.9.3"
3329+
33203330
33213331
version "6.2.1"
33223332
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.1.tgz#8f01480e1b56bc7dd54adf925e5317f233e19384"

0 commit comments

Comments
 (0)