gpt4 book ai didi

service-worker - 服务 worker : when does the browser sync back again?

转载 作者:行者123 更新时间:2023-12-04 06:48:14 24 4
gpt4 key购买 nike

我正在使用 Google Workbox 构建一个渐进式 Web 应用程序。我已经使用 bgSync(在我的构建文件夹中)设置了一个 Service Worker,以便我的 POST 请求被推送到一个队列并在用户重新连接时发送到我的端点,但是同步事件究竟何时发生?

出于开发目的,我使用 Chrome 中包含的“同步”按钮,如以下答案所述:How to manually trigger a background sync (for testing)? , 但这是手动的,我希望在应用程序重新联机后立即将请求发送到端点,但是当我获得连接时,我的请求未发送,我必须手动单击“同步”按钮发生,它确实工作得很好,但用户不会在真实场景中点击同步按钮。

准确地说,我想知道在我的服务 worker 内部是否有一种方法可以检测应用程序何时重新联机并强制同步。或者知道同步发生的时间。这是一个供引用的片段(使用 Workbox 3.0.0):

const bgSyncPlugin = new workbox.backgroundSync.Plugin('myQueue', {
callbacks: {
requestWillEnqueue: (storableRequest) => {}, // I show a push notification indicating user is offline
requestWillReplay: (storableRequest) => {},
queueDidReplay: (storableRequestArray) => {} // If I get a response, I show a push notification
}
},
);

workbox.routing.registerRoute(
"https://myapi.com/action",
workbox.strategies.networkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);

最佳答案

因此,截至今天,Chrome 中的后台同步尝试推送排队的请求 3 次(从未在工作箱或后台同步文档中解释过):

  • 第一次:当请求第一次真正尝试并且浏览器意识到用户没有连接时,所以将请求放入 indexedDB 中。
  • 第二次:第一次尝试后正好 5 分钟。
  • 第三次:第一次尝试后正好 15 分钟。

  • 如果用户在 15 分钟后没有连接,那么请求只会卡在 indexedDB 中,直到新的排队请求尝试推送其余请求。当我们期望用户数小时没有互联网连接时,这不是很有帮助。

    有计划(自 2016 年以来!)实现 PeriodicSync,这将让开发人员选择浏览器尝试同步的次数和时间,但从未真正实现,请参阅: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/periodicSync

    不过,我想出了一个笨拙的解决方案,可能不是最好的解决方案,但它可以满足我的需求。有一个同步事件,我们可以操纵它来重试我们卡在 indexedDB 中的请求。不过,我们需要使用 Workbox Queue 类而不是 Plugin。
    // our service worker file
    // we create a push notification function so the user knows when the requests are being synced

    const notificate = (title, message) => {
    self.registration.showNotification(title, {
    body: message,
    icon: '/image.png',
    tag: 'service-worker'
    })
    }

    // let's create our queue
    const queue = new workbox.backgroundSync.Queue('myQueue', {
    callbacks: {
    requestWillEnqueue: () => {
    notificate('You are offline! 🛠', 'Your request has been submitted to the Offline
    queue. The queue will sync with the server once you are back online.')
    }
    });

    // sync event handler
    self.addEventListener("sync", (ev) => {
    queue.replayRequests().then((a) => {
    notificate('Syncing Application... 💾', 'Any pending requests will be sent to the
    server.');
    }).catch(
    notificate('We could not submit your requests. ❌', 'Please hit the \'Sync Pending
    Requests\' button when you regain internet connection.')
    );
    });

    现在在我们的 HTML/React/Node View 文件中,我们可以执行以下操作:
      // this will trigger our Sync event
    <button type="button" onClick={navigator.serviceWorker.ready.then(reg =>
    reg.sync.register('myEvent'))}>{'Sync Pending
    Requests'}</button>

    请注意,我创建了一个 html 按钮,强制我的服务 worker 运行 queue.replayRequests(),因此后台同步功能不会自动发生,我必须手动单击一个按钮才能发生。

    关于service-worker - 服务 worker : when does the browser sync back again?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53786395/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com