At the moment there is no way to alert the user when an updated service worker has been installed. Often pages inform the user of this with a prompt such as:

I suggest that amp-install-serviceworker be extended to add support for an updatefound event which can be triggered on the amp-install-serviceworker element.
|
function install(win, src) { |
|
return win.navigator.serviceWorker.register(src).then(function(registration) { |
|
if (getMode().development) { |
|
user().info(TAG, 'ServiceWorker registration successful with scope: ', |
|
registration.scope); |
|
} |
|
return registration; |
|
}, function(e) { |
|
user().error(TAG, 'ServiceWorker registration failed:', e); |
|
}); |
|
} |
Perhaps instead of updatefound it would be better to name it updateinstalled since as I understand we're primarily interested in when an updatefound event results in 'installed' === registration.installing.state being true. So perhaps something like so:
function install(win, src) {
return win.navigator.serviceWorker.register(src).then(function(registration) {
if (getMode().development) {
user().info(TAG, 'ServiceWorker registration successful with scope: ',
registration.scope);
}
reg.addEventListener('updatefound', () => {
reg.installing.addEventListener('statechange', () => {
if ( 'installed' === registration.installing.state ) {
const name = 'updateinstalled';
const event = createCustomEvent(this.win, `${TAG}.${name}`, dict({}));
Services.actionServiceForDoc(this.element).trigger(this.element, name, event, ActionTrust.HIGH);
}
});
});
return registration;
}, function(e) {
user().error(TAG, 'ServiceWorker registration failed:', e);
});
}
With something like this in place, an author could then show a notice using amp-bind like so:
<amp-state id="updateAvailable">false</amp-state>
<amp-install-serviceworker
on="updateinstalled:AMP.setState({updateAvailable:true})"
src="https://www.example.com/serviceworker.js"
data-iframe-src="https://www.example.com/install-serviceworker.html"
layout="nodisplay"
></amp-install-serviceworker>
<button hidden [hidden]="! updateAvailable" on="tap:AMP.navigateTo(url=AMPDOC_URL)">
Update available!
</button>
(Maybe there should be an AMP.reload() action available as well.)
Thoughts?
At the moment there is no way to alert the user when an updated service worker has been installed. Often pages inform the user of this with a prompt such as:
I suggest that
amp-install-serviceworkerbe extended to add support for anupdatefoundevent which can be triggered on theamp-install-serviceworkerelement.amphtml/extensions/amp-install-serviceworker/0.1/amp-install-serviceworker.js
Lines 293 to 303 in 9e9554c
Perhaps instead of
updatefoundit would be better to name itupdateinstalledsince as I understand we're primarily interested in when anupdatefoundevent results in'installed' === registration.installing.statebeing true. So perhaps something like so:With something like this in place, an author could then show a notice using
amp-bindlike so:(Maybe there should be an
AMP.reload()action available as well.)Thoughts?