Background Sync API: Send Data When the Network Returns

The Background Sync API lets your app defer work until the network is back. You register a task, and the browser wakes your service worker to run it when connectivity returns. Use it to reliably send data submitted while offline, like form posts or chat.
Why it exists
Web apps need to work reliably even with intermittent network connections. Before this API, if a user submitted a form while offline, the request would fail and the data could be lost. The Background Sync API was created to solve this by providing a standard way to queue work that will run automatically once the network is available.
The mental model
Think of it as a browser-managed "outbox" for your web app. Your application code drops a request into the outbox by registering a sync task with a unique tag. The browser, acting as the delivery service, waits for clear roads (a stable network connection) and then wakes up your service worker to process everything in the outbox.
How it works
The process has two parts. First, in your client-side JavaScript, you access the service worker registration and call registration.sync.register('my-task-tag'). This tells the browser you have work to be done later. Second, inside your service worker file, you add an event listener for the 'sync' event. When the browser detects a stable connection, it fires this event. Your listener code checks event.tag to identify the task and then executes the necessary logic, like sending a POST request to your server.
When to use it
Use it for non-urgent data that needs to eventually reach a server. It's perfect for an offline-first Progressive Web App (PWA). Good use cases include sending chat messages, uploading small photos, submitting form data, or syncing application state that was changed while the user was offline.
When not to use it
Do not use it for operations that require immediate feedback, like a user login or a password reset. The browser controls when the sync event fires, so there's no guarantee of immediacy. For large file uploads or downloads, the Background Fetch API is a better choice as it's designed specifically for that purpose.
One canonical example
An email client PWA lets a user compose and hit "send" on a message while on an airplane's spotty Wi-Fi. The app UI can immediately move the message to the "Outbox". In the background, the app calls navigator.serviceWorker.ready.then(reg => reg.sync.register('send-queued-emails')). Later, once the connection is stable, the browser wakes the service worker, which receives the 'sync' event, finds all emails pending in IndexedDB, and sends them to the server.
Interview question
What is the fundamental mechanism by which the Background Sync API ensures data submitted offline eventually reaches the server?
- a.It provides a real-time, persistent WebSocket connection that buffers data during offline periods.
- b.It allows the client-side JavaScript to continuously poll the network status and resend data when online.
- c.It registers a task with the browser that wakes the service worker to send data once a stable network connection is available.Correct
- d.It automatically stores all failed network requests in IndexedDB for manual re-submission.
Why? this is the answer
The Background Sync API's core mechanism involves registering a task with the browser, which then takes responsibility for waking the service worker to execute that task when a stable network connection is detected. This differs from client-side polling (option B) or manual re-submission (option D), and it does not use WebSockets (option A).
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #web apis
- #pwa
- #service worker
- #offline
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on web apis — each one lists the topics its interview covers.
See open roles