amp-install-serviceworker: support cross-origin SW installation
Support cross-origin SW installation (somehow)
This will enable folks to install service workers when an AMP HTML file is loaded from the AMP Cache
What's the objective here? importScripts() works cross-origin, so you really only need a stub service worker on the CDN itself. (Which it probably doesn't support right now, but still, supporting that might make for an easier "fix".) importScripts() does make upgrading the service worker more difficult.
@jmadler is looking for the ability for the page on the CDN to install a SW that would only be active on the origin of the doc. I'm not currently convinced we actually want to to support that.
Letting people install SWs on the CDN isn't entirely crazy, but would require a lot of thought.
For context: https://github.com/ampproject/amphtml/pull/954#discussion-diff-45132552
Sorry for the slow reply.
It's worth noting that a Service Worker can be installed via an <iframe> element. The way you might structure this in AMP is something along the lines of:
<!doctype html>
<html>
<!-- article.html, hosted at either:
https://cdn.example.com/..../article.html
https://originalsite.com/.../article.html
-->
<head>
<title>...</title>
<link rel="canonical" href="https://originalsite.com/.../article.html" >
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<script src="https://cdn.ampproject.org/v0.js" async></script>
<style>body {opacity: 0}</style><noscript><style>body {opacity: 1}</style></noscript>
<style amp-custom>
...
</style>
</style>
</head>
<body>
<amp-iframe width=1 height=1
sandbox="allow-scripts"
layout="responsive"
frameborder="0"
src="https://originalsite.com/serviceworker_bootstrap.html">
</amp-iframe>
</body>
</html>
This allows any AMP article to bootstrap a SW for navigations to the eventual origin.
I don't think this is something we want to do. Please object strongly if you feel otherwise.
Please re-open this discussion.