79154869

Date: 2024-11-04 09:33:11
Score: 1
Natty:
Report link

Found a fix that fits my requirements. Not really sure if it s the perfect one, but for the moment it does what I am looking for.

I have created a new Queue with forceSyncFallback: true, so no auto sync, and when a request with status >= 400 happens, it goes to the newly created queue as it follows

const failedPostQueue = new Queue('failed-post-requests', {
  forceSyncFallback: true,
})

const postSyncPlugin = new BackgroundSyncPlugin('post-requests', {
  maxRetentionTime: 24 * 60,
  onSync: async ({queue}) => {
    let entry
    while ((entry = await queue.shiftRequest())) {
      try {
        const response = await fetch(entry.request.clone())

        if (response.status >= 400) {
          await failedPostQueue.pushRequest({
            request: entry.request.clone(),
            metadata: {
              response: await response.json(),
            },
          })
        }
      } catch (error) {
        await queue.unshiftRequest(entry)
      }
    }
  },
})

It is still work in progress, but looks good for the moment. If there is a better approach I would be really happy to try it.

Reasons:
  • Blacklisted phrase (2): I am looking for
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: stefanz