Workbox背景同步CORS

时间:2018-09-07 03:03:31

标签: cors service-worker progressive-web-apps workbox background-sync

我正在使用background sync plugin使用Workbox。不幸的是,该插件未按我的应用程序的预期运行,这取决于CORS请求。

我的应用程序首先发送一个选项预检请求,以确保允许该请求,然后发送实际请求。

问题是当我脱机时,预检请求失败并崩溃了我的应用程序,然后我的实际请求才有机会保存到IndexDb中并稍后重播。

我该如何解决?

我目前正在按照文档中的建议配置后台同步,但是具有跨域端点,例如:

    const bgSyncPlugin = new workbox.backgroundSync.Plugin('myQueueName', {
      maxRetentionTime: 24 * 60 // Retry for max of 24 Hours
    });

    workbox.routing.registerRoute(
      new RegExp("https://my-api.example.com/api/add"),
      workbox.strategies.networkOnly({
        plugins: [bgSyncPlugin]
      }),
      'POST'
    );

从这里获取:https://developers.google.com/web/tools/workbox/modules/workbox-background-sync

我想知道是否也应该将OPTIONS请求添加到IndexDB吗?

    workbox.routing.registerRoute(
      new RegExp("https://my-api.example.com/api/add"),
      workbox.strategies.networkOnly({
        plugins: [bgSyncPlugin]
      }),
      'OPTIONS'
    );

任何帮助将不胜感激:)

0 个答案:

没有答案