小编Dia*_*sta的帖子

使用 Webpack 时如何在 Service Worker 中导入脚本

在 Service Worker 中,我尝试使用 importScripts 导入另一个帮助程序脚本,但是我不断收到 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:5000/src/js/utility.js' failed to load.

我在 Service Worker 中有以下代码:

importScripts('../src/js/utility.js');

workbox.routing.registerRoute(/.*(?:jsonplaceholder\.typicode)\.com.*$/, function(args){
    console.log('Json placeholder being called');
    // Send request to the server.
    return fetch(args.event.request)
        .then(function(res){
            // Clone the response obtained from the server.
            var clonedRes = res.clone();
            // Clear data stored in the posts store.
            clearAllData('posts') // Function from helper file
                .then(function(){
                    return clonedRes.json()
                })
                .then(function(data){
                    for(var key in data){
                        // Write the updated …
Run Code Online (Sandbox Code Playgroud)

javascript webpack service-worker progressive-web-apps workbox-webpack-plugin

6
推荐指数
1
解决办法
7626
查看次数