【问题标题】:Workbox build not generating service worker工作箱构建不生成服务工作者
【发布时间】:2018-12-04 10:36:39
【问题描述】:

我正在尝试将 workbox-build 集成到我的 grunt 构建系统中以预缓存文件。 我正在关注这篇文章-generateSW Mode

按照上述谷歌文档后,我将函数定义为

var workBox = require('workbox-build');

function swCache(){
    workBox.generateSW({
        swPath : path.join('target/app', 'sw_cache.js')
    })
    .then(function(details){
        console.log(details);
    })
}

上面的 swPath 是我的 grunt 文件的相对路径。 下面我附上实现上述功能的咕噜任务的代码

grunt.task.registerTask('generateSWCache', function(){
        swCache();
    });

    if (env === 'production') {
        build = preBuild.concat(productionBuild).concat('generateSWCache');
    } else {
        build = preBuild.concat(developmentBuild).concat('generateSWCache');
    }

任务 generateSWCache 正在运行,但它没有生成任何用于预缓存文件的 sw_cache.js。 下面是截图

【问题讨论】:

    标签: javascript gruntjs service-worker workbox


    【解决方案1】:

    generateSWCache 是一个异步任务,所以你应该使用 Grunt 的 this.async 函数。

    var workBox = require('workbox-build');
    
    function swCache(done) {
        workBox.generateSW({
            swPath : path.join('target/app', 'sw_cache.js')
        })
        .then(function(details){
            console.log(details);
            done(true);
        })
        .catch(function (err) {
            console.log(err);
            done(false);
        });
    }
    
    grunt.task.registerTask('generateSWCache', function(){
        swCache(this.async());
    });
    
    if (env === 'production') {
        build = preBuild.concat(productionBuild).concat('generateSWCache');
    } else {
        build = preBuild.concat(developmentBuild).concat('generateSWCache');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2016-07-28
      • 2020-07-02
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多