BackgroundSync Form
The BackgroundSync Form component transforms regular HTML forms into offline-capable forms that automatically queue submissions when the user is offline. When a form is submitted without internet connectivity, the request is queued by the service worker and automatically retried when the connection is restored. This ensures that user data is never lost due to connectivity issues.
This component is particularly useful for:
Building reliable data entry forms that work offline
Creating survey and feedback forms that don't lose responses
Implementing offline-first contact forms and support tickets
Building field service applications with unreliable connectivity
Creating order and checkout forms for e-commerce in low-connectivity areas
Implementing CRM and sales forms for mobile workers
Building event registration forms that work anywhere
Creating time-tracking and expense reporting applications
Browser Support
The BackgroundSync Form component relies on the Background Sync API and service workers, which are widely supported in modern browsers.
Support level: Excellent - Works on all modern browsers that support service workers (Chrome, Firefox, Safari, Edge).
This component requires:
An active service worker with Workbox configured
Background Sync enabled in your PWA configuration
Proper queue configuration matching your form endpoints
When online, forms submit normally with no performance penalty. The background sync mechanism only activates when the network is unavailable.
How It Works
User Submits Form: User fills out and submits the form
Network Check: Component attempts to submit via fetch API
If Online: Form submits normally, response handled, user redirected
If Offline: Request automatically queued by service worker via Workbox Background Sync
Auto Retry: Service worker automatically retries when connectivity returns
Success: User notified when queued submission succeeds
Configuration
First, configure background sync in your PWA configuration to handle form submissions:
Key configuration options:
queue_name: Unique identifier for this queuematch_callback: Pattern to match URLs that should be queuedmethod: HTTP method(s) to queue (POST, PUT, PATCH, DELETE)max_retention_time: How long to keep failed requests (in minutes)force_sync_fallback: Enable fallback for browsers without Background Sync APIbroadcast_channel: Optional channel for queue status updates
Usage
Basic Form with Background Sync
Form with Custom Redirect
After successful submission (or queueing if offline), the user is redirected to /thank-you.
Form with Custom Headers
Form with Custom Fetch Parameters
JSON Form Submission
File Upload Form
Form with Loading State
Form with Validation Feedback
Best Practices
Always Configure Matching Routes
Ensure your background sync configuration matches the form action URLs. Mismatched patterns will cause forms to fail offline instead of being queued.
Handle All Submission States
Always provide feedback for all three states: loading, success, and offline queueing. Users need to know what's happening with their data.
Validate Before Sending
The component automatically calls form.reportValidity() before submission. Use HTML5 validation attributes (required, pattern, min, max) for client-side validation.
Choose Appropriate Encoding
The component supports three encoding types:
multipart/form-data: For file uploads (default for forms with<input type="file">)application/x-www-form-urlencoded: Standard form encoding (default)application/json: For API submissions requiring JSON
Set Appropriate Retention Times
Configure max_retention_time based on the importance and time-sensitivity of your data:
Critical data (orders, registrations): 7-14 days
Regular forms (contacts, feedback): 3-5 days
Non-critical updates: 1-2 days
Common Use Cases
1. Offline Contact Form
2. E-Commerce Order Form
3. Survey/Feedback Form
4. Field Service Report
API Reference
Values
params
paramsType: Object Default: {mode: 'cors', cache: 'no-cache', credentials: 'same-origin', redirect: 'follow', referrerPolicy: 'no-referrer'}
Fetch API parameters to use for the request.
headers
headersType: Object Default: {}
Additional HTTP headers to include in the request.
redirection
redirectionType: String Default: null
URL to redirect to after form submission (success or queued). If null, no redirect occurs.
authenticating
authenticatingType: Boolean Default: false
If true, automatically adds JWT authorization header to the request.
keyIdIndex
keyIdIndexType: String Default: 'default'
Key identifier to use for JWT signing when authenticating is true.
Actions
send(event)
send(event)Submits the form. Should be attached to the form's submit button.
Targets
None
Events
before:send
before:sendFired just before the form submission request is sent.
Event detail:
url(string): The form action URLparams(object): Complete fetch parameters including headers and body
after:send
after:sendFired immediately after receiving a response from the server.
Event detail:
url(string): The form action URLparams(object): Fetch parameters usedresponse(Response): The fetch Response object
error
errorFired when the submission fails (typically due to network error/offline status). Request is automatically queued.
Event detail:
error(Error): The error object
invalid-data
invalid-dataFired when form validation fails (form.reportValidity() returns false).
unsupported-enctype
unsupported-enctypeFired when the form uses an unsupported encoding type.
auth-missing-key
auth-missing-keyFired when authenticating is true but the JWT key is not found.
Event detail:
keyIdIndex(string): The missing key identifier
Related Components
BackgroundSync Queue - Monitor and manage background sync queues
Service Worker - Service worker configuration and setup
Connection Status - Display online/offline status to users
Resources
Last updated
Was this helpful?