Workbox: Cannot access window in sw.js

Created on 14 May 2018  路  1Comment  路  Source: GoogleChrome/workbox

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 ?

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 window to self.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings