是否有Service Worker启动waitUntil来延迟处理获取? [英] Is there a Service Worker startup waitUntil to delay processing fetch?

查看:341
本文介绍了是否有Service Worker启动waitUntil来延迟处理获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能让服务人员等待开始处理获取事件,直到服务人员启动时异步工作完成?

Is it possible to get service workers to wait to begin processing fetch events until asynchronous work completes at service worker startup?

我有一个应用程序外壳,其中包含在数据中定义的路由.要在服务工作者启动时安装特定的路由提取处理程序,我需要从IndexedDB(异步)中查找路由数据.

I have an app shell with routes defined in data. To install specific route fetch handlers at service worker startup, I need to lookup route data from IndexedDB (asynchronous).

不幸的是,服务工作者在IndexedDB查找完成并设置路由的获取处理之前开始接受获取事件.

Unfortunately, the service worker starts accepting fetch events before the IndexedDB lookup can complete and setup fetch handling for routes.

就目前而言,我只是为此硬编码一个特殊情况的默认处理程序,但是让服务工作者延迟处理获取事件,直到在服务工作者启动时完成IndexedDB处理为止,这将是很棒的事情.

For now, I'm just hardcoding a special case default handler for this, but it would be great to get the service worker to just delay processing fetch events until the IndexedDB processing is complete at service worker startup.

我没有找到"waitUntil"的方法,也许我错过了吗?

I didn't see a way to "waitUntil" for this, maybe I missed it?

推荐答案

有一种解决方法:

function startupAsyncStuff() {
  return new Promise(function (fulfill, reject) {
    // put here your async stuff and fulfill the promise when done.
  });
}

// Launch your startup async code
var asyncStuffDone = startupAsyncStuff();

// On fetch, wait for the former promise to be resolved first
self.onfetch = function (event) {
  event.respondWith(asyncStuffDone.then(function () {
    // your fetch handling code
  }));
};

这篇关于是否有Service Worker启动waitUntil来延迟处理获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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