loading
Warning:
Chromium only Background Sync and Periodic Background Sync ship in Chromium-based browsers (Chrome, Edge, Opera) and are not available in Firefox or Safari. Always feature-detect with IsSupported / IsPeriodicSupported before relying on them.
Note:
A service worker registration is required Both APIs hang off an active ServiceWorkerRegistration. Use the prerequisite section below (or the ServiceWorker page) to register the demo worker at /sw.js first - its sync handler logs every fired tag to the DevTools console.

Check support

IsSupported / IsPeriodicSupported

IsSupported is true when the runtime exposes ServiceWorkerRegistration.sync; IsPeriodicSupported checks for ServiceWorkerRegistration.periodicSync.

C#
var oneShot = await backgroundSync.IsSupported();
var periodic = await backgroundSync.IsPeriodicSupported();
Live sample
support output
Results will appear here when you interact with the samples.

Prerequisite: register the demo worker

ServiceWorker.Register

Background sync tags are stored on a service worker registration, so one must exist before Register or GetTags can succeed. This button registers the same minimal /sw.js used across this category.

@inject Bit.Butil.ServiceWorker serviceWorker

<button @onclick="RegisterWorker">Register /sw.js</button>

@code {
    private async Task RegisterWorker() => await serviceWorker.Register("/sw.js");
}
Live sample
service worker output
Results will appear here when you interact with the samples.

One-shot sync

Register / GetTags

Register queues a one-shot sync under a tag; the service worker's sync event fires for that tag once the device is online (immediately, if it already is). GetTags lists the tags currently registered. Registering the same tag twice coalesces into one pending sync.

@inject Bit.Butil.BackgroundSync backgroundSync

@code {
    private async Task Queue()
    {
        // Registering the same tag twice coalesces into one pending sync, so this is safe to call
        // on every failed send rather than tracking whether one is already queued.
        var ok = await backgroundSync.Register("sync-outbox");

        string[] tags = await backgroundSync.GetTags();
    }
}
Live sample
one-shot sync output
Results will appear here when you interact with the samples.

Periodic sync

RegisterPeriodic / GetPeriodicTags / UnregisterPeriodic

Periodic sync wakes the service worker at a browser-chosen cadence, never more often than the minimum interval you pass (in milliseconds). It requires the periodic-background-sync permission, which Chromium only grants to installed web apps (PWAs) with sufficient site engagement - expect a rejection when running this demo from a plain tab.

@inject Bit.Butil.BackgroundSync backgroundSync

@code {
    private async Task Schedule()
    {
        // minimum interval: 1 hour (the browser may extend it)
        var ok = await backgroundSync.RegisterPeriodic("refresh-feed", 60 * 60 * 1000);

        string[] tags = await backgroundSync.GetPeriodicTags();
    }

    private async Task Cancel() => await backgroundSync.UnregisterPeriodic("refresh-feed");
}
Live sample
periodic sync output
Results will appear here when you interact with the samples.
Note:
Where does the work happen? The page only registers tags. The actual work runs inside the service worker's sync / periodicsync handlers - the demo worker simply logs the tag, so open the DevTools console (and, in Chromium, DevTools, Application, Service workers, where you can also dispatch a sync event manually) to observe it firing.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes ServiceWorkerRegistration.sync.
IsPeriodicSupported
ValueTask<bool> IsPeriodicSupported()
True when ServiceWorkerRegistration.periodicSync is available.
Register
ValueTask<bool> Register(string tag)
Registers a one-shot sync. The service worker's sync event fires once the device is online.
GetTags
ValueTask<string[]> GetTags()
Lists tags currently registered for one-shot sync.
RegisterPeriodic
ValueTask<bool> RegisterPeriodic(string tag, long minInterval)
Registers a periodic sync with a minimum interval in milliseconds. Requires the periodic-background-sync permission.
GetPeriodicTags
ValueTask<string[]> GetPeriodicTags()
Lists tags currently registered for periodic sync.
UnregisterPeriodic
ValueTask<bool> UnregisterPeriodic(string tag)
Removes a periodic sync registration. Returns true when a matching tag was unregistered.
An unhandled error has occurred. Reload 🗙