Send forms filled offline later with background sync
Short answer: If people fill in your web forms where the signal drops, such as farm visits, building sites or event halls, a normal form submission simply fails and the work can be lost. The fix is to save each submission on the device first, then send it when the connection comes back. The Background Synchronization API lets a service worker do that sending automatically, even after the page is closed, in browsers that support it. Because support isn't universal, pair it with a simple fallback that retries when the page is next open and online.
The problem with ordinary forms offline
A standard form posts straight to your server. With no connection, the browser shows an error page, and depending on the browser and the form, what the person typed may be gone. Someone who has just spent ten minutes on an inspection checklist in a barn with one bar of signal won't do it twice.
Offline-friendly forms change the order of events: save first, send second. The person sees "Saved, will send when you're back online", and the app takes responsibility for delivery.
What background sync does
MDN describes the Background Synchronization API as a way for a web app to defer tasks to its service worker, to be run once the device has a stable network connection. Its example is an email client that lets people press send with no signal: the page registers a sync request, and the service worker is alerted when the network returns and sends the message.
The parts involved:
- A service worker, the background script that many web apps already use for caching. Background sync requires one, and like service workers it only works over HTTPS.
- SyncManager, available through
registration.sync, which you use to register a sync with a tag name such assend-forms. - The sync event, which fires in the service worker with that tag when the browser decides the connection is good enough.
MDN marks the API as not Baseline, because it doesn't work in some of the most widely used browsers. Plan for both cases from the start.
A practical design
1. Store the submission locally. When the person presses submit, write the form data to IndexedDB, the browser's database for structured data, which MDN notes can also hold files and blobs such as photos. Give each submission a unique ID and a status of "pending".
2. Try to send immediately. If the device is online, send straight away and mark it as sent on success. Many submissions never need the offline path.
3. Register a background sync if sending fails. If the request fails, or the device reports it's offline, register a sync with your tag. Registering again with the same tag doesn't create duplicates, so you can register after every saved submission.
4. Send the queue in the service worker. In the sync event, read all pending submissions from IndexedDB, send each one, and remove or mark it as sent only after the server confirms. Wrap the work so the browser knows the sync isn't finished until all requests complete. If something fails, let the sync fail so the browser can try again later.
5. Handle the last attempt. Browsers don't retry forever. MDN documents lastChance, which is true when the browser won't make further attempts after the current one. Use it to keep the submission saved and show a message next time the app opens, asking the person to send it manually.
The fallback for other browsers
Where background sync isn't available, keep the same queue and send it from the page itself:
- check for pending submissions whenever the app opens,
- listen for the browser's
onlineevent and try then, - add a visible "Send now" button on a list of unsent items.
This means the work is still never lost; it just needs the app to be opened again with signal. For field staff, a small badge showing "2 forms waiting to send" makes the state obvious.
Avoiding duplicates on the server
Retries can deliver the same submission twice, for example if the server saved it but the response never reached the device. Send the submission's unique ID with every attempt, and have your server ignore a second copy with the same ID. This one rule removes most of the "why do we have two identical reports?" problems.
Photos, files and privacy
Large attachments make offline queues slow and can hit storage limits. Resize photos on the device before saving them, and send attachments separately from the form fields if your server supports it, so a stuck photo doesn't block the text.
Be careful about what stays on shared devices. Clear sent submissions from IndexedDB promptly, and if staff share tablets, require sign-in before the queue is visible.
Telling people what's happening
Status messages matter as much as code:
- "Saved on this device. We'll send it when you're back online."
- "Sent" with the time, once the server confirms.
- "Couldn't send. Tap to try again." when automatic attempts have ended.
Avoid showing "Submitted" before the server has the data. That's the message that leads to lost work being discovered weeks later.
An illustrative example
A building surveyor's firm in Kilkenny uses a web app for site inspection checklists. Inspectors often work in basements with no signal and used to lose submissions. The developer stores each checklist and its resized photos in IndexedDB, tries to send immediately, and registers a background sync on failure. On phones without support, the app retries when it opens and shows a "waiting to send" count. Each checklist carries a unique ID, so the office system ignores duplicates.
FAQ
Does background sync work after the browser is closed?
In supporting browsers, the service worker can run the sync later without the page open. Exact behaviour depends on the browser and device.
Can I control when the sync happens?
No. The browser decides when connectivity is good enough. For scheduled refreshes there's a separate periodic sync API with its own limits.
Is this the same as making the whole app work offline?
No. This handles sending. Showing pages and data offline needs caching too.
Do I need a native app instead?
Not for most forms. A web app with a queue and background sync covers many field use cases.
If your team loses form submissions in poor-signal locations and you want them saved and sent reliably, tell us where your forms get used.