Foreword: Service workers in production require https, for good reason. You intercept network requests, and serve back what you want, so https is needed for security. You can get away with not having it on localhost.
The application (app) shell is the bare minimum needed to load your web application to a user. For my personal site that means the index page, js file and css file.
This application shell is cached by the service worker when it installs. This means that it can be loaded very quickly on subsequent requests, even with poor or no connection.
The actual content, within a proper web application will then be loaded asynchronously. This allows for a more native mobile application UX, as a native app will load instantly, with all the colours, branding etc, but will loaded the “content” when it’s ready.
To achieve this we will need to write a small amount code.
2 files are needed. One to register a service worker and another to hold the service worker implementation.
Few things to clear up here.
Always check that the browser supports service workers, most do now. Apple is massively dragging it’s heels. Even Microsoft is super keen, come on Apple…
The service worker needs to be in another file realistically for readability.
Scope is the scope that the service worker can work within, in the above example I’ve given it root.
I’ve captured the registration object for use outside of this method. This is needed for other features we’ll explore later.
Now let’s build the service worker itself:
There is a lot going on here. I could have broken it down into separate functions and wrote about each, but then I thought when I was learning this I wanted a complete working example so I could mess with it. Also I didn’t want to piss off the copy and paste warriors.
App shell, ultimately is an array of critical files. If you want to go further you can architect your app to load the content asynchronously.
Service worker install is an event, that is fired after the previous registration finishes.
You guessed it, activate fires after installing.
Now for the stuff you actually want to know, the gotchas!
The initial cache is atomic, one fails, they all fail. This is another reason why we keep it to crucial files only.
Skip waiting - the original service worker will not unregister until all the tabs with that service worker are closed, force it with this.
Removing the old caches - reading from the old cache will serve you outdated files, get rid of it. Tidy house tidy mind.
Any change in the service worker file will cause an unregister for the service worker. This might sound bad, but it’s needed to control the cache.
I force this with the version number. I have a gulp script that will rewrite that version statement incrementing the number in the process.
You will get fucked by the cache. It happens at times with the normal cache. You change something, refresh, nothing changes. 20 minutes of pulling your hair out and you realise it’s the cache. Service workers make this problem worse. With great power comes great responsibility.
If you’re doing the workshop the code here will help with completing 001_AppShell