debug
debug copied to clipboard
Doc suggestion: Using `debug` in WebWorkers / SharedWorkers
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!
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
})
}
})