AMP Conf 2019. April 17/18. Tokyo.


DescriptionInstalls a ServiceWorker for the current page.
Required Script<script async custom-element="amp-install-serviceworker" src=""></script>
Supported Layoutsnodisplay
ExamplesAnnotated code example for amp-install-serviceworker


Registers the service worker given by the src attribute if the AMP document is loaded from the same origin as the given service worker URL. If the data-iframe-src is set, loads that URL as an iframe when the AMP document is served from an AMP cache. This allows ServiceWorker installation from the AMP cache, so that the service worker is installed by the time users visit the origin site.

This service worker runs whenever the AMP file is served from the origin where you publish the AMP file. On documents served from an AMP cache, the service worker will be installed in the background but will not execute or affect the page's behavior.

See this article for how ServiceWorkers can help with making the AMP experience awesome with ServiceWorkers.




src (required)The URL of the ServiceWorker to register, which must use https protocol.
data-iframe-src (optional)The URL of an HTML document that installs a ServiceWorker. The URL must use https protocol.
data-scope (optional)The scope of the service worker to be installed.
layoutMust have the value nodisplay.

The is a regular expression that matches URLs to be rewritten to navigate via shell for no-service-worker fallback. See Shell URL rewrite section for more details. The value must be a valid JavaScript RegExp string. For example:

  • amp.html
  • .*amp
  • .*\.amp\.html
  • .*\/amp$
data-no-service-worker-fallback-shell-urlThe URL to the shell to use to rewrite URL navigations for no-service-worker fallback. See Shell URL rewrite section for more details. The value must be an URL on the same origin as the AMP document itself.

Shell URL rewrite

When service workers are not available or not yet active, it's possible to configure URL rewrite to direct navigations to the shell. This way, for example, AMP Runtime can redirect navigation to the "shell" instead of a "leaf" AMP document.

This fallback is only used when the document is opened on the source origin, and NOT on proxy origin.

The URL rewrite is configured using data-no-service-worker-fallback-url-match and data-no-service-worker-fallback-shell-url attributes as following:

<amp-install-serviceworker layout="nodisplay"

Where: - data-no-service-worker-fallback-shell-url specifies the link for AMP+PWA shell. It's required to be on the source origin as the AMP document. - data-no-service-worker-fallback-url-match is a JavaScript regular expression that describes how to match “in-shell” links vs non-in-shell links. - Both of these attributes must be present to trigger URL rewrite.

URL rewrite works as following: 1. The document provides a configuration that explains how to navigate within the shell. 2. AMP Runtime tries to install the service worker. 3. If service worker is not installed (not installable), as a fallback AMP Runtime will preload the shell page via a hidden iframe. 4. AMP Runtime will intercept the “in-shell” navigations (which will often be AMP-to-AMP navigations) and if the service worker is not running, rewrite the navigation URL to proceed to the “shell”-based URL. 5. The shell will startup and run the requested navigation via its router. Typically the shell will immediately execute history.replaceState(href).

A URL is rewritten in the form shell-url#href={encodeURIComponent(href)}. For example:

Besides rewriting URLs, amp-install-serviceworker also will try to preload the shell. This is done by creating an iframe with #preload fragment:

<iframe src="" hidden sandbox="allow-scripts allow-same-origin"></iframe>

For the preload to be effective, of course, the shell response must have appropriate HTTP cache headers.


See amp-install-serviceworker rules in the AMP validator specification.

Need more help?

You've read this document a dozen times but it doesn't really cover all of your questions? Maybe other people felt the same: reach out to them on Stack Overflow.

Go to Stack Overflow
Found a bug or missing a feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Go to GitHub