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

Sentry does not init in browser extension's service worker #13152

Closed
3 tasks done
ddomonkos opened this issue Aug 1, 2024 · 3 comments · Fixed by #13156
Closed
3 tasks done

Sentry does not init in browser extension's service worker #13152

ddomonkos opened this issue Aug 1, 2024 · 3 comments · Fixed by #13156
Assignees
Labels
Package: browser Issues related to the Sentry Browser SDK Type: Bug

Comments

@ddomonkos
Copy link

ddomonkos commented Aug 1, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.21.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

No response

Steps to Reproduce

  1. Have a Manifest V3 browser (Chrome) extension
  2. Run Sentry.init in the service worker (this used to be the "background script")
  3. Observe an error

Expected Result

Sentry initializes without error.

Actual Result

An error is printed out:

[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/

Relevant PRs:

The problem is with the condition WINDOW === WINDOW.top. In a service worker, WINDOW is resolved to globalThis and globalThis.top is undefined.

However, AFAIK it still applies that service worker's context is not shared, i.e. it should be fine to initialize Sentry there.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 1, 2024
@github-actions github-actions bot added the Package: browser Issues related to the Sentry Browser SDK label Aug 1, 2024
@Lms24
Copy link
Member

Lms24 commented Aug 1, 2024

Hey @ddomonkos thanks for writing in!

However, AFAIK it still applies that service worker's context is not shared

Right, this is what we need to confirm. The serviceWorker's globalThis cannot be the same "instance" as window in the page. If they're not the same, Sentry.init along with global integratinos can be safely called.

Do you know if there's a unique property on the globalThis object of the service worker we can use to identify it?

@ddomonkos
Copy link
Author

Hi @Lms24,

while I don't consider myself to be an expert in the service workers domain, I can observe that globalThis is an instance of ServiceWorkerGlobalScope on which you can find a serviceWorker property. I suppose we could check for the existence of that.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 1, 2024
@Lms24
Copy link
Member

Lms24 commented Aug 2, 2024

Hey @ddomonkos I relaxed this check a bit in general in #13156 so that global scopes that are not the window global scope are generally exempted. For now I think this should fix your issue as well without special casing service workers. We'll merge this soon and include it in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK Type: Bug
Projects
Archived in project
2 participants