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(gatsby): Support non-serializable SDK options #4064

Merged
merged 11 commits into from Oct 22, 2021

Conversation

iker-barriocanal
Copy link
Member

@iker-barriocanal iker-barriocanal commented Oct 14, 2021

The current Gatsby SDK requires to set the SDK options in gatsby-config.js, which doesn't allow to set non-serializable options. This PR introduces sentry.config.js, a file where you can define your Sentry.init with non-serializable options. The current approach of defining the SDK options is still supported. Only one of the two approaches can be used; if a user defines both, the init in sentry.config.js is prioritized and other options are ignored.

This PR is a follow-up to #4040.

How does this work?

That file is injected into the webpack config's entry properties, which makes the SDK to initialize before gatsby-browser.js is run, the only place where the SDK was being initialized before. That script has the following behavior:

  • If the SDK has been initialized by sentry.config.js and there are options defined in gatsby-config.js, a warning message telling the user to merge both configs is logged. The SDK is initialized with the init in the config file.
  • If the SDK hasn't been initialized in sentry.config.js and there aren't any options defined in gatsby-config.js, an error message telling the user init options aren't defined is logged, linking to the docs.
  • If options are only defined in the gatsby config, the SDK is initialized with these options.

@iker-barriocanal iker-barriocanal requested a review from a team October 14, 2021 08:55
@iker-barriocanal iker-barriocanal self-assigned this Oct 14, 2021
@iker-barriocanal iker-barriocanal requested review from lobsterkatie and vladanpaunovic and removed request for a team October 14, 2021 08:55
@github-actions
Copy link
Contributor

github-actions bot commented Oct 14, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 22.47 KB (+0.01% 🔺)
@sentry/browser - Webpack 23.35 KB (0%)
@sentry/react - Webpack 23.38 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.91 KB (+0.01% 🔺)

packages/gatsby/gatsby-node.js Outdated Show resolved Hide resolved
packages/gatsby/gatsby-browser.js Outdated Show resolved Hide resolved
@@ -1,17 +1,63 @@
const Sentry = require('@sentry/gatsby');

// To avoid confusion, you must set the Sentry configuration in one
Copy link
Member

Choose a reason for hiding this comment

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

I think this behaviour is still too complicated. Let's just make it so that if sentry.config.{js, ts} is defined, that will always take priority over the plugin params (and then we can just log a warning if plugin params are also used).

Copy link
Member

Choose a reason for hiding this comment

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

We can then also rm -rf all this enabled logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified the logic. We can't get rid of the isSentryInitialized because the gatsby-browser script's scope is in the browser, so we need to still check for the Sentry instance. But, we are no longer playing around with the enabled.

If...
- The SDK has been initialized in a config file, use that instance.
  If options are also defined, log a warning to merge the options.
- The SDK has not been initialized, initialize it.
  Unless options have not been provided, and log an error if that happens.
Copy link
Contributor

@rhcarvalho rhcarvalho left a comment

Choose a reason for hiding this comment

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

Was curious to see how this is implemented. A few suggestions / stale comments (?).

packages/gatsby/gatsby-browser.js Show resolved Hide resolved
packages/gatsby/gatsby-browser.js Show resolved Hide resolved
packages/gatsby/gatsby-node.js Outdated Show resolved Hide resolved
packages/gatsby/gatsby-node.js Outdated Show resolved Hide resolved
packages/gatsby/gatsby-node.js Outdated Show resolved Hide resolved
@iker-barriocanal iker-barriocanal enabled auto-merge (squash) October 22, 2021 08:44
@iker-barriocanal iker-barriocanal merged commit 83b5cc6 into master Oct 22, 2021
@iker-barriocanal iker-barriocanal deleted the iker/fix/gatsby-non-serializable-opts branch October 22, 2021 08:49
iker-barriocanal added a commit to getsentry/sentry-docs that referenced this pull request Nov 4, 2021
Adds the docs supporting the Gatsby SDK initialization in a config file, see getsentry/sentry-javascript#4064.
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

3 participants