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

Commit d443053

Browse files
committed
New post 🚀
1 parent c38a820 commit d443053

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,25 @@ self.addEventListener('install', (event: ExtendableEvent) => {
9898
//...other code...
9999
```
100100

101-
...routes and strategies...
101+
After the precache we define the routes and strategies for the network call after the install events. In my implementation I decided to setup a cache for each one of the following assets:
102+
103+
* styles (CSS files)
104+
* scripts (JavaScript files)
105+
* documents (HTML files)
106+
* fonts
107+
* images
108+
109+
Each route is registered in the service worker with the `registerRoute` function. It expects 3 parameters: a regex that defines the URLs intercepted by the route, a strategy handler and eventually the HTTP method expected on the route (the last two parameters are optional and could be substituted by a `Route` object that contains this 3 params). In our case, as a consequence of the fact that I decided that all the routes must be served with a `CacheFirst` strategy, we can define a `registerRoute` function that defines a route with a cache first strategy given the regex of the route, the cache name and the `ExpirationPlugin`. What is an `ExpirationPlugin`? It is a class that define the retention policy of the cache to which it is assigned. In our case I defined different `ExpirationPlugin` instances based on the policy I chosen for each media type supported by the routes we defined above:
110+
111+
* 15 days for the styles
112+
* 180 days for the fonts
113+
* 60 days for the images
114+
* 60 days for the documents
115+
116+
Above you can see the implementation for the routes and strategies.
102117

103118
```typescript
104-
import { registerRoute, setCatchHandler, Route } from 'workbox-routing';
119+
import { registerRoute, Route } from 'workbox-routing';
105120
import { ExpirationPlugin } from 'workbox-expiration';
106121
import { CacheFirst } from 'workbox-strategies';
107122

@@ -138,7 +153,7 @@ registerCacheFirstRouteUsing('image', CACHE_IMAGES_NAME, imagesExpirationPlugin)
138153
...set catch handler...
139154

140155
```typescript
141-
import { registerRoute, setCatchHandler, Route } from 'workbox-routing';
156+
import { setCatchHandler } from 'workbox-routing';
142157

143158
//...other code...
144159

@@ -183,13 +198,23 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => {
183198
})
184199
```
185200

186-
...strict types + tslint + google analytics...
201+
...google analytics offline...
187202

188203
```typescript
189204
import * as googleAnalytics from 'workbox-google-analytics';
190205

191206
//...other code...
192207

208+
googleAnalytics.initialize()
209+
210+
//...other code...
211+
```
212+
213+
...strict types + tslint + google analytics...
214+
215+
```typescript
216+
//...other code...
217+
193218
// Fix self: https://stackoverflow.com/questions/56356655/structuring-a-typescript-project-with-workers
194219
declare const self: ServiceWorkerGlobalScope;
195220
export {};
@@ -201,11 +226,6 @@ export {};
201226
precacheAndRoute(self.__WB_MANIFEST)
202227

203228
//...other code...
204-
205-
googleAnalytics.initialize()
206-
207-
//...other code...
208-
209229
```
210230

211231
#### Conclusion

0 commit comments

Comments
 (0)