Skip to content

feat: Add initial files for NextJS SDK #3306

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 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"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.
19 changes: 19 additions & 0 deletions packages/next-plugin-sentry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<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 NextJS

TODO: npm version, npm dm, npm dt, typedoc

## Links

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

## Usage

TODO
2 changes: 2 additions & 0 deletions packages/next-plugin-sentry/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.serverConfig = {};
exports.clientConfig = {};
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;
10 changes: 10 additions & 0 deletions packages/next-plugin-sentry/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { serverConfig, clientConfig } = require('./config.js');

const Sentry = require('@sentry/nextjs');
Sentry.showReportDialog = (...args) => {
Sentry._callOnClient('showReportDialog', ...args);
};

exports.Sentry = Sentry;
exports.serverConfig = serverConfig;
exports.clientConfig = clientConfig;
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.1",
"description": "Plugin for Next.js",
"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.1",
"@sentry/integrations": "6.2.1"
},
"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"
}
}
14 changes: 14 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,14 @@
import { withScope, captureException } from '@sentry/nextjs';

export default async function onErrorClient({ err, errorInfo, renderErrorProps, data, version }) {
// TODO: Extract some useful metadata from the router and other arguments — Kamil

withScope(scope => {
if (typeof errorInfo?.componentStack === 'string') {
scope.setContext('react', {
componentStack: errorInfo.componentStack.trim(),
});
}
captureException(err);
});
}
23 changes: 23 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,23 @@
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 (typeof err.req !== 'undefined') {
scope.addEventProcessor(event =>
parseRequest(event, err.req, {
// 'cookies' and 'query_string' use `dynamicRequire` which has a bug in SSR envs right now — Kamil
request: ['data', 'headers', 'method', 'url'],
}),
);
}
captureException(err instanceof Error ? err : err.err);
});

await flush(sentryTimeout);
}
17 changes: 17 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,17 @@
import { init } from '@sentry/nextjs';
import getConfig from 'next/config';

import { getDsn, getRelease } from '../env';
import { clientConfig } from '../config';

export default async function initClient() {
const { publicRuntimeConfig = {} } = getConfig() || {};
const runtimeConfig = publicRuntimeConfig.sentry || {};

init({
dsn: getDsn(),
...(getRelease() && { release: getRelease() }),
...runtimeConfig,
...clientConfig,
});
}
33 changes: 33 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,33 @@
import { init } from '@sentry/nextjs';
import { RewriteFrames } from '@sentry/integrations';
import getConfig from 'next/config';

import { getDsn, getRelease } from '../env';
import { serverConfig } from '../config';

export default async function initServer() {
const { serverRuntimeConfig = {}, publicRuntimeConfig = {} } = getConfig() || {};
const runtimeConfig = serverRuntimeConfig.sentry || publicRuntimeConfig.sentry || {};

init({
dsn: getDsn(),
...(getRelease() && { release: getRelease() }),
...runtimeConfig,
...serverConfig,
integrations: [
new RewriteFrames({
iteratee: frame => {
try {
const [_, path] = frame.filename.split('.next/');
if (path) {
frame.filename = `app:///_next/${path}`;
}
} catch {}
return frame;
},
}),
...(runtimeConfig.integrations || []),
...(serverConfig.integrations || []),
],
});
}
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"engines": {
"node": ">=6"
},
"main": "./dist/index.js",
"module": "./esm/node.js",
"browser": "./esm/browser.js",
"types": "./dist/index.d.ts",
Expand All @@ -21,6 +20,7 @@
"@sentry/minimal": "6.2.1",
"@sentry/node": "6.2.1",
"@sentry/react": "6.2.1",
"@sentry/next-plugin-sentry": "6.2.1",
"@sentry/wizard": "1.2.0",
"@sentry/webpack-plugin": "1.14.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { InitDecider } from './utils/initDecider';
import { MetadataBuilder } from './utils/metadataBuilder';
import { NextjsOptions } from './utils/nextjsOptions';

export * from '@sentry/react';

/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
export function init(options: NextjsOptions): any {
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']);
Expand All @@ -16,5 +18,3 @@ export function init(options: NextjsOptions): any {
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
}
}

export * from '@sentry/minimal';
4 changes: 2 additions & 2 deletions packages/nextjs/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { InitDecider } from './utils/initDecider';
import { MetadataBuilder } from './utils/metadataBuilder';
import { NextjsOptions } from './utils/nextjsOptions';

export * from '@sentry/node';

/** Inits the Sentry NextJS SDK on node. */
export function init(options: NextjsOptions): any {
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']);
Expand All @@ -16,5 +18,3 @@ export function init(options: NextjsOptions): any {
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
}
}

export * from '@sentry/minimal';
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3317,6 +3317,16 @@
"@sentry/utils" "6.2.1"
tslib "^1.9.3"

"@sentry/integrations@6.2.1":
version "6.2.1"
resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-6.2.1.tgz#caa9b49de29523698668d45827633be86b2268ff"
integrity sha512-UBvuil/b9M5HGH6aBDzTiIVRsmpC/wqwDKy28IO05XLdalmKgJ9C1EQhoyN6xw+1lINpXXFtfq4NhfgZgWbc7Q==
dependencies:
"@sentry/types" "6.2.1"
"@sentry/utils" "6.2.1"
localforage "^1.8.1"
tslib "^1.9.3"

"@sentry/minimal@6.2.1":
version "6.2.1"
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.1.tgz#8f01480e1b56bc7dd54adf925e5317f233e19384"
Expand Down