mardi 30 janvier 2018

Code breaks when running as a service worker

I have two js files - one with some vendor code (jQuery, medium-zoom.js, and prism.js), and another with some really minimal custom code, and I'm trying to save them with a service worker.

(function () {
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {  

    navigator.serviceWorker.register('/js/vendor.js').then((registration) => {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, (err) => {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });

    navigator.serviceWorker.register('/js/scripts.js').then((registration) => {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, (err) => {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
});

The error I'm seeing

vendor.js:2 Uncaught TypeError: Cannot read property 'createElement' of undefined
sw.js:12 ServiceWorker registration failed:  TypeError: Failed to register a ServiceWorker: ServiceWorker script evaluation failed
sw.js:17 ServiceWorker registration successful with scope:  http://localhost:3000/js/

Essentially the code works when it's not attempting to be a service worker - but the moment it is - I'm getting errors like Uncaught TypeError: Cannot read property 'createElement' of undefined.

I thought that it might be due to one file working off of another (MediumZoom) as it is built in one file, and init in the other - but even when I put both files into one, I still ran into the exact same issue.

Is there something I'm missing?

Thanks




Aucun commentaire:

Enregistrer un commentaire