我试图在服务人员 中使用提取事件,但他没有为我工作。取不工作的服务人员
self.addEventListener('activate', function(event) {
console.log('Activated', event);
fetch('/embed_testing/notification/main.js').then(response =>
function() {
console.log(response)
});
});
self.addEventListener('fetch', function(event) {
console.log("heyyyo");
if (event.request.url === new URL('/', location).href) {
event.respondWith(
new Response("<h1>Hello!</h1>", {
headers: {'Content-Type': 'text/html'}
})
)
}
});
任何想法,为什么它不工作?
你可以给我一个示例如何使用服务工作人员抓取js在我的项目中? – user6609184
我不确定你的意思?如果我不得不猜测,也许你在谈论如何将外部JavaScript文件导入服务工作者,以便您可以在全局范围内使用它?如果是这样的话,那么你想使用['importScripts()'](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts)方法,它将同步请求和评估一个外部脚本。 –