Skip to content

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

Merged
lobsterkatie merged 4 commits into
masterfrom
kmclb-nextjs-init-node-with-global-hub
Oct 26, 2021
Merged

fix(nextjs): Always initialize SDK with global hub#4086
lobsterkatie merged 4 commits into
masterfrom
kmclb-nextjs-init-node-with-global-hub

Conversation

@lobsterkatie

Copy link
Copy Markdown
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

github-actions Bot commented Oct 26, 2021

Copy link
Copy Markdown
Contributor

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% 🔽)

Comment thread packages/nextjs/src/index.server.ts
Comment thread packages/nextjs/src/index.server.ts
@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