Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request fails and the user has to try again.
The Background Sync API helps solve this problem by allowing a web application to postpone certain network requests until the device has a connection again.
Background Sync is a browser API that works with Service Workers.
Instead of sending a request immediately and failing when the network is unavailable, your application can register a sync task. The browser can then retry that task when connectivity returns.
The basic flow is:
User action → Service Worker → Request queued → Internet returns → Request processed
This is particularly useful for Progressive Web Apps (PWAs).
First, your application needs a Service Worker.
navigator.serviceWorker.ready.then(async (registration) => {
await registration.sync.register("send-message");
}); Here, "send-message" is a tag identifying the background synchronization task.
The Service Worker listens for the sync event:
self.addEventListener("sync", (event) => {
if (event.tag === "send-message") {
event.waitUntil(sendMessage());
}
}); You can then perform the network operation:
async function sendMessage() {
const response = await fetch("/api/messages", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
message: "Hello from the background!"
})
});
if (!response.ok) {
throw new Error("Failed to send message");
}
} Users can continue interacting with your application even when their connection temporarily disappears.
Instead of displaying an immediate network error, your application can attempt the operation later.
Background Sync is especially valuable for applications designed to work in unreliable or intermittent network environments.
Operations can be retried when the browser determines that network connectivity is available.
With a normal request:
User action
↓
fetch()
↓
Network unavailable
↓
Request fails With Background Sync:
User action
↓
Store request/data
↓
Register sync
↓
Network unavailable
↓
Browser waits
↓
Connection returns
↓
Service Worker runs
↓
Request is sent Background Sync does not magically remember arbitrary requests.
If you need to retry data later, you typically store the relevant information locally, commonly using IndexedDB.
For example:
const requestData = {
name: "John",
message: "Hello"
};
// Store requestData in IndexedDB
// Register a background sync task The Service Worker can retrieve the stored data when synchronization occurs.
The example above uses one-time Background Sync.
registration.sync.register("upload-data"); The browser attempts to trigger the corresponding sync event when appropriate.
This is different from continuously running background jobs.
There is also Periodic Background Sync, which is designed for periodically updating application content.
Conceptually:
Every so often
↓
Browser wakes Service Worker
↓
Fetch updated content
↓
Store/update cached data However, periodic background execution has additional browser and permission constraints and should not be treated as a guaranteed timer.
Background Sync has several limitations:
Consider an offline note-taking PWA.
A user writes:
Meeting starts at 10:00 AM. They press Save, but their connection is unavailable.
Your application can:
This creates a much more resilient offline experience.
The Background Sync API isn’t about making the internet work offline.
It’s about allowing your application to delay network-dependent work until a better opportunity to perform it becomes available.
For developers building PWAs and offline-first applications, it can be a powerful part of a broader architecture involving:
Service Workers + IndexedDB + Cache API + Background Sync
That combination can turn a web application from something that simply fails when the network disappears into an application that can gracefully handle unreliable connectivity.
Latest tech news and coding tips.
Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…
For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…
Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT. A Git merge…
Spring Boot is one of the most popular frameworks for building REST APIs in Java.…
Why borrowing code is a skill—when you understand what you're copying. For years, "copy-paste developer"…
Interactive forms rarely keep every field active all the time. Sometimes an input should only…