When I was moderating that panel at the Progressive Web App dev Summit, I brought up this point about twenty minutes in:
Alex, in your talk yesterday you were showing the AMP demo there with the Washington Post. You click through and there’s the Washington Post AMP thing, and it was able to install the Service Worker with that custom element. But I was looking at the URL bar …and that wasn’t the Washington Post. It was on the CDN from AMP. So I talked to Paul Backaus from the AMP team, and he explained that it’s an
iframe
, and using aniframe
you can install a Service Worker from somewhere else.
Alex and Emily explained that, duh, that’s the way iframe
s work. It makes sense when you think about it—an iframe
is pretty much the same as any other browser window. Still, it feels like it might violate the principle of least surprise.
Let’s say you followed my tongue-in-cheek advice to build a progressive web app store. Your homepage might have the latest 10 or 20 progressive web apps. You could also include 10 or 20 iframe
s so that those sites are “pre-installed” for the person viewing your page.
Enough theory. Here’s a practical example…
Suppose you’ve never visited the website for my book, html5forwebdesigners.com (if you have visited it, and you want to play along with this experiment, go to your browser settings and delete anything stored by that domain).
You happen to visit my website adactio.com. There’s a little blurb buried down on the home page that says “Read my book” with a link through to html5forwebdesigners.com. I’ve added this markup after the link:
<iframe src="https://html5forwebdesigners.com/iframe.html" style="width: 0; height: 0; border: 0">
</iframe>
That hidden iframe
pulls in an empty page with a script
element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML5 For Web Designers</title>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/serviceworker.js');
}
</script>
</head>
</html>
That registers the Service Worker on my book’s site which then proceeds to install all the assets it needs to render the entire site offline.
There you have it. Without ever visiting the domain html5forwebdesigners.com, the site has been pre-loaded onto your device because you visited the domain adactio.com.
A few caveats:
I had to relax the Content Security Policy for html5forwebdesigners.com to allow the
iframe
to be embedded on adactio.com:Header always set Access-Control-Allow-Origin: "https://adactio.com"
If your browser’s settings has “Block third-party cookies and site data” selected in the preferences, the
iframe
-invoked Service Worker won’t install:Uncaught (in promise) DOMException: Failed to register a ServiceWorker: The user denied permission to use Service Worker.
The example I’ve put together here is relatively harmless. But it’s possible to imagine more extreme scenarios. Imagine there’s a publishing company that has 50 websites for 50 different publications. Each one of them could have an empty page waiting to be embedded via iframe
from the other 49 sites. You only need to visit one page on one of those 50 sites to have 50 Service Workers spun up and caching assets in the background.
There’s the potential here for a tragedy of the commons. I hope we’ll be sensible about how we use this power.
Just don’t tell the advertising industry about this.
2 Shares
# Shared by Claudio (aglioeolio) on Thursday, July 7th, 2016 at 4:16pm
# Shared by Jay Wintermeyer on Wednesday, March 6th, 2019 at 6:38pm