Commit a75202f
authored
Feature: @webdoc/default-template service worker (#190)
# Synopsis
This PR adds a service worker to the sites generated by the default template to
* cache all core assets (CSS & JavaScript)
* add support for a offline storage setting (in the new settings page)
It also adds an IndexedDB layer that holds the manifest data. The manifest is used to get all the pages in the site to be cached.
# Service Worker
The protocol/ folder in the site source contains the interface b/w the main & service-worker threads.
* `webdocService` lets the main application initialize and pass messages to the service worker.
* `webdocDB` is a wrapper around IndexedDB for data persistence. The "settings" store holds the application settings and the manifest url & hash.
The service worker accepts 2 types of messages:
* "lifecycle:init" − This is to be sent whenever the manifest changes (or the site is loaded for the first time). The manifest hash is to be fetched on each page load to check for changes.
* "trigger:settings" − This lets the service worker know if a setting changed (like offline storage).
## Caching
There are 2 types of caches:
* The main cache is permanent and holds the core assets like react, react-dom scripts & icons that are versioned or won't change.
* The ephemeral cache. This cache evicts items when the manifest hash changes, which is detected by attaching a "x-manifest-hash" header to cached responses and comparing it to the hash in the settings from IndexedDB (the exact settings object is found by the request origin).
The "offline storage" setting lets the user cache the whole website immediately (well all the pages in the manifest registry, which doesn't include things like README and settings pages). It's an opt-in setting; however, the service worker will cache each HTML page when the user visits them into the ephemeral cache anyway.
# IndexedDB
There are 2 object stores in the current database model:
* "settings" − This holds the application settings (identified by the application name or the site origin). It holds the manifest hash and the manifest URL.
* "hyperlinks" − This holds all the documentation hyperlinks in the website. Offline storage uses it to identify which pages need to be cached.
# UI
## Toolbar in explorer & settings page
This toolbar has the new settings page link [button]
<img width="297" alt="Screen Shot 2022-08-25 at 6 37 50 AM" src="https://user-images.githubusercontent.com/22450567/186643536-854af31e-9c94-412e-a764-4bb7f93f15c3.png">
The settings page lets you toggle offline storage:
<img width="510" alt="Screen Shot 2022-08-25 at 6 38 44 AM" src="https://user-images.githubusercontent.com/22450567/186643694-3d8647dd-6366-4ea2-8da0-87f0a68ed26d.png">
## Toasts for offline / stale pages
These toasts let the user know if
* the page was served from their offline cache (i.e. verifying the manifest hash failed)
* the page is stale (the fetched hash was different from what was in IndexedDB).
<img width="371" alt="Screen Shot 2022-08-25 at 6 33 45 AM" src="https://user-images.githubusercontent.com/22450567/186643048-3da8e064-057b-408b-b664-5a6e48f63a7c.png">
<img width="357" alt="Screen Shot 2022-08-25 at 6 34 18 AM" src="https://user-images.githubusercontent.com/22450567/186643055-e71d348e-d33f-47ba-a08f-f42eeef2e60b.png">1 parent 87a9737 commit a75202f
File tree
35 files changed
+1107
-52
lines changed- .idea
- common/config/rush
- core
- webdoc-cli/src
- webdoc-default-template
- helper/crawl
- src
- app
- components
- Explorer
- Header
- hooks
- pages/Settings
- protocol
- webdocDB
- service-worker
- static/scripts/vendor
- tmpl
- pages
35 files changed
+1107
-52
lines changedThis file was deleted.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
137 | | - | |
| 138 | + | |
| 139 | + | |
138 | 140 | | |
139 | 141 | | |
140 | 142 | | |
| |||
181 | 183 | | |
182 | 184 | | |
183 | 185 | | |
184 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
185 | 195 | | |
186 | 196 | | |
187 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
6 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
81 | 87 | | |
82 | 88 | | |
83 | 89 | | |
| |||
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
136 | 149 | | |
137 | 150 | | |
138 | 151 | | |
| |||
169 | 182 | | |
170 | 183 | | |
171 | 184 | | |
| 185 | + | |
172 | 186 | | |
173 | 187 | | |
174 | 188 | | |
| |||
291 | 305 | | |
292 | 306 | | |
293 | 307 | | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
294 | 321 | | |
295 | 322 | | |
296 | 323 | | |
| |||
0 commit comments