Important: this documentation is not applicable to your currently selected format ads!
Installs a ServiceWorker for the current page.
<script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>
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
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.
<amp-install-serviceworker src="https://www.your-domain.com/serviceworker.js" data-iframe-src="https://www.your-domain.com/install-serviceworker.html" layout="nodisplay" > </amp-install-serviceworker>
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-shell-url attributes as following:
<amp-install-serviceworker layout="nodisplay" src="https://www.your-domain.com/serviceworker.js" data-no-service-worker-fallback-url-match=".*\.amp\.html" data-no-service-worker-fallback-shell-url="https://pub.com/shell" > </amp-install-serviceworker>
data-no-service-worker-fallback-shell-urlspecifies the link for AMP+PWA shell. It's required to be on the source origin as the AMP document.
- Both of these attributes must be present to trigger URL rewrite.
URL rewrite works as following:
- The document provides a configuration that explains how to navigate within the shell.
- AMP Runtime tries to install the service worker.
- If service worker is not installed (not installable), as a fallback AMP Runtime will preload the shell page via a hidden iframe.
- 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.
- The shell will startup and run the requested navigation via its router. Typically the shell will immediately execute
A URL is rewritten in the form
https://pub.com/doc.amp.html --> https://pub.com/shell#href=%2Fdoc.amp.html
Besides rewriting URLs,
amp-install-serviceworker also will try to preload the
shell. This is done by creating an iframe with
<iframe src="https://pub.com/shell#preload" 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.
The URL of the ServiceWorker to register, which must use
The URL of an HTML document that installs a ServiceWorker. The URL must use
https protocol. This attribute is necessary if the AMP page is going to be served from an AMP Cache.
The scope of the service worker to be installed.
Must have the value
The 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.
See amp-install-serviceworker rules in the AMP validator specification.
Bạn đã đọc tài liệu này hàng chục lần, nhưng nó không thật sự trả lời mọi thắc mắc của bạn? Có lẽ những người khác cũng cảm thấy như vậy: hãy liên hệ với họ trên Stack Overflow.Truy cập Stack Overflow Bạn tìm thấy một lỗi hoặc cần bổ sung một tính năng?
Dự án AMP đặc biệt khuyến khích sự tham gia và đóng góp của bạn! Chúng tôi hi vọng bạn sẽ trở thành một người tham gia tích cực trong cộng đồng mã nguồn mở của chúng tôi, nhưng chúng tôi cũng chào mừng các đóng góp đơn lẻ về vấn đề mà bạn đặc biệt quan tâm.Truy cập GitHub