Skip to content

Add fallback to globalThis if window and global undefined#123

Merged
niemyjski merged 4 commits into
csnover:masterfrom
austintheriotgl:master
Jan 27, 2026
Merged

Add fallback to globalThis if window and global undefined#123
niemyjski merged 4 commits into
csnover:masterfrom
austintheriotgl:master

Conversation

@austintheriotgl

@austintheriotgl austintheriotgl commented Oct 28, 2025

Copy link
Copy Markdown
Contributor

This PR attempts to fix this error here: #122

It keeps the original window keyword for backwards compatibility for older browsers, adds a check if the global keyword is defined (it is in Node environments, but not in browser contexts), then falls back to globalThis if the other two are not defined, which should allow it to work in Web Worker contexts.

@drfuzzyness drfuzzyness left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Rather than remove the window check, would it be better to add to the ternary chain to check if global is defined, then fall back to globalThis? Just using globalThis is a JS feature from 2020 and this library targets platforms from the 2000s.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#browser_compatibility
[2] https://github.com/csnover/TraceKit#supports-all-major-browsers-from-ie6-to-opera-the-android-webview-and-everywhere-in-between

@austintheriotgl

Copy link
Copy Markdown
Contributor Author

Thanks @drfuzzyness, that seems reasonable to me, so long as the expression typeof global doesn't throw in Web Worker contexts. I would expect it to work similarly to the main thread, which doesn't throw:

CleanShot 2025-11-03 at 13 05 06@2x

Updated the PR with your suggestion.

@austintheriotgl

Copy link
Copy Markdown
Contributor Author

I also adjusted the .jshintrc file to allow the globalThis global keyword, and the build now passes locally for me:

CleanShot 2025-11-03 at 13 09 42@2x

@austintheriotgl

Copy link
Copy Markdown
Contributor Author

This is failing in CI for reasons that appear unrelated to my changes:

Fatal error: Failed to launch chrome!
[1104/145111.167389:FATAL:zygote_host_impl_linux.cc(116)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox.

https://github.com/csnover/TraceKit/actions/runs/19046314393/job/54479505479?pr=123

@niemyjski

Copy link
Copy Markdown
Collaborator

Can you please merge in master to see if the updated ci works. We really need to move off grunt and jasmine. Would love a pr for that if anyone is reading this

@austintheriotgl

Copy link
Copy Markdown
Contributor Author

@niemyjski Just merged in master 👍

@austintheriotgl austintheriotgl changed the title Replace window and global keywords with globalThis Add fallback to globalThis if window and global undefined Nov 18, 2025
@austintheriotgl

Copy link
Copy Markdown
Contributor Author

Hey all, is there anything you need from me to move this along? I see that CI is still failing

@niemyjski

Copy link
Copy Markdown
Collaborator

@austintheriotgl really sorry for the delay. I just had time to get the ci partially updated and building again.

@niemyjski niemyjski merged commit 3e4965c into csnover:master Jan 27, 2026
1 check failed
@austintheriotgl

Copy link
Copy Markdown
Contributor Author

Thanks for merging this, @niemyjski! Could we get a release out with the fix, so that I can bump the version in the Honeycomb SDK?

@niemyjski

Copy link
Copy Markdown
Collaborator

I'll try and get this pushed

@niemyjski

Copy link
Copy Markdown
Collaborator

Pushed :)

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.

3 participants