-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatingServiceWorker.txt
More file actions
108 lines (96 loc) · 3.03 KB
/
Copy pathCreatingServiceWorker.txt
File metadata and controls
108 lines (96 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Creating Service Workers by { Daily Investors }
Creating a service worker to interact with `localStorage` involves a few steps. Here's a basic outline to get you started:
1. **Register the Service Worker**: This is done in your main JavaScript file.
2. **Install the Service Worker**: This is where you can cache files and set up the service worker.
3. **Activate the Service Worker**: This is where you can clean up old caches.
4. **Fetch Event**: This is where you can intercept network requests and serve cached responses.
Here's a simple example:
### 1. Register the Service Worker
```javascript
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(registration => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(error => {
console.log('Service Worker registration failed:', error);
});
}
```
### 2. Install the Service Worker
```javascript
self.addEventListener('install', event => {
event.waitUntil(
caches.open('my-cache').then(cache => {
return cache.addAll([
'/',
'/index.html',
'/styles.css',
'/script.js'
]);
})
);
});
```
### 3. Activate the Service Worker
```javascript
self.addEventListener('activate', event => {
const cacheWhitelist = ['my-cache'];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
});
```
### 4. Fetch Event
```javascript
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
return response || fetch(event.request);
})
);
});
```
### Interacting with `localStorage`
Service workers don't have direct access to `localStorage`, but you can use the `postMessage` API to communicate between your main script and the service worker.
#### In your main script:
```javascript
navigator.serviceWorker.controller.postMessage({
type: 'SAVE_TO_LOCAL_STORAGE',
key: 'myKey',
value: 'myValue'
});
```
#### In your service worker:
```javascript
self.addEventListener('message', event => {
if (event.data.type === 'SAVE_TO_LOCAL_STORAGE') {
self.clients.matchAll().then(clients => {
clients.forEach(client => {
client.postMessage({
type: 'SAVE_TO_LOCAL_STORAGE',
key: event.data.key,
value: event.data.value
});
});
});
}
});
```
#### Back in your main script:
```javascript
navigator.serviceWorker.addEventListener('message', event => {
if (event.data.type === 'SAVE_TO_LOCAL_STORAGE') {
localStorage.setItem(event.data.key, event.data.value);
}
});
```
This setup allows your service worker to indirectly interact with `localStorage` by passing messages to the main script, which then handles the `localStorage` operations.