Skip to content

feat: NextJS initialization config #3285

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 27 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
053e1ea
build: Install @sentry/browser, @sentry/node and @sentry/types
iker-barriocanal Feb 22, 2021
8357ff2
feat: Add `flush` and `close` functionality
iker-barriocanal Feb 22, 2021
56e6056
feat: Add NextJS options
iker-barriocanal Feb 22, 2021
a60448b
feat: Add init
iker-barriocanal Feb 22, 2021
9657701
feat: Add NextJS browser entry point and exports for @sentry/types an…
iker-barriocanal Feb 23, 2021
f62ecc7
feat: Add NextJS entry point
iker-barriocanal Feb 23, 2021
0a7e96b
ref: Extract dynamic module requiring
iker-barriocanal Feb 23, 2021
8931f67
build: Add dependencies and update `yarn.lock`
iker-barriocanal Feb 23, 2021
633a67a
feat: NextjsClient wrapper and interface
iker-barriocanal Feb 23, 2021
62ceb09
feat: Add browser client and backend
iker-barriocanal Feb 24, 2021
205c228
ref: Simplify approach with package.json
iker-barriocanal Feb 24, 2021
33f5a50
feat: Export @sentry/minimal
iker-barriocanal Feb 24, 2021
6273877
build: Remove @sentry/core and @sentry/utils from dependencies
iker-barriocanal Feb 24, 2021
57a7c94
docs: Add docs to browser and node inits
iker-barriocanal Feb 24, 2021
0d4c937
ref: Import only `init` from @sentry/browser and @sentry/node
iker-barriocanal Feb 24, 2021
949a5f4
build: Set exact versions for dependencies
iker-barriocanal Feb 24, 2021
0c75211
build: Update package version
iker-barriocanal Feb 24, 2021
8435adb
ref: Add MetadataBuilder and use it to build the metadata before SDKs…
iker-barriocanal Feb 24, 2021
241ee5a
fix: Include all used SDKs in the event payload
iker-barriocanal Feb 25, 2021
b53ed5a
ref: Split options into modules and use @sentry/react in the browser
iker-barriocanal Feb 26, 2021
c894423
feat: Only initialize the NextJS SDK in production environments
iker-barriocanal Feb 26, 2021
97ba3a0
feat: Add `forceInit` option to enable using Sentry in development en…
iker-barriocanal Feb 26, 2021
d479df6
test: Add test for initDecider
iker-barriocanal Feb 26, 2021
ef013b5
ref: Use `dev` flag in the NextJS options instead of `forceInit`
iker-barriocanal Mar 1, 2021
1902bff
Merge branch 'feat/next-js' into feat/next-js-config-init
iker-barriocanal Mar 1, 2021
5b43edb
build: Remove empty line from `yarn.lock`
iker-barriocanal Mar 1, 2021
7efbd9a
ref: Delete options.ts
iker-barriocanal Mar 1, 2021
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
5 changes: 3 additions & 2 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"access": "public"
},
"dependencies": {
"@sentry/browser": "6.2.0",
"@sentry/core": "6.2.0",
"@sentry/minimal": "6.2.0",
"@sentry/node": "6.2.0",
"@sentry/minimal": "6.2.0"
"@sentry/react": "6.2.0"
},
"devDependencies": {
"@sentry/types": "6.2.0",
Expand Down
18 changes: 13 additions & 5 deletions packages/nextjs/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { init as browserInit } from '@sentry/browser';
import { init as reactInit } from '@sentry/react';

import { MetadataBuilder, NextjsOptions } from './options';
import { InitDecider } from './utils/initDecider';
import { MetadataBuilder } from './utils/metadataBuilder';
import { NextjsOptions } from './utils/nextjsOptions';

/** Inits the Sentry NextJS SDK on the browser. */
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
export function init(options: NextjsOptions): any {
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'browser']);
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']);
metadataBuilder.addSdkMetadata();
browserInit(options);
const initDecider = new InitDecider(options);
if (initDecider.shouldInitSentry()) {
reactInit(options);
} else {
// eslint-disable-next-line no-console
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
}
}

export * from '@sentry/minimal';
12 changes: 10 additions & 2 deletions packages/nextjs/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { init as nodeInit } from '@sentry/node';

import { MetadataBuilder, NextjsOptions } from './options';
import { InitDecider } from './utils/initDecider';
import { MetadataBuilder } from './utils/metadataBuilder';
import { NextjsOptions } from './utils/nextjsOptions';

