Course contentsShow
← Web Development
Lesson 2329 of 2,98242. Progressive Web AppsFree lesson

Queue Failed Requests

Implement a queue system to store failed requests and retry them automatically when connectivity returns.

Queue Failed Requests

What you'll learn: How to automatically store and retry failed network requests when a user's connection is restored, creating a seamless offline experience.

When users interact with your PWA while offline—submitting forms, posting comments, or updating data—those requests fail instantly. Rather than losing that data or frustrating users with error messages, you can queue these failed requests and automatically retry them when connectivity returns.

The Core Idea

Think of this like a post office holding mail during a postal strike. When someone tries to send a letter (make a request) but the service is down (offline), the post office keeps those letters in a queue. Once service resumes (online again), all queued letters are sent automatically in order.

How It Works

When a fetch request fails in your service worker's fetch event handler, catch the error and store the request details (URL, method, headers, body) in IndexedDB. Listen for the online event using the Detecting Online/Offline State techniques you learned, then retrieve and replay all queued requests.

Since you've already learned the Fetch Event and IndexedDB Inspection, you can intercept failed requests, serialize their details, and save them. When the Network Information API or online event signals connectivity, loop through your queue and retry each stored request.

Key Considerations

Queue management requires careful handling of request order, duplicate prevention, and retry limits. You should also provide UI feedback showing users which actions are pending sync.

Key Takeaway: A request queue transforms connection failures from data loss into delayed success, ensuring user actions persist even through network interruptions by storing failed requests and automatically retrying them when online.