Failed doing this thing console.log('window...', window) in file sw.js that generated by workbox v3.2.0.
It always return undefined.
Here my script to register my sw.js:
(function () {
'use strict'
var workerScript = document.currentScript.dataset.serviceWorker
if (workerScript && 'serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register(`${workerScript}?${Math.random()}`)
})
}
})()
which is coming from index.html
<script src="/sw/companion.js" data-service-worker="/sw/sw.js"></script>
is there any wrong from my script ?
Seems this is about WebWorker.
Found the answer here: https://stackoverflow.com/questions/11219775/global-variable-in-web-worker?rq=1
Change window to self.
Most helpful comment
Seems this is about WebWorker.
Found the answer here: https://stackoverflow.com/questions/11219775/global-variable-in-web-worker?rq=1
Change
windowtoself.