获取脚本时发生未知错误(Service Worker) [英] An unknown error occurred when fetching the script (Service Worker)

查看:493
本文介绍了获取脚本时发生未知错误(Service Worker)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当离线时,我的服务工作人员收到以下错误:

When going offline, I get the following error by my service worker:

(未知)#3016获取时出现未知错误脚本

我的服务人员看起来像这样:

my service worker looks like this:

var version = 'v1'

this.addEventListener('install', function(event){
  event.waitUntil(
     caches.open(version).then(cache => {
       return cache.addAll([
         'https://fonts.googleapis.com/icon?family=Material+Icons',
         'https://fonts.googleapis.com/css?family=Open+Sans:400,600,300',
         './index.html'
       ])
     })
   )
})

this.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(resp) {
      // if it's not in the cache, server the regular network request. And save it to the cache
      return resp || fetch(event.request).then(function(response) {
        return caches.open(version).then(function(cache) {
          cache.put(event.request, response.clone())
          return response
        })
      })
    })
  )
})

它位于顶级目录,紧邻index.html中的清单导入:

It is at top level directory, right next to a manifest importing like this in index.html:

< link rel = manifesthref =/ manifest.json>

我在我的条目js文件中导入服务工作者。并立即注册。

I import the service worker in my entry js file. And register it right after.

require('tether-manifest.json')
import serviceWorker from 'sw'

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register(serviceWorker)
  .then(() => {
    // registration worked
  }).catch(error => {
    throw new Error(error)
  })
}

它注册正常。在离线之前我没有遇到错误。

It registers fine. I don't encounter the error until I go offline.

我正在使用带有React的webpack,并在webpack中执行以下操作将我的sw.js文件复制到dist文件夹:

I am using webpack with React, and doing the following in webpack to copy my sw.js file to the dist folder:

loaders: [
      { // Service worker
        test: /sw\.js$/,
        include: config.src,
        loader: 'file?name=[name].[ext]'
      },
      { // Manifest
        test: /manifest\.json$/,
        include: config.src,
        loader: 'file?name=[name].[ext]'
      }
]

错误没有提供任何有关正在发生的事情的信息。

The error doesn't give any information as to what is happening.

任何人都知道如何解决这个问题?

Anyone have any idea how to fix this?

推荐答案

我有完全相同的问题,我花了一个小时试图解决它,事实证明我有另一个选项卡为同一个来源左边打开(所以使用相同的共享服务工作者)进行了离线检查框已经选中,因为某些原因阻止其他标签请求 sw.js

I had exactly the same problem, I spent an hour trying to figure it out and it turned out that I had another tab for the same origin left opened somewhere (so using the same shared Service Worker) that had the "Offline" checkbox left checked and that prevented another tabs from requesting sw.js for some reason.

似乎离线状态从服务工作者范围内的选项卡泄漏,而没有被其他选项卡正确反映或管理,而不是首先处于脱机状态的选项卡。

It seems that the offline state is leaking from the tab within Service Worker scope while not being properly reflected nor managed by the other tabs than the one that was turned Offline first.

所以请确保你有没有其他客户端运行相同的Service Worker - 您应该能够在DevTools> Application> Service Workers中找到它们。

So make sure you have no other clients running the same Service Worker - you should be able to find them listed in DevTools > Application > Service Workers.

这篇关于获取脚本时发生未知错误(Service Worker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