在 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 data to the posts store.
writeData('posts', data[key]) // Function from helper file
}
});
return res;
})
});
workbox.precaching.precacheAndRoute(self.__precacheManifest);
在utility.js
我有以下代码:
import { openDB } from 'idb';
export function writeData(st, data){
console.log(st, data);
}
export function clearAllData(st){
console.log(st);
}
这些功能还没有做任何事情,但即使是这些占位符也不起作用!最终我希望能够使用idb
npm 模块,这就是我在帮助程序中这样做的原因,所以我也可以从我的普通 Javascript 文件中使用它。
另外我正在使用 Webpack 来构建我的文件,在另一个我不使用它的项目中,它工作正常,但是在这个项目中它只是在构建后找不到文件,所以我想 Webpack 可能是搞砸了。
提前致谢 :)