Skip to content

Commit 06a192c

Browse files
Add: CodeSandbox 119.
1 parent 1841238 commit 06a192c

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,35 +4278,57 @@ if ('serviceWorker' in navigator) {
42784278

42794279
- The code above demonstrates how to register a Service Worker for offline capabilities.
42804280
- First, it checks if the browser supports Service Workers.
4281-
- If supported, it registers the Service Worker script (`service-worker.js`) using the `register()` method.
4281+
- 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.
42824282
- The Service Worker script can intercept network requests, cache assets, and manage offline behavior.
42834283

42844284
##### Example: Caching Assets for Offline Use
42854285

42864286
```javascript
4287-
var CACHE_NAME = 'my-site-cache-v1';
4288-
var urlsToCache = [
4289-
'/',
4290-
'/styles/main.css',
4291-
'/scripts/main.js',
4292-
'/images/logo.png'
4287+
const CACHE_NAME = 'my-site-cache-v1';
4288+
const urlsToCache = [
4289+
"index.html",
4290+
"./", // Alias for index.html
4291+
"styles.css",
4292+
"script.js",
4293+
"example.png",
42934294
];
42944295

42954296
self.addEventListener('install', function(event) {
42964297
event.waitUntil(
42974298
caches.open(CACHE_NAME)
42984299
.then(function(cache) {
4299-
console.log('Cache opened');
4300+
alert('Cache opened');
43004301
return cache.addAll(urlsToCache);
43014302
})
43024303
);
43034304
});
43044305
```
43054306

4307+
In `script.js`, write a simple script:
4308+
4309+
```javascript
4310+
alert("to be cached script.js");
4311+
```
4312+
4313+
In `styles.css`, add simple styling:
4314+
4315+
```css
4316+
h1 {
4317+
color: navy;
4318+
}
4319+
```
4320+
4321+
[![Edit 119-Caching Assets for Offline Use](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/119-caching-assets-for-offline-use-tmpq6w)
4322+
4323+
- [^119]CodeSandbox: Caching Assets for Offline Use.
4324+
4325+
[^119]:[CodeSandbox: Caching Assets for Offline Use.](https://tmpq6w.csb.app/), last access: October 11, 2024.
4326+
43064327
- The code above demonstrates how to cache assets for offline use using a Service Worker.
43074328
- During the Service Worker installation phase (`install` event), it opens a cache named `my-site-cache-v1`.
43084329
- It then adds specified URLs (including HTML, CSS, JavaScript, and image files) to the cache using the `addAll()` method.
43094330
- The cached assets can be served from the cache when the user is offline, providing offline access to the application.
4331+
- **There is a problem with this example in CodeSandbox environment to get it fully working, it's just for your information how it should work, sorry for that.**
43104332

43114333
##### Example: Offline Data Synchronization
43124334

0 commit comments

Comments
 (0)