Skip to content

fix(nextjs): Always initialize SDK with global hub #4086

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 4 commits into from
Oct 26, 2021

Conversation

lobsterkatie
Copy link
Member

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 when Sentry.init() is called, we're already in a domain, and as a result, @sentry/node's init 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's sentry.server.config.js) which triggers the user's Sentry.init to run.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 26, 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.92 KB (-0.01% 🔽)

@lobsterkatie lobsterkatie merged commit 7c15a97 into master Oct 26, 2021
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-init-node-with-global-hub branch October 26, 2021 15:06
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.

2 participants