async function syncMessagesLater() {
const registration = await navigator.serviceWorker.ready;
try {
await registration.sync.register("sync-messages");
} catch {
console.log("Background Sync could not be registered!");
}
}Background Synchronization API / notebook
Background Synchronization API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
navigator.serviceWorker.ready.then((registration) => {
registration.sync.getTags().then((tags) => {
if (tags.includes("sync-messages")) {
console.log("Messages sync already requested");
}
});
});self.addEventListener("sync", (event) => {
if (event.tag === "sync-messages") {
event.waitUntil(sendOutboxMessages());
}
});