Skip to content

Commit a560585

Browse files
finish coding
1 parent 9664ea7 commit a560585

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,7 +4257,7 @@ Offline Applications and Service Workers are integral parts of modern web develo
42574257
##### Example: Implementing Offline Capabilities with Service Workers
42584258

42594259
```javascript
4260-
const registerServiceWorker = async () => {
4260+
const registerServiceWorker = async function () {
42614261
if ("serviceWorker" in navigator) {
42624262
try {
42634263
const registration = await navigator.serviceWorker.register(
@@ -4343,15 +4343,29 @@ h1 {
43434343
##### Example: Offline Data Synchronization
43444344

43454345
```javascript
4346-
self.addEventListener('sync', function(event) {
4347-
if (event.tag === 'sync-data') {
4346+
const syncMessagesLater = async function () {
4347+
const registration = await navigator.serviceWorker.ready;
4348+
try {
4349+
await registration.sync.register("sync-data");
4350+
alert("Background Sync registered");
4351+
} catch {
4352+
alert("Background Sync could not be registered!");
4353+
}
4354+
};
4355+
4356+
syncMessagesLater();
4357+
```
4358+
4359+
```javascript
4360+
self.addEventListener("sync", function(event) {
4361+
if (event.tag === "sync-data") {
43484362
event.waitUntil(syncData());
43494363
}
43504364
});
43514365

43524366
function syncData() {
43534367
// Perform data synchronization logic here
4354-
alert('Syncing data...');
4368+
alert("Syncing data...");
43554369
}
43564370
```
43574371

@@ -4362,6 +4376,7 @@ function syncData() {
43624376
[^120]:[CodeSandbox: Offline Data Synchronization.](https://tmpq6w.csb.app/), last access: October 13, 2024.
43634377

43644378
- The code above demonstrates how to perform offline data synchronization using a background sync event (`sync` event).
4379+
- The `syncMessagesLater()` asynchronous function registers a background sync from a browsing context.
43654380
- When a sync event with the tag `sync-data` is triggered, it calls the `syncData()` function.
43664381
- Developers can implement custom synchronization logic inside the `syncData()` function to synchronize data with a server or local storage.
43674382

0 commit comments

Comments
 (0)