Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit b983ea3

Browse files
committed
New post 🚀
1 parent 9ef8272 commit b983ea3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

_drafts/2020-08-10-webpack-workbox-service-worker-typescript.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,16 @@ setCatchHandler((options: RouteHandlerCallbackOptions): Promise<Response> => {
179179
//...other code...
180180
```
181181

182-
...message event to manage my personal pull to refresh implementation...
182+
As a consequence of the fact that I'm migrating my previous version of the service worker for my website, I needed also to migrate the part that is responsible for getting messages from the page related to the pull to refresh. In this case the previous version of the code was responsible for the clear of some caches. We can write this feature using the `deleteCacheAndMetadata` method of the `ExpirationPlugin` class. Below you can find the related code.
183183

184184
```typescript
185+
//...other code...
186+
185187
self.addEventListener('message', (event: ExtendableMessageEvent) => {
186188
const isARefresh = (event: ExtendableMessageEvent): boolean => event.data.message === 'refresh'
187189
const sendRefreshCompletedMessageToClient = (event: ExtendableMessageEvent): void => event.ports[0].postMessage({ refreshCompleted: true })
188190

189191
if (isARefresh(event)) {
190-
console.log(cacheNames)
191192
Promise.all([
192193
imagesExpirationPlugin.deleteCacheAndMetadata(),
193194
documentExpirationPlugin.deleteCacheAndMetadata()
@@ -196,9 +197,14 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => {
196197
.catch(() => sendRefreshCompletedMessageToClient(event))
197198
}
198199
})
200+
201+
//...other code...
199202
```
200203

201-
...google analytics offline...
204+
In a [previous post](/2019/12/15/pwa-offiline-tracking-google-analytics.html) I already talked about the fact that workbox could help us to manage the Google Analytics tracking when the PWA is offline. So for this part, we just need (as in the previous version of my service worker) to call the `googleAnalytics.initialize()` function and workbox will take care of:
205+
206+
* store the analytics tracking occured while the app is offline
207+
* sync this tracking events to the server when a network connection is available again
202208

203209
```typescript
204210
import * as googleAnalytics from 'workbox-google-analytics';

_ts/sw.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => {
9191
const sendRefreshCompletedMessageToClient = (event: ExtendableMessageEvent): void => event.ports[0].postMessage({ refreshCompleted: true })
9292

9393
if (isARefresh(event)) {
94-
console.log(cacheNames)
9594
Promise.all([
9695
imagesExpirationPlugin.deleteCacheAndMetadata(),
9796
documentExpirationPlugin.deleteCacheAndMetadata()

0 commit comments

Comments
 (0)