Skip to content
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

feat: Next.js SDK + Plugin #3301

Merged
merged 54 commits into from Apr 20, 2021
Merged

feat: Next.js SDK + Plugin #3301

merged 54 commits into from Apr 20, 2021

Conversation

iker-barriocanal
Copy link
Member

@iker-barriocanal iker-barriocanal commented Mar 3, 2021

Getting Started

It's assumed that NextJS is installed. Note that its version can't be latest; please replace it with a numeric version instead (it can be the latest version, but use the numeric notation).

Install:

yarn add @sentry/nextjs@6.3.0-beta.7

Note: It's important to use exactly @sentry/nextjs@6.3.0-beta.7

to setup the SDK call

npx @sentry/wizard -i nextjs

follow the steps and finish the setup.
After starting your next app, the Sentry SDK is started on both the frontend and backend.
The next config we provide out of the box comes with automatic source map setup and Vercel deployment support.

Features

✅  Error Monitoring both frontend and backend
✅  Automatic source maps (incl. upload) for both JS and TS
🔜  Performance Monitoring (transactions frontend + backend)


Manual Setup

The config we provide by default is rather lengthy and if you don't start from scratch and have an existing config, you need to merge it manually. Note however that the config files are automatically generated by the wizard.

next.config.js
// This file sets a custom webpack configuration to use your Next.js app
// with Sentry.
// https://nextjs.org/docs/api-reference/next.config.js/introduction
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

const { withSentryConfig } = require('@sentry/nextjs');

const moduleExports = {
  // Your existing module.exports
};

const SentryWebpackPluginOptions = {
  // Additional config options for the Sentry Webpack plugin. Keep in mind that
  // the following options are set automatically, and overriding them is not
  // recommended:
  //   release, url, org, project, authToken, configFile, stripPrefix,
  //   urlPrefix, include, ignore
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options.
};

// Make sure adding Sentry options is the last code to run before exporting, to
// ensure that your source maps include changes from all other Webpack plugins
module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions);
sentry.client.config.js
import * as Sentry from '@sentry/nextjs';

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({
  dsn: SENTRY_DSN || '__DSN__',
  // ...
});
sentry.server.config.js
import * as Sentry from '@sentry/nextjs';

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({
  dsn: SENTRY_DSN || '__DSN__',
  // ...
});

TypeScript Source Maps

If you are using TypeScript, make sure to add "sourceMap": true and "noEmit": false in your tsconfig.json in the compiler options:

{
  "compilerOptions": {
    "sourceMap": true,
    "noEmit": false,
  }
}

Usage

With the provided configuration you don't have to do anything else, the SDK is already working. If you want to configure it, custom the Sentry.init in the sentry.client.config.js or sentry.server.config.js.

The SDK itself underneath uses either @sentry/react or @sentry/node depending on if it's called on the frontend or backend.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 20.6 KB (0%)
@sentry/browser - Webpack 21.48 KB (0%)
@sentry/react - Webpack 21.52 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 27.82 KB (0%)

@HazAT HazAT changed the title feat: NextJS feat: Next.js SDK + Plugin Mar 4, 2021
@scefali

This comment has been minimized.

packages/next-plugin-sentry/README.md Outdated Show resolved Hide resolved
packages/nextjs/README.md Outdated Show resolved Hide resolved
packages/nextjs/README.md Show resolved Hide resolved
packages/nextjs/README.md Outdated Show resolved Hide resolved
packages/nextjs/README.md Outdated Show resolved Hide resolved
packages/nextjs/README.md Outdated Show resolved Hide resolved
packages/nextjs/src/utils/nextjsOptions.ts Outdated Show resolved Hide resolved
packages/nextjs/src/utils/serverIntegrations.ts Outdated Show resolved Hide resolved
`toHaveLength` instead of `toEqual({length})`
also: `toBeInstanceOf`
packages/next-plugin-sentry/package.json Outdated Show resolved Hide resolved
packages/next-plugin-sentry/src/on-error-server.js Outdated Show resolved Hide resolved
if (typeof err.req !== 'undefined') {
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?

});
}

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?

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?

packages/next-plugin-sentry/src/on-init-client.js Outdated Show resolved Hide resolved
packages/nextjs/test/serverIntegrations.test.ts Outdated Show resolved Hide resolved
Copy link
Member

@HazAT HazAT left a comment

Choose a reason for hiding this comment

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

:shipit:

@JavaGalaxy
Copy link

does the next.js supports sentry now?

@iker-barriocanal
Copy link
Member Author

Yes, it should be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants