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

ref(nextjs): Use generic loader to inject global values #6484

Merged
merged 6 commits into from
Dec 12, 2022

Conversation

lforst
Copy link
Member

@lforst lforst commented Dec 9, 2022

Refactor to add a webpack loader to the Next.js SDK to inject global variables into both the browser and the server-side SDK.

This change is needed for #5571 where we need to put a string on the global scope in the build step to read from during runtime. We have the prefix loader that is able to inject pretty much any code in the SDK - that would have also been an option to use but it seemed a bit overkill for this simple use-case.

This loader will also potentially become useful in the future for planned heartbeat/pulse checks during initialization to check if the SDK has been set up correctly.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.7 KB (+0.01% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 61.03 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.49 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 54.56 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 20.28 KB (0%)
@sentry/browser - Webpack (minified) 66.33 KB (0%)
@sentry/react - Webpack (gzipped + minified) 20.3 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 47.3 KB (+0.02% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.68 KB (+0.01% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 25.13 KB (+0.02% 🔺)
@sentry/replay ES6 CDN Bundle (gzipped + minified) 41.72 KB (+0.09% 🔺)
@sentry/replay - Webpack (gzipped + minified) 38 KB (+0.09% 🔺)

@lforst lforst marked this pull request as ready for review December 9, 2022 12:16
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Great refactor, much easier to understand. Only had a question about the isomorphic values.

packages/nextjs/src/config/webpack.ts Outdated Show resolved Hide resolved
Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

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

I like this idea!

Probably out of scope for this PR, but as the number of values we're shoving into the global namespace increases, I'm wondering if we should maybe think about putting them inside of __SENTRY__. WDYT?

packages/nextjs/src/config/loaders/valueInjectionLoader.ts Outdated Show resolved Hide resolved
const { values } = 'getOptions' in this ? this.getOptions() : this.query;

// Define some global proxy that works on server and on the browser.
let injectedCode = 'var _sentryCollisionFreeGlobalObject = typeof window === "undefined" ? global : window;\n';
Copy link
Member

Choose a reason for hiding this comment

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

M: Do we need to worry about web workers/self here, do you think? Is there a reason not to just use GLOBAL_OBJ from @sentry/utils?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we don't need to worry about web workers here. The loader is only applied to sentry.server.config.js and sentry.client.config.js and I think we are safe to assume that sentry.client.config.js will not be injected into any web worker contexts. Unless I am missing something.

Considering the above I would just keep it as-is to keep the logic simple.

packages/nextjs/src/config/loaders/valueInjectionLoader.ts Outdated Show resolved Hide resolved
packages/nextjs/src/config/webpack.ts Outdated Show resolved Hide resolved
packages/nextjs/test/config/loaders.test.ts Show resolved Hide resolved
packages/nextjs/test/config/loaders.test.ts Outdated Show resolved Hide resolved
@lforst
Copy link
Member Author

lforst commented Dec 12, 2022

Probably out of scope for this PR, but as the number of values we're shoving into the global namespace increases, I'm wondering if we should maybe think about putting them inside of SENTRY. WDYT?

Had a very similar thought. We probably should at some point - right now it would bloat the logic of the loader a lot. Let's see how many values we'll be injecting first.

@lforst lforst merged commit d9c6887 into master Dec 12, 2022
@lforst lforst deleted the lforst-generalize-prefix-loader branch December 12, 2022 13:03
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