-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
iker-barriocanal
merged 11 commits into
master
from
iker/fix/gatsby-non-serializable-opts
Oct 22, 2021
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9acb791
fix(gatsby): Support non-serializable SDK options
iker-barriocanal cf31a72
Add comments and TODOs
iker-barriocanal 01573c5
fix browser tests
iker-barriocanal 00363ef
Merge branch 'master' into iker/fix/gatsby-non-serializable-opts
iker-barriocanal 41c41db
Support TS config files
iker-barriocanal 0e60931
simplify browser logic
iker-barriocanal 6685552
Clarify outdated comment
iker-barriocanal 4d63460
catch potential erros on `existsSync`
iker-barriocanal 4ce50e0
Use `forEach` instead of `map`
iker-barriocanal b3c4a82
Merge branch 'master' into iker/fix/gatsby-non-serializable-opts
iker-barriocanal 91e5787
Merge branch 'master' into iker/fix/gatsby-non-serializable-opts
iker-barriocanal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,63 @@ | ||
const Sentry = require('@sentry/gatsby'); | ||
|
||
// To avoid confusion, you must set the Sentry configuration in one | ||
// place: either in `gatsby-config.js` (without non-serializable | ||
// option support) or in `sentry.config.js` (supporting them). | ||
// Defining them in `sentry.config.js` makes the SDK to initialize | ||
// before this script is run, so we want to make sure to disable | ||
// the SDK if both places contain options and warn the user about | ||
// it. If the SDK hasn't been initialized at this point, we know | ||
// there aren't any options set in `sentry.config.js`, so it's safe | ||
// to initialize it here. | ||
|
||
exports.onClientEntry = function(_, pluginParams) { | ||
if (pluginParams === undefined) { | ||
const isIntialized = isSentryInitialized(); | ||
if (!areSentryOptionsDefined(pluginParams)) { | ||
if (isIntialized) { | ||
window.Sentry = Sentry; // For backwards compatibility | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'Sentry Logger [Error]: No config for the Gatsby SDK was found. Learn how to configure it on\n' + | ||
'https://docs.sentry.io/platforms/javascript/guides/gatsby/', | ||
); | ||
} | ||
return; | ||
} | ||
|
||
Sentry.init({ | ||
// eslint-disable-next-line no-undef | ||
release: __SENTRY_RELEASE__, | ||
// eslint-disable-next-line no-undef | ||
dsn: __SENTRY_DSN__, | ||
...pluginParams, | ||
}); | ||
if (isIntialized) { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'Sentry Logger [Error]: The SDK has been disabled because your Sentry config must live in one place.\n' + | ||
'Consider moving it all to your `sentry.config.js`.', | ||
); | ||
// TODO: link to the docs where the new approach is documented | ||
window.__SENTRY__.hub.getClient().getOptions().enabled = false; | ||
iker-barriocanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
Sentry.init({ | ||
// eslint-disable-next-line no-undef | ||
release: __SENTRY_RELEASE__, | ||
// eslint-disable-next-line no-undef | ||
dsn: __SENTRY_DSN__, | ||
...pluginParams, | ||
}); | ||
} | ||
|
||
window.Sentry = Sentry; | ||
window.Sentry = Sentry; // For backwards compatibility | ||
}; | ||
|
||
function isSentryInitialized() { | ||
// Although `window` should exist because we're in the browser (where this script | ||
// is run), and `__SENTRY__.hub` is created when importing the Gatsby SDK, double | ||
// check that in case something weird happens. | ||
return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient()); | ||
} | ||
|
||
function areSentryOptionsDefined(params) { | ||
if (params == undefined) return false; | ||
// Even if there aren't any options, there's a `plugins` property defined as an empty array | ||
if (Object.keys(params).length == 1 && Array.isArray(params.plugins) && params.plugins.length == 0) { | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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 thisenabled
logic.There was a problem hiding this comment.
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 thegatsby-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 theenabled
.