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

feat(browser): Add __SENTRY_RELEASE__ magic string #6322

Merged
merged 4 commits into from
Nov 30, 2022

Conversation

lforst
Copy link
Member

@lforst lforst commented Nov 29, 2022

This adds a magic string in the browser code that can be used by build tooling to inject a release value into the SDK during build time.

This is not used right now but it makes sense to introduce something like it as early as possible because, in the future, we want to move our bundler plugins to use this approach instead of injecting a variable on global.

@lforst lforst requested review from mydea, Lms24 and AbhiPrasad November 29, 2022 10:30
@lforst lforst self-assigned this Nov 29, 2022
Comment on lines -28 to -29
// eslint-disable-next-line no-undef
release: __SENTRY_RELEASE__,
Copy link
Member

Choose a reason for hiding this comment

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

m: Let's just check that removing this doesn't change/break anything for Gatsby

Copy link
Member Author

Choose a reason for hiding this comment

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

Tested it in a gatsby app and it still works!

@github-actions
Copy link
Contributor

github-actions bot commented Nov 29, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.55 KB (+0.07% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 60.49 KB (+0.12% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.22 KB (+0.11% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 53.84 KB (+0.13% 🔺)
@sentry/browser - Webpack (gzipped + minified) 19.95 KB (+0.09% 🔺)
@sentry/browser - Webpack (minified) 65.32 KB (+0.11% 🔺)
@sentry/react - Webpack (gzipped + minified) 19.97 KB (+0.09% 🔺)
@sentry/nextjs Client - Webpack (gzipped + minified) 45.98 KB (+0.05% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.43 KB (+0.06% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 24.85 KB (+0.08% 🔺)
@sentry/replay index.js 83.65 KB (-0.07% 🔽)
@sentry/replay - Webpack (gzipped + minified) 83.67 KB (-0.07% 🔽)
@sentry/replay - Webpack (minified) 272.53 KB (-0.06% 🔽)

@@ -97,6 +102,10 @@ export function init(options: BrowserOptions = {}): void {
if (WINDOW.SENTRY_RELEASE && WINDOW.SENTRY_RELEASE.id) {
options.release = WINDOW.SENTRY_RELEASE.id;
}

if (typeof __SENTRY_RELEASE__ === 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

l: Should this take precedence over a release set on window? just thinking, to make sure this is what we expect :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I think so. It's either that or the other way around.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, you are more "into" these things, but I imagine it could be weird if you somehow set window.SENTRY_RELEASE to a specific value, just to have it be overwritten by SENTRY_RELEASE?

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 would put it like this: I would give precedence depending on how much work it is to set a value. To set window.SENTRY_RELEASE you just have to do exactly that - assign a value. To set __SENTRY_RELEASE__ you have to instruct your bundler to replace that string, probably even by using some plugin. I would argue that this is a lot more effort to set this value.

I guess there is the argument that we want to have the value be configurable via window even if the bundler injects something. I can change it.

Copy link
Member

Choose a reason for hiding this comment

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

I guess there is the argument that we want to have the value be configurable via window even if the bundler injects something.

Yeah, that's basically my thought - if someone overwrites this at runtime or whenever, I think it may make sense to have this take precendence. But I don't have strong feelings on this, so feel free to also keep it as it is if you think that makes more sense 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

No I def like it. Will change!

@@ -97,6 +102,10 @@ export function init(options: BrowserOptions = {}): void {
if (WINDOW.SENTRY_RELEASE && WINDOW.SENTRY_RELEASE.id) {
Copy link
Member

Choose a reason for hiding this comment

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

m: Should we deprecate and remove in v8?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would kill old webpack plugin compatibility. We definitely could. But I would probably delay it by a major or two.

Copy link
Member

@AbhiPrasad AbhiPrasad Nov 29, 2022

Choose a reason for hiding this comment

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

it would be a great way to get people to migrate to newer webpack plugin version, and also we can then point people to this as a feature in the first place when highlighting it in the changelog.

@lforst lforst merged commit 9d69b0f into master Nov 30, 2022
@lforst lforst deleted the lforst-release-magic-string branch November 30, 2022 10:52
@olivierchatry
Copy link

olivierchatry commented Dec 2, 2022

Well, this change make my build crash, as I was using __SENTRY_RELEASE__ to pass the release value to my software :( renamed it for now.

@lforst
Copy link
Member Author

lforst commented Dec 2, 2022

Well, this change make my build crash, as I was using __SENTRY_RELEASE__ to pass the release value to my software :( renamed it for now.

Damn, sorry about that. Wasn't expecting to run into collisions. We will probably leave it at __SENTRY_RELEASE__ so the thing you rename it to probably is a long term solution!

@@ -93,6 +98,11 @@ export function init(options: BrowserOptions = {}): void {
options.defaultIntegrations = defaultIntegrations;
}
if (options.release === undefined) {
// This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
if (typeof __SENTRY_RELEASE__ === 'string') {
options.release = __SENTRY_RELEASE__;

Choose a reason for hiding this comment

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

Potential pitfall of this approach is that any new release on user side will be changing vendor module source even if sentry version does not change. This will lead to new hash of vendor chunk of user app (which is usually the heaviest) and cache busting for end-users, need to download it again even if release contains only app changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi, good point! This isn't used yet so we can change it at any point. Do you have suggestions on how to solve it differently?

Choose a reason for hiding this comment

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

Simplest way is to provide release name as parameter at runtime.

Another way is to provide separate entry point that user need to include into build pipeline manually (or via bundler plugin, sentry webpack plugin does something similar). This entry file can have inline release name but user can choose chunk for this entry, for example inline chunk that will be inlined into index.html

Copy link
Member Author

Choose a reason for hiding this comment

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

The SDK already has a way of configuring a release name during runtime: Sentry.init({ release: 'your-release-name'})

As for the separate entry point - this is something users can pretty easily do themselves. The SDK will currently already read the release name from window.SENTRY_RELEASE.id or global.SENTRY_RELEASE.id if it is set. This is the global variable that the webpack plugin and our other bundler plugins leverage to inject a release https://github.com/getsentry/sentry-javascript-bundler-plugins. If you want to disable the release injection done by the plugins you can use the releaseInjectionTargets option.

Considering the above, I think we can confidently leave everything as-is. I like the idea of providing a separate entry point though. We will definitely consider it in the future.

Choose a reason for hiding this comment

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

Thanks for mentioning releaseInjectionTargets, was not aware of this option.

So considering this option and ability to provide release name at runtime, changes in PR can be mitigated with careful setup for those who care about long term caching.

Thank you for thoughtful reply.

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

6 participants