简体   繁体   English

工作区版本未生成服务工作者

[英]Workbox build not generating service worker

I am trying to integrate workbox-build into my grunt build system for pre-caching of files. 我正在尝试将workbox-build集成到我的grunt构建系统中,以预缓存文件。 I am following this article - generateSW Mode 我正在关注这篇文章-generateSW模式

After following the above google documentation i have made a function definition as 按照上述谷歌文档后,我做了一个函数定义为

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

The above swPath is a relative path to my grunt file. 上面的swPath是我的grunt文件的相对路径。 Below i am attaching the code for implementing grunt task of the above function 下面我附上实现上述功能的grunt任务的代码

 grunt.task.registerTask('generateSWCache', function(){ swCache(); }); if (env === 'production') { build = preBuild.concat(productionBuild).concat('generateSWCache'); } else { build = preBuild.concat(developmentBuild).concat('generateSWCache'); } 

The task generateSWCache is running but it is not producing any sw_cache.js for pre-caching of files. 任务generateSWCache正在运行,但不会生成任何sw_cache.js用于文件的预缓存。 Below is a screenshot 以下是屏幕截图 在此处输入图片说明

generateSWCache is an async task, so you should use Grunt's this.async function. 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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM