我正在使用sw-precache,并且我知道为了编辑service-worker.js文件,我需要这样做(在service-worker.js文件中有详细介绍)
//应在构建过程中覆盖此文件。//如果您需要扩展生成的服务工作者的行为,//最好的方法是编写额外的代码并使用importScripts选项将其包含进来: // https://github.com/GoogleChrome/sw-precache#importscripts-arraystring
但是我不知道在哪里添加importscripts()代码。它是否包含在service-worker.js文件中?当然,这在每次项目构建时都会被覆盖。
发布于 2018-07-04 22:01:01
只需像这样包含它:
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);
}希望我能帮到你!
发布于 2017-09-11 20:33:04
将其添加到sw-precache config.js文件中。
https://stackoverflow.com/questions/46155080
复制相似问题