iOS PWA – After updating the service worker once, killing the app & restarting it, another copy of the active service worker is waiting

huangapple go评论69阅读模式
英文:

iOS PWA - After updating the service worker once, killing the app & restarting it, another copy of the active service worker is waiting

问题

抱歉,我只能翻译代码部分。以下是您提供的代码的中文翻译:

我有一个应用程序,注册了一个服务工作线程,并给用户触发服务工作线程更新的机会,当有一个新的服务工作线程等待时。在Android上一切似乎都运行正常,iOS上也运行得相当顺利。然而,在iOS的独立模式下,在进行了一次服务工作线程更新后,似乎应用程序会一遍又一遍地尝试安装相同的服务工作线程。

我使用运行iPadOS 16.4.1的iPad执行以下步骤以产生问题:

  1. 我安装了PWA,并且它正在运行服务工作线程A。
  2. 我部署了一个新的服务工作线程(服务工作线程B)。应用程序检测到服务工作线程有更新,并拉取了新的服务工作线程。一旦服务工作线程B达到“等待”状态,应用程序会弹出一个提示,允许我向服务工作线程B发送一个skipWaiting消息,我就这么做了。
  3. 服务工作线程B接管了。
  4. 我完全关闭并重新打开PWA。
  5. 应用程序正在运行服务工作线程B,并且没有部署新的服务工作线程,但是另一个服务工作线程B的副本以某种方式达到了等待状态,因此应用程序再次提示我触发更新,即使不应该需要更新。

我通过Safari的Web检查器确认了最初运行服务工作线程A,然后加载并激活了服务工作线程B,应用程序在被关闭然后重新启动后运行的是服务工作线程B,并且随后加载的服务工作线程是服务工作线程B的副本。

这是否是在苹果设备上运行的PWA的已知限制/怪异行为,还是听起来像是我的问题出了什么差错?

当我在PWA仍在运行服务工作线程A的同时将其关闭时,我没有遇到相同的问题,而且还没有部署服务工作线程B(只有在我至少更新了一次服务工作线程后才会出现问题)。所以我目前的猜测是,在iOS上,PWA保留了关于安装时使用的原始服务工作线程的信息,并将其与查找更新时的当前活动服务工作线程进行比较,而不是与最初的服务工作线程进行比较。然而,我还没有找到支持这个理论的任何文档。

英文:

I have an app that registers a service worker and gives the user the chance to trigger a service worker update when a new service worker is waiting. Everything seems to be functioning well on Android, and it's also functioning fairly well on iOS. However, in standalone mode on iOS, after making one service worker update, it seems like the app keeps trying to install the same service worker over and over again.

Using an iPad running iPadOS 16.4.1, I take the following steps to produce the issue:

  1. I install the PWA, and it is running service worker A.
  2. I deploy a new service worker (service worker B). The app detects that there is an update to the service worker and pulls the new one. Once service worker B reaches a state of "waiting," the app presents a prompt that allows me to post a skipWaiting message to service worker B, and I do so.
  3. Service worker B takes over.
  4. I completely kill and reopen the PWA.
  5. The app is running service worker B, and no new service worker has been deployed, but another copy of service worker B has somehow reached the waiting state, so the app, checking for any service workers in the waiting state and finding one, again prompts me to trigger an update even though one shouldn't be needed.

I've confirmed via Safari's Web Inspector that service worker A is running initially, that service worker B is then loaded and made active, that it is service worker B that's running after the app is killed and then restarted, and that the subsequently loaded service worker is a copy of service worker B.

Is this a known limitation/quirk of PWAs running on Apple devices, or does it sound like something's gone amiss on my end to cause it?

I'm not running into the same issue when I kill the PWA while it's still running service worker A and service worker B hasn't been deployed (it's only after I've updated the service worker at least once that the issue crops up). So my current guess is that perhaps on iOS the PWA retains information about the original service worker it was installed with and compares against that original service worker rather than the currently active service worker when looking for updates. I haven't been able to find any documentation that supports that theory yet though.

答案1

得分: 2

我在 iOS 上也遇到类似情况,而在 Android 上则没有。有时候当我更新服务工作线程时,会不断触发更新操作的提示,即使服务工作线程已经更新并正在运行。

但通常情况下,这种情况很快就会停止,可能只会在下一次更新时再次出现,而且也只是短暂的。

很遗憾,我没有解决方案提供,但至少你知道你并非唯一遇到这个问题的人 iOS PWA – After updating the service worker once, killing the app & restarting it, another copy of the active service worker is waiting

英文:

I do have a similar situation with my PWA on iOS (not on Android) where sometimes when I update the service worker, the prompt to update action will trigger again and again even if the sw is already updated and running.

But usually that stop to be the case a moment later, and might come back at the next update only, again just for a moment.

Sadly I dont have a solution to offer, but at least you know you're not alone with the issue iOS PWA – After updating the service worker once, killing the app & restarting it, another copy of the active service worker is waiting

答案2

得分: 2

我不完全确定为什么这样解决了问题,但以防对他人有所帮助:

我一直在定时调用服务工作者的registration.update()方法以检查更新,最初我是在我的服务工作者代码内部执行这个操作的。将这个逻辑简单地移到应用程序代码中,并在那里调用registration.update()而不是在服务工作者中调用,最终解决了我的问题。由于在Android上两种方式都能正常工作,而且更新方法的文档指出,一个字节相同的服务工作者不应该被安装为一个新的服务工作者,我怀疑这只是一个iOS的怪癖。

英文:

I'm not entirely sure why this fixed the issue, but in case it helps others:

I was making a call to the service worker's registration.update() method on a set interval to check for updates, and originally I was doing so within my service worker's code. Simply moving that logic to the app code and making the registration.update() call from there instead of from the service worker ended up fixing the problem for me. Since it works just fine on Android either way and since the documentation for the update method indicates that a byte-by-byte identical service worker shouldn't be installed as a new service worker, I suspect this is just an iOS quirk.

huangapple
  • 本文由 发表于 2023年4月11日 05:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定