【问题标题】:Make Changes To sw-precache service-worker对 sw-precache service-worker 进行更改
【发布时间】:2017-09-11 11:50:41
【问题描述】:

我正在使用 sw-precache,我知道为了编辑 service-worker.js 文件,我需要这样做(详见 service-worker.js 文件)

// 这个文件应该作为构建过程的一部分被覆盖。 // 如果需要扩展生成的 service worker 的行为,最好的办法是写 // 附加代码并使用 importScripts 选项包含它: //https://github.com/GoogleChrome/sw-precache#importscripts-arraystring

但我不知道在哪里添加 importscripts() 代码。它是否在 service-worker.js 文件中?当然,这在每个项目构建中都会被覆盖。

【问题讨论】:

    标签: service-worker sw-precache


    【解决方案1】:

    像这样包含它:

    importScripts: ['custom-offline-import.js']
    

    这是一个配置示例,最后包含 importScripts 配置选项:

    var packageJson = require('../package.json');
    var swPrecache = require('../lib/sw-precache.js');
    var path = require('path');
    
    function writeServiceWorkerFile(rootDir, handleFetch, callback) {
    
      var config = {
        cacheId: packageJson.name,
        runtimeCaching: [{
          // See https://github.com/GoogleChrome/sw-toolbox#methods
          urlPattern: /runtime-caching/,
          handler: 'cacheFirst'
        }],
        staticFileGlobs: [rootDir + '/**/*.{js,html,css,png,jpg,gif}'],
        stripPrefix: rootDir,
        importScripts: ['custom-offline-import.js']
      };
    
      swPrecache.write(path.join(rootDir, 'service-worker.js'), config, callback);
    }
    

    希望能帮到你!

    【讨论】:

      【解决方案2】:

      将其添加到 sw-precache-config.js 文件中。

      这里的好例子: Rewrite URL offline when using a service worker

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-15
        • 2018-05-26
        相关资源
        最近更新 更多