October 2017
Intermediate to advanced
370 pages
8h 57m
English
So how can an AMP page install a service worker? Conveniently, there's a dedicated component, amp-install-serviceworker, for exactly this purpose. It's imported with the following script:
<script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>
We can now use the following code (/ch11/ampaspwa/index.html) to install the service worker:
<amp-install-serviceworker src="https://theampbook.com/ch11/ampaspwa/sw.js" layout="nodisplay"></amp-install-serviceworker>
The src attribute here points at a file /ch11/ampaspwa/sw.js. This file contains the JavaScript code for the service worker. Let's see how it works.
Read now
Unlock full access