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.