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

Doc suggestion: Using debug in WebWorkers / SharedWorkers #916

Open
pvh opened this issue Nov 10, 2022 · 1 comment
Open

Doc suggestion: Using debug in WebWorkers / SharedWorkers #916

pvh opened this issue Nov 10, 2022 · 1 comment

Comments

@pvh
Copy link

pvh commented Nov 10, 2022

Just a quick note that localStorage isn't available in web workers. The dynamic approach described under "Set dynamically" works fine in this environment but it might be worth adding a sentence or two in the "Browser Support" section of the docs to hint people in the right direction.

Thanks for a helpful library!

@SgtPooki
Copy link

SgtPooki commented May 9, 2023

FYI for others coming here, you can get around this by doing:

// sw.ts
import debug from 'debug'

globalThis.addEventListener('message', event => {
  if (event.data && event.data.type === 'DEBUG_JS_ENABLE') {
    debug.enable(event.data.debugString)
  }
})

// app.ts
    navigator.serviceWorker.ready.then((registration) => {
      const debugString = localStorage.getItem('debug')
      if (debugString) {
        registration.active?.postMessage({
          type: 'DEBUG_JS_ENABLE',
          debugString
        })
      }
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants