|
1 |
| -# Service Worker Application shell architecture |
| 1 | +# Service Worker Application Shell architecture |
2 | 2 |
|
3 |
| -A sample web app shell demonstrating a shell + content architecture using [Service Worker](http://www.html5rocks.com/en/tutorials/service-worker/introduction/). This allows you to offline your 'shell', gaining performance advantages. More info coming soon. |
| 3 | +A sample web app shell demonstrating a shell + content architecture using [Service Worker](http://www.html5rocks.com/en/tutorials/service-worker/introduction/). This allows you to offline your 'shell', gaining performance advantages. |
4 | 4 |
|
5 | 5 | ## Goals
|
6 | 6 |
|
@@ -38,13 +38,45 @@ $ gulp dev
|
38 | 38 |
|
39 | 39 | You will need to use AppEngine to serve the contents of the directory.
|
40 | 40 |
|
41 |
| -## Philosophy |
| 41 | +## Why? |
42 | 42 |
|
43 | 43 | [Service Workers](http://www.html5rocks.com/en/tutorials/service-worker/introduction/) are fantastic for offline caching but they also offer significant performance wins in the form of instant loading for repeat visits. This is possible with just a few changes to our overall application’s UI architecture.
|
44 | 44 |
|
45 | 45 | We can offline cache our application shell without the network being present and populate the content for it using JavaScript. This allows us to get meaningful pixels from our UI on the screen without the network, even if our content eventually comes from there. Think of it as displaying regions of the screen where toolbars and cards will eventually be populated very quickly and then loading in the rest of the content progressively.
|
46 | 46 |
|
47 |
| -### Tips for your application shell |
| 47 | +## How? |
| 48 | + |
| 49 | +During the first load experience, our goal is to get meaningful content to the user’s screen as quickly as possible. To achieve this: |
| 50 | + |
| 51 | +* **Server** will send down HTML content the client can render and will use far-future HTTP cache expiration headers to account for browsers without Service Worker support. It will serve filenames using hashes to enable ‘versioning’ and easy updates for later on in the application lifecycle. |
| 52 | +* **Page(s)** will include inline CSS styles in the document <head> to provide a fast first paint of the application shell. Each page will asynchronously load in the JavaScript necessary for the current view. As CSS cannot be asynchronously loaded in natively, this can be emulated using JavaScript (or something like the Filament Group’s [loadCSS](https://github.com/filamentgroup/loadCSS) project). |
| 53 | +* **Service Worker** will store a cached entry of the application shell so that on repeat visits, the shell can be loaded entirely from the SW cache unless an update is available on the network. |
| 54 | + |
| 55 | +In the background, we will register our Service Worker following this lifecycle: |
| 56 | + |
| 57 | +| Event | Action | |
| 58 | +|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 59 | +| Install | Cache application shell and other SPA resources | |
| 60 | +| Activate | Clear out old caches | |
| 61 | +| Fetch | Serve up single page web app for urls.progressively cache future content - unless it’s no-cache header. Cache in a ‘content-cache’ to separate from the web app | |
| 62 | + |
| 63 | +#### File revision |
| 64 | + |
| 65 | +One question that arises is how exactly should file revisions/updates be handled. This is application specific and the options are: |
| 66 | + |
| 67 | +* Network first and cache old version otherwise |
| 68 | +* Network only and fail if offline |
| 69 | +* Cache the old version and update later |
| 70 | + |
| 71 | +#### Tooling |
| 72 | + |
| 73 | +* Use [sw-toolbox](https://github.com/GoogleChrome/sw-toolbox) for runtime caching, with varying strategies depending on the resource: |
| 74 | + * [cacheFirst](https://github.com/GoogleChrome/sw-toolbox#toolboxcachefirst) for images, along with a dedicated named cache that has a custom expiration policy of N maxEntries. |
| 75 | + * [networkFirst](https://github.com/GoogleChrome/sw-toolbox#toolboxnetworkfirst) or fastest for API requests, depending on the desired freshness. fastests might end up being fine for everything, but if there’s a specific API feed that gets updated very frequently, we can go with networkFirst for that. |
| 76 | +* Use [sw-precache](https://github.com/GoogleChrome/sw-precache) for caching the application shell |
| 77 | +should handle the concerns around file revisions, the install/activate questions, and the fetch scenario for the app shell. |
| 78 | + |
| 79 | +## Tips for your application shell |
48 | 80 |
|
49 | 81 | In a [Progressive webapp](https://infrequently.org/2015/06/progressive-apps-escaping-tabs-without-losing-our-soul/), everything necessary to load the the simplest "shell" of your UI consists of HTML, CSS and JavaScript. Keep this shell as lean as possible. Some of the will come from your application’s index file (inline DOM, styles) and the rest may be loaded from external scripts and stylesheets. Together, they are all you need to display a simple, static app. It’s important to keep the shell of your webapp learn to ensure that some inline static structural content can be displayed as soon as the webapp is opened, regardless of the network being available or not.
|
50 | 82 |
|
|
0 commit comments