Workbox Background Sync CORS

Viewed 342

I'm using Workbox using the background sync plugin. Unfortunately the plugin did not run as expected for my application, which relies on a CORS request.

My app first sends an options preflight request to ensure that the request is allowed, and then the real request is sent.

The problem is that when i go offline, the preflight request fails and crashes my app before my actual request has a chance to be saved in IndexDb and replayed later.

How can i solve this?

I am currently configuring my background sync as suggested in the documentation, but with a cross-origin endpoint, like so:

    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'
    );

taken from here: https://developers.google.com/web/tools/workbox/modules/workbox-background-sync

I wonder if I should be adding the OPTIONS requests to IndexDB as well?

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

Any help would be appreciated :)

0 Answers
Related