debug icon indicating copy to clipboard operation
debug copied to clipboard

Doc suggestion: Using `debug` in WebWorkers / SharedWorkers

Open pvh opened this issue 3 years ago • 1 comments

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!

pvh avatar Nov 10 '22 20:11 pvh

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
        })
      }
    })

SgtPooki avatar May 09 '23 00:05 SgtPooki