/** Inits the Sentry NextJS SDK on node. */
export function init(options: NextjsOptions): any {
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']);
metadataBuilder.addSdkMetadata();
nodeInit(options);
const initDecider = new InitDecider(options);
if (initDecider.shouldInitSentry()) {
nodeInit(options);
} else {
// eslint-disable-next-line no-console
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
}
}

export * from '@sentry/minimal';
14 changes: 11 additions & 3 deletions packages/nextjs/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { init as browserInit } from '@sentry/browser';
import { init as reactInit } from '@sentry/react';

import { MetadataBuilder, NextjsOptions } from './options';
import { InitDecider } from './utils/initDecider';
import { MetadataBuilder } from './utils/metadataBuilder';
import { NextjsOptions } from './utils/nextjsOptions';

/**
* The Sentry NextJS SDK Client.
Expand All @@ -11,5 +13,11 @@ import { MetadataBuilder, NextjsOptions } from './options';
export function init(options: NextjsOptions): void {
const metadataBuilder = new MetadataBuilder(options, ['nextjs']);
metadataBuilder.addSdkMetadata();
browserInit(options);
const initDecider = new InitDecider(options);
if (initDecider.shouldInitSentry()) {
reactInit(options);
} else {
// eslint-disable-next-line no-console
console.log('[Sentry] Detected a non-production environment. Not initializing Sentry.');
}
}
37 changes: 37 additions & 0 deletions packages/nextjs/src/utils/initDecider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextjsOptions } from './nextjsOptions';

export class InitDecider {
private _options: NextjsOptions;

constructor(options: NextjsOptions) {
this._options = options;
}

/**
* Returns a boolean representing whether the NextJS SDK should be initialised.
*
* The SDK should be initialised if the `dev` option is set to true.
* `dev` is optional, so if it isn't set or is set to false, the SDK will only
* be initialised in a production environment.
*/
public shouldInitSentry(): boolean {
if (this._isEnabledInDev() || this._isProdEnv()) {
return true;
}
return false;
}

/**
* Returns true if the option `dev` is true, and false otherwise.
*/
private _isEnabledInDev(): boolean {
return this._options.dev || false;
}

/**
* Returns whether the environment is a production environment.
*/
private _isProdEnv(): boolean {
return process.env.NODE_ENV !== undefined && process.env.NODE_ENV === 'production';
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { BrowserOptions, SDK_VERSION } from '@sentry/browser';
import { NodeOptions } from '@sentry/node';
import { Options, Package, SdkInfo } from '@sentry/types';
import { SDK_VERSION } from '@sentry/core';
import { Package, SdkInfo } from '@sentry/types';

import { NextjsOptions } from './nextjsOptions';

const SDK_NAME = 'sentry.javascript.nextjs';
const PACKAGE_NAME_PREFIX = 'npm:@sentry/';

export interface NextjsOptions extends Options, BrowserOptions, NodeOptions {
// TODO: options for NextJS
}

/**
* A builder for the SDK metadata in the options for the SDK initialization.
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/nextjs/src/utils/nextjsOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NodeOptions } from '@sentry/node';
import { BrowserOptions } from '@sentry/react';
import { Options } from '@sentry/types';

export interface NextjsOptions extends Options, BrowserOptions, NodeOptions {
/**
* A flag enabling the initialization of the SDK in development and other
* non-production environments. By default, the SDK is only initialised in
* production.
*/
dev?: boolean;
}
60 changes: 60 additions & 0 deletions packages/nextjs/test/initDecider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { InitDecider } from '../src/utils/initDecider';
import { NextjsOptions } from '../src/utils/nextjsOptions';

function setDevEnv(): void {
process.env.NODE_ENV = 'development';
}

function setProdEnv(): void {
process.env.NODE_ENV = 'production';
}

function getEmptyOptions(): NextjsOptions {
return {};
}

function getDevTrueOptions(): NextjsOptions {
return { dev: true };
}

function getDevFalseOptions(): NextjsOptions {
return { dev: false };
}

describe('decide initialization in development', () => {
beforeEach(setDevEnv);

test('without options', () => {
const initDecider = new InitDecider(getEmptyOptions());
expect(initDecider.shouldInitSentry()).toBeFalsy();
});

test('without development', () => {
const initDecider = new InitDecider(getDevFalseOptions());
expect(initDecider.shouldInitSentry()).toBeFalsy();
});

test('with development', () => {
const initDecider = new InitDecider(getDevTrueOptions());
expect(initDecider.shouldInitSentry()).toBeTruthy();
});
});

describe('decide initialization in production', () => {
beforeEach(setProdEnv);

test('without options', () => {
const initDecider = new InitDecider(getEmptyOptions());
expect(initDecider.shouldInitSentry()).toBeTruthy();
});

test('without development', () => {
const initDecider = new InitDecider(getDevFalseOptions());
expect(initDecider.shouldInitSentry()).toBeTruthy();
});

test('with development', () => {
const initDecider = new InitDecider(getDevTrueOptions());
expect(initDecider.shouldInitSentry()).toBeTruthy();
});
});
72 changes: 42 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,7 @@
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=

"@sentry/browser@^6.2.0":
"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-6.2.0.tgz#4113a92bc82f55e63f30cb16a94f717bd0b95817"
integrity sha512-4r3paHcHXLemj471BtNDhUs2kvJxk5XDRplz1dbC/LHXN5PWEXP4anhGILxOlxqi4y33r53PIZu3xXFjznaVZA==
Expand Down Expand Up @@ -3306,7 +3306,7 @@
"@sentry/utils" "6.2.0"
tslib "^1.9.3"

"@sentry/[email protected]", "@sentry/minimal@^6.2.0":
"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-6.2.0.tgz#718b70babb55912eeb38babaf7823d1bcdd77d1e"
integrity sha512-haxsx8/ZafhZUaGeeMtY7bJt9HbDlqeiaXrRMp1CxGtd0ZRQwHt60imEjl6IH1I73SEWxNfqScGsX2s3HzztMg==
Expand All @@ -3315,7 +3315,7 @@
"@sentry/types" "6.2.0"
tslib "^1.9.3"

"@sentry/node@^6.2.0":
"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/node/-/node-6.2.0.tgz#4c1860822a4a73d24242e5254124bd3bf9028d52"
integrity sha512-02lXk+56tPA3lWTvNLMGorp77wUVti8wOs+TlYARkJ+N+16dwqEBSBTy3hCDxlxriB+qHchSIS+ovPGi6WNiYA==
Expand All @@ -3330,6 +3330,18 @@
lru_map "^0.3.3"
tslib "^1.9.3"

"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/react/-/react-6.2.0.tgz#bf46c38762554f246ca9dec527e6a5b3168fb0b0"
integrity sha512-Jf3s7om1iLpApkN26O7c3Ult3lS91ekZNC4WKtcPb6b+KOBQ36sB0d1KhL3hGZ55UKLmgZu3jn2hd7bJ9EY3yA==
dependencies:
"@sentry/browser" "6.2.0"
"@sentry/minimal" "6.2.0"
"@sentry/types" "6.2.0"
"@sentry/utils" "6.2.0"
hoist-non-react-statics "^3.3.2"
tslib "^1.9.3"

"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/tracing/-/tracing-6.2.0.tgz#76c17e5dd3f1e61c8a4e3bd090f904a63d674765"
Expand All @@ -3341,7 +3353,7 @@
"@sentry/utils" "6.2.0"
tslib "^1.9.3"

"@sentry/[email protected]", "@sentry/types@^6.2.0":
"@sentry/[email protected]":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-6.2.0.tgz#ca020ff42913c6b9f88a9d0c375b5ee3965a2590"
integrity sha512-vN4P/a+QqAuVfWFB9G3nQ7d6bgnM9jd/RLVi49owMuqvM24pv5mTQHUk2Hk4S3k7ConrHFl69E7xH6Dv5VpQnQ==
Expand Down Expand Up @@ -9967,25 +9979,26 @@ eslint-visitor-keys@^2.0.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8"
integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==

