Service-Worker, “TypeError:Request failed at

前端 未结 3 1759
失恋的感觉
失恋的感觉 2021-02-20 03:41

I hope you can help me with my problem. Currently I build a PWA with a service-worker. It registerd successful, but something is wrong with the installation.

The \"cach

3条回答
  •  [愿得一人]
    2021-02-20 04:11

    This happens when your resourcesToCache includes something that returns a 404 response. Make sure you have everything typed correctly. Also make sure that the scope is correct. You can check your worker scope using:

    if("serviceWorker" in navigator) {
      navigator.serviceWorker
      .register(`worker.js`)
      .then(registration => {
        console.log("SW scope:", registration.scope);
      });
    }
    

    If your project is not in your server domain root, doing something like this might help:

    //your main js
    if("serviceWorker" in navigator) {
      navigator.serviceWorker
      .register(`${window.location.pathname}worker.js`)
      .then(registration => {
        //do your thing
      });
    }
    
    
    //your worker js
    let resourcesToCache = [
      './',
      './index.html',
      './jquery-3.2.1.min.js',
      './pouchdb.min-6.4.1.js',
      './styles/inline.css',
      './scripts/app.js',
    ];
    //...
    

    As a side-note, you should be loading your libraries (jQuery, pouchdb), from a CDN to improve performance. Those can be cached too:

    let resourcesToCache = [
      './',
      './index.html',
      'https://code.jquery.com/jquery-3.3.1.min.js',
      'https://cdnjs.cloudflare.com/ajax/libs/pouchdb/6.4.3/pouchdb.min.js',
      './styles/inline.css',
      './scripts/app.js',
    ];
    

提交回复
热议问题