Skip to content

Commit f84b2df

Browse files
Improve: CodeSandbox 118. & add 12.0
1 parent 06a192c commit f84b2df

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4256,18 +4256,29 @@ Offline Applications and Service Workers are integral parts of modern web develo
42564256
##### Example: Implementing Offline Capabilities with Service Workers
42574257

42584258
```javascript
4259-
// Register service worker
4260-
if ('serviceWorker' in navigator) {
4261-
window.addEventListener('load', function() {
4262-
navigator.serviceWorker.register('service-worker.js')
4263-
.then(function(registration) {
4264-
alert('Service Worker registered with scope: ' + registration.scope);
4265-
})
4266-
.catch(function(error) {
4267-
alert('Service Worker registration failed: ' + error);
4268-
});
4269-
});
4270-
}
4259+
const registerServiceWorker = async () => {
4260+
if ("serviceWorker" in navigator) {
4261+
try {
4262+
const registration = await navigator.serviceWorker.register(
4263+
"/service-worker.js",
4264+
{
4265+
scope: "/",
4266+
}
4267+
);
4268+
if (registration.installing) {
4269+
alert("Service Worker installing");
4270+
} else if (registration.waiting) {
4271+
alert("Service Worker installed");
4272+
} else if (registration.active) {
4273+
alert("Service Worker active with scope: " + registration.scope);
4274+
}
4275+
} catch (error) {
4276+
alert(`Registration failed with ${error}`);
4277+
}
4278+
}
4279+
};
4280+
4281+
registerServiceWorker();
42714282
```
42724283

42734284
[![Edit 118-Implementing Offline Capabilities with Service Workers](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/118-implementing-offline-capabilities-with-service-workers-zjm7tx)
@@ -4280,6 +4291,7 @@ if ('serviceWorker' in navigator) {
42804291
- First, it checks if the browser supports Service Workers.
42814292
- If supported, it registers the Service Worker script (`service-worker.js` - what the script does is outside the scope of this book) using the `register()` method.
42824293
- The Service Worker script can intercept network requests, cache assets, and manage offline behavior.
4294+
- Please turn off your internet and reload the page; it will still load, showing the offline capabilities of the Service Worker.
42834295

42844296
##### Example: Caching Assets for Offline Use
42854297

@@ -4341,10 +4353,16 @@ self.addEventListener('sync', function(event) {
43414353

43424354
function syncData() {
43434355
// Perform data synchronization logic here
4344-
console.log('Syncing data...');
4356+
alert('Syncing data...');
43454357
}
43464358
```
43474359

4360+
[![Edit 120-Offline Data Synchronization](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/120-offline-data-synchronization-5gpt4c)
4361+
4362+
- [^120]CodeSandbox: Offline Data Synchronization.
4363+
4364+
[^120]:[CodeSandbox: Offline Data Synchronization.](https://tmpq6w.csb.app/), last access: October 13, 2024.
4365+
43484366
- The code above demonstrates how to perform offline data synchronization using a background sync event (`sync` event).
43494367
- When a sync event with the tag `sync-data` is triggered, it calls the `syncData()` function.
43504368
- Developers can implement custom synchronization logic inside the `syncData()` function to synchronize data with a server or local storage.

0 commit comments

Comments
 (0)