eslint@7.6.0:
version "7.6.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.6.0.tgz#522d67cfaea09724d96949c70e7a0550614d64d6"
integrity sha512-QlAManNtqr7sozWm5TF4wIH9gmUm2hE3vNRUvyoYAa4y1l5/jxD/PQStEjBMQtCqZmSep8UxrcecI60hOpe61w==
eslint@7.20.0:
version "7.20.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.20.0.tgz#db07c4ca4eda2e2316e7aa57ac7fc91ec550bdc7"
integrity sha512-qGi0CTcOGP2OtCQBgWZlQjcTuP0XkIpYFj25XtRTQSHC+umNnp7UMshr2G8SLsRFYDdAPFeHOsiteadmMH02Yw==
dependencies:
"@babel/code-frame" "^7.0.0"
"@babel/code-frame" "7.12.11"
"@eslint/eslintrc" "^0.3.0"
ajv "^6.10.0"
chalk "^4.0.0"
cross-spawn "^7.0.2"
debug "^4.0.1"
doctrine "^3.0.0"
enquirer "^2.3.5"
eslint-scope "^5.1.0"
eslint-scope "^5.1.1"
eslint-utils "^2.1.0"
eslint-visitor-keys "^1.3.0"
espree "^7.2.0"
esquery "^1.2.0"
eslint-visitor-keys "^2.0.0"
espree "^7.3.1"
esquery "^1.4.0"
esutils "^2.0.2"
file-entry-cache "^5.0.1"
file-entry-cache "^6.0.0"
functional-red-black-tree "^1.0.1"
glob-parent "^5.0.0"
globals "^12.1.0"
Expand All @@ -9996,7 +10009,7 @@ [email protected]:
js-yaml "^3.13.1"
json-stable-stringify-without-jsonify "^1.0.1"
levn "^0.4.1"
lodash "^4.17.19"
lodash "^4.17.20"
minimatch "^3.0.4"
natural-compare "^1.4.0"
optionator "^0.9.1"
Expand All @@ -10005,30 +10018,29 @@ [email protected]:
semver "^7.2.1"
strip-ansi "^6.0.0"
strip-json-comments "^3.1.0"
table "^5.2.3"
table "^6.0.4"
text-table "^0.2.0"
v8-compile-cache "^2.0.3"

eslint@^7.20.0:
version "7.20.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.20.0.tgz#db07c4ca4eda2e2316e7aa57ac7fc91ec550bdc7"
integrity sha512-qGi0CTcOGP2OtCQBgWZlQjcTuP0XkIpYFj25XtRTQSHC+umNnp7UMshr2G8SLsRFYDdAPFeHOsiteadmMH02Yw==
eslint@7.6.0:
version "7.6.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.6.0.tgz#522d67cfaea09724d96949c70e7a0550614d64d6"
integrity sha512-QlAManNtqr7sozWm5TF4wIH9gmUm2hE3vNRUvyoYAa4y1l5/jxD/PQStEjBMQtCqZmSep8UxrcecI60hOpe61w==
dependencies:
"@babel/code-frame" "7.12.11"
"@eslint/eslintrc" "^0.3.0"
"@babel/code-frame" "^7.0.0"
ajv "^6.10.0"
chalk "^4.0.0"
cross-spawn "^7.0.2"
debug "^4.0.1"
doctrine "^3.0.0"
enquirer "^2.3.5"
eslint-scope "^5.1.1"
eslint-scope "^5.1.0"
eslint-utils "^2.1.0"
eslint-visitor-keys "^2.0.0"
espree "^7.3.1"
esquery "^1.4.0"
eslint-visitor-keys "^1.3.0"
espree "^7.2.0"
esquery "^1.2.0"
esutils "^2.0.2"
file-entry-cache "^6.0.0"
file-entry-cache "^5.0.1"
functional-red-black-tree "^1.0.1"
glob-parent "^5.0.0"
globals "^12.1.0"
Expand All @@ -10039,7 +10051,7 @@ eslint@^7.20.0:
js-yaml "^3.13.1"
json-stable-stringify-without-jsonify "^1.0.1"
levn "^0.4.1"
lodash "^4.17.20"
lodash "^4.17.19"
minimatch "^3.0.4"
natural-compare "^1.4.0"
optionator "^0.9.1"
Expand All @@ -10048,7 +10060,7 @@ eslint@^7.20.0:
semver "^7.2.1"
strip-ansi "^6.0.0"
strip-json-comments "^3.1.0"
table "^6.0.4"
table "^5.2.3"
text-table "^0.2.0"
v8-compile-cache "^2.0.3"

Expand Down Expand Up @@ -17947,7 +17959,7 @@ rimraf@2, [email protected], rimraf@^2.2.8, rimraf@^2.3.4, rimraf@^2.4.3, rimraf@^2.4
dependencies:
glob "^7.1.3"

rimraf@^3.0.0, rimraf@^3.0.1, rimraf@^3.0.2:
rimraf@3.0.2, rimraf@^3.0.0, rimraf@^3.0.1, rimraf@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
Expand Down