<i id='sq4gF'><tr id='sq4gF'><dt id='sq4gF'><q id='sq4gF'><span id='sq4gF'><b id='sq4gF'><form id='sq4gF'><ins id='sq4gF'></ins><ul id='sq4gF'></ul><sub id='sq4gF'></sub></form><legend id='sq4gF'></legend><bdo id='sq4gF'><pre id='sq4gF'><center id='sq4gF'></center></pre></bdo></b><th id='sq4gF'></th></span></q></dt></tr></i><div id='sq4gF'><tfoot id='sq4gF'></tfoot><dl id='sq4gF'><fieldset id='sq4gF'></fieldset></dl></div>

        <bdo id='sq4gF'></bdo><ul id='sq4gF'></ul>

    1. <tfoot id='sq4gF'></tfoot>
    2. <small id='sq4gF'></small><noframes id='sq4gF'>

      <legend id='sq4gF'><style id='sq4gF'><dir id='sq4gF'><q id='sq4gF'></q></dir></style></legend>
    3. `self.Clients.claim()` 有什么用

      时间:2023-09-06

          <bdo id='Hf8fK'></bdo><ul id='Hf8fK'></ul>

          <small id='Hf8fK'></small><noframes id='Hf8fK'>

          <i id='Hf8fK'><tr id='Hf8fK'><dt id='Hf8fK'><q id='Hf8fK'><span id='Hf8fK'><b id='Hf8fK'><form id='Hf8fK'><ins id='Hf8fK'></ins><ul id='Hf8fK'></ul><sub id='Hf8fK'></sub></form><legend id='Hf8fK'></legend><bdo id='Hf8fK'><pre id='Hf8fK'><center id='Hf8fK'></center></pre></bdo></b><th id='Hf8fK'></th></span></q></dt></tr></i><div id='Hf8fK'><tfoot id='Hf8fK'></tfoot><dl id='Hf8fK'><fieldset id='Hf8fK'></fieldset></dl></div>

            <legend id='Hf8fK'><style id='Hf8fK'><dir id='Hf8fK'><q id='Hf8fK'></q></dir></style></legend>
            <tfoot id='Hf8fK'></tfoot>
                <tbody id='Hf8fK'></tbody>
              • 本文介绍了`self.Clients.claim()` 有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                要注册 Service Worker,我可以调用

                To register a service worker, I can call

                navigator.serviceWorker.register('/worker.js')
                

                每次页面加载时,它都会检查 worker.js 的更新版本.如果找到更新,则在关闭所有页面的选项卡然后重新打开之前,不会使用新的工作人员.我读到的解决方案是:

                Every time the page loads it checks for an updated version of worker.js. If an update is found, the new worker won't be used until all the page's tabs are closed and then re-opened. The solution I read was:

                self.addEventListener('install', function(event) {
                  event.waitUntil(self.skipWaiting());
                });
                self.addEventListener('activate', function(event) {
                  event.waitUntil(self.clients.claim());
                });
                

                我可以理解 skipWaiting 部分,但 clients.claim() 究竟是做什么的?我做了一些简单的测试,即使没有它,它似乎也能按预期工作.

                I can understand the skipWaiting part, but what exactly does clients.claim() do? I've done some simple tests and it seems to work as expected even without it.

                推荐答案

                我从 服务工作者生命周期指南:

                clients.claim

                您可以通过调用来控制不受控制的客户端clients.claim() 一旦它被激活就在你的 Service Worker 中.

                You can take control of uncontrolled clients by calling clients.claim() within your service worker once it's activated.

                这里是演示的变体,它调用了 clients.claim() 在它的激活事件.你应该第一次看到一只猫.我说应该",因为这是时间敏感的.你只会看到一只猫,如果service worker 激活并且 clients.claim() 在之前生效图片尝试加载.

                Here's a variation of the demo above which calls clients.claim() in its activate event. You should see a cat the first time. I say "should", because this is timing sensitive. You'll only see a cat if the service worker activates and clients.claim() takes effect before the image tries to load.

                如果您使用 Service Worker 加载页面的方式与他们不同通过网络加载,clients.claim() 可能会很麻烦,因为您的服务工作者最终控制了一些没有加载的客户端它.

                If you use your service worker to load pages differently than they'd load via the network, clients.claim() can be troublesome, as your service worker ends up controlling some clients that loaded without it.

                注意:我看到很多人包括 clients.claim() 作为样板,但我自己很少这样做.只在最初时才真正重要加载,并且由于渐进增强,页面通常可以正常工作无论如何,没有服务人员很高兴.

                Note: I see a lot of people including clients.claim() as boilerplate, but I rarely do so myself. It only really matters on the very first load, and due to progressive enhancement the page is usually working happily without service worker anyway.

                这篇关于`self.Clients.claim()` 有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 下一篇:同时针对同一事件的两个 ajax 请求.典型的行为应该是什么?如果请求是同步的,会有什么不同

                相关文章

                <small id='Wx7VH'></small><noframes id='Wx7VH'>

                  <bdo id='Wx7VH'></bdo><ul id='Wx7VH'></ul>

                1. <tfoot id='Wx7VH'></tfoot>
                2. <legend id='Wx7VH'><style id='Wx7VH'><dir id='Wx7VH'><q id='Wx7VH'></q></dir></style></legend>
                3. <i id='Wx7VH'><tr id='Wx7VH'><dt id='Wx7VH'><q id='Wx7VH'><span id='Wx7VH'><b id='Wx7VH'><form id='Wx7VH'><ins id='Wx7VH'></ins><ul id='Wx7VH'></ul><sub id='Wx7VH'></sub></form><legend id='Wx7VH'></legend><bdo id='Wx7VH'><pre id='Wx7VH'><center id='Wx7VH'></center></pre></bdo></b><th id='Wx7VH'></th></span></q></dt></tr></i><div id='Wx7VH'><tfoot id='Wx7VH'></tfoot><dl id='Wx7VH'><fieldset id='Wx7VH'></fieldset></dl></div>