Skip to content

Conversation

@austintheriotgl
Copy link

@austintheriotgl austintheriotgl commented Oct 28, 2025

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.

Copy link

@drfuzzyness drfuzzyness left a comment

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
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
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
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
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
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
Author

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

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