首页 > 解决方案 > Workbox 预缓存:如果某些 URL 失败,不要失败

问题描述

我正在尝试使用带有工作箱和 gulp 的下一个代码生成 SW

gulp.task('generate-service-worker', () => {
    return workbox.generateSW({
        globDirectory: root,
        globPatterns: [
          '**/!(*.map|*.html|*.txt|*.orig)'
        ],
        swDest: `${root}/javascripts/sw/sw.js`,
        clientsClaim: true,
        skipWaiting: true
    }).then(({warnings, count, size}) => {
        //In case there are any warnings from workbox-build, log them.
        for (const warning of warnings) {
            console.warn(warning);
        }
        console.info(`Service worker generation completed.Count: ${count}, size: ${size}`);
    }).catch((error) => {
        console.warn('Service worker generation failed:', error);
    });
});

生成服务工作者。但是,如果在安装期间其中一个 URL 失败,则整个安装过程将失败,并且新的 service worker 不会被激活。是否可以忽略获取失败并继续使用剩余 URL 的预缓存?

标签: service-workerworkbox

解决方案


推荐阅读