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).

circle-info

This component requires:

  • An active service worker with Workbox configured

  • Background Sync enabled in your PWA configuration

  • Proper queue configuration matching your form endpoints

circle-check

How It Works

  1. User Submits Form: User fills out and submits the form

  2. Network Check: Component attempts to submit via fetch API

  3. If Online: Form submits normally, response handled, user redirected

  4. If Offline: Request automatically queued by service worker via Workbox Background Sync

  5. Auto Retry: Service worker automatically retries when connectivity returns

  6. 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 queue

  • match_callback: Pattern to match URLs that should be queued

  • method: 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 API

  • broadcast_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

circle-exclamation

Handle All Submission States

circle-check

Validate Before Sending

circle-info

The component automatically calls form.reportValidity() before submission. Use HTML5 validation attributes (required, pattern, min, max) for client-side validation.

Choose Appropriate Encoding

circle-info

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

circle-exclamation

Common Use Cases

1. Offline Contact Form

2. E-Commerce Order Form

3. Survey/Feedback Form

4. Field Service Report

API Reference

Values

params

Type: Object Default: {mode: 'cors', cache: 'no-cache', credentials: 'same-origin', redirect: 'follow', referrerPolicy: 'no-referrer'}

Fetch API parameters to use for the request.

headers

Type: Object Default: {}

Additional HTTP headers to include in the request.

redirection

Type: String Default: null

URL to redirect to after form submission (success or queued). If null, no redirect occurs.

authenticating

Type: Boolean Default: false

If true, automatically adds JWT authorization header to the request.

keyIdIndex

Type: String Default: 'default'

Key identifier to use for JWT signing when authenticating is true.

Actions

send(event)

Submits the form. Should be attached to the form's submit button.

Targets

None

Events

before:send

Fired just before the form submission request is sent.

Event detail:

  • url (string): The form action URL

  • params (object): Complete fetch parameters including headers and body

after:send

Fired immediately after receiving a response from the server.

Event detail:

  • url (string): The form action URL

  • params (object): Fetch parameters used

  • response (Response): The fetch Response object

error

Fired when the submission fails (typically due to network error/offline status). Request is automatically queued.

Event detail:

  • error (Error): The error object

invalid-data

Fired when form validation fails (form.reportValidity() returns false).

unsupported-enctype

Fired when the form uses an unsupported encoding type.

auth-missing-key

Fired when authenticating is true but the JWT key is not found.

Event detail:

  • keyIdIndex (string): The missing key identifier

Resources

Last updated

Was this helpful?