fix(nextjs): Always initialize SDK with global hub #4086
Merged
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.
In a nextjs app using
@sentry/nextjs
, the code which initializes the SDK is kicked off by the first incoming request.* Because we use domains to prevent scope bleed between requests, at the time whenSentry.init()
is called, we're already in a domain, and as a result, @sentry/node'sinit
function sets the domain's hub as the global hub, the hub from which all subsequent hubs will inherit. This means that - currently - all future hubs inherit data from that first request, which they shouldn't.This PR fixes that by essentially deactivating the domain while the SDK is initialized, so that all initialization code will act on the global hub. Then, because ideally the domain hub would have inherited from the global hub, the work done to the global hub is copied over to the domain hub (to mimic the effects of the inheritance) before the domain is made active again.
*It's the request handler's loading of either
_app
or the API route handler (both of which we've prefaced with the user'ssentry.server.config.js
) which triggers the user'sSentry.init
to run.