A4A: Handle iOS iframe cache miss better
A4A currently writes non-AMP creatives into a cross-domain iframe via the src= attribute, assuming that the browser cache will retain/serve the same creative from the original XHR request for it. This appears to be problematic on iOS, where iframe src= appears to skip cache and go directly to a network request, resulting in a second network request for the same resource, increased cost on ad servers, etc.
We propose to handle this by using a "SafeFrame-like" mechanism: use an iframe with a src= targeted toward an AMP project-hosted loader JS file and set the iframe name= attribute to the creative content (already available, from the original ad request XHR), plus, possibly, a small amount of metadata. The loader will read the name= attribute and document.write() the content to the iframe.
As an initial proof of concept, and to validate our assumptions quickly, we will use an existing Google-hosted SafeFrame implementation and run a small experiment, comparing to the current A4A implementation. If this proves successful, and resolves the iOS caching issues, we will replace the Google SafeFrame implementation with a custom-developed, open-source component in the AMP code base. The new component is unlikely to implement the complete SafeFrame spec -- we'll probably implement only the behaviors we need for A4A.
/cc @ampproject/a4a
Seems this changed sometime in iOS 9. Simple test server:
// server.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/ad', function(req, res) {
console.log('ad request');
res.header({
'Cache-Control': 'public, max-age=60'
});
setTimeout(function() {
res.send('ad');
}, 3000);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
- - -
// in browser
document.body.innerHTML = '';
var url = 'http://localhost:3000/ad';
console.log('start', Date.now());
fetch(url).then(function() {
console.log('xhr', Date.now());
var i = document.createElement('iframe');
i.src = url;
i.onload = function() {
console.log('iframe', Date.now());
};
document.body.appendChild(i);
});
My server only logs "ad request" once, and the browser (iOS 8.4 Safari) shows a cached result being used as the iframe:
[Log] start – 1476471993832
[Log] xhr – 1476471996846 # 3 second delay, the expected response time
[Log] iframe – 1476471996854 # 8ms delay, because I had to create an iframe
iOS 9 makes two requests. 😢
Possibly. I've gotten conflicting results about this when I've asked around and tried it myself. More importantly, we're seeing server-side statistics that are consistent with lots and lots of repeated A4A ad requests from iOS. We want to do this experiment in part to see whether it makes a difference in those statistics (thus validating the cache-miss story).
@keithwrightbos Was looking into SRA, right? I haven't seen the design yet, but wouldn't that need something like the mechanism proposed here anyway?
Apologies for not getting the SRA design out sooner... had other priorities. In any event, no I actually was not planning on requiring a safeframe-like mechanism. Instead I was planning to wrap the array of creatives (and their potential signatures) with javascript. If the creative is missing/fails signature, then the iframe would be attached with the src equal to the ad request and a hash would indicate the offset of the creative to be loaded. The JS within the creative would read this hash and the document.write the creative indicated. It moves the understanding of how to render the creative into the creative itself.
But in the end they will be functionally equivalent. I plan to experiment with only enabling safeframe for iOS (perhaps only >= 9) as I still believe the cache method will be more performant and it ensures that the javascript within the creative is running under the expected domain (assuming they care about cookies, local storage, etc).
We are also looking into if its possible to use srcdoc combined with sandbox attributes to ensure proper JS isolation. This would allow for not having a resource to load which document.writes (more performant) and would ensure that creatives cannot cross access each other due to being under the same domain (an issue that exists generally). I don't suppose you know whether this is supported in iOS/chrome and if so, is the combination of sandbox + srcdoc equivalent to cross domain src?
To be clear, we do not intend to implement the safeframe API at this time but are still pursuing supporting window.context within cross domained creatives (JS library for #3019)
the iframe would be attached with the src equal to the ad request and a hash would indicate the offset of the creative to be loaded. The JS within the creative would read this hash and the document.write the creative indicated.
Would Blobs work with this? The response should already be one.
They appear to be supported for both iOS and Android (http://caniuse.com/#search=blob%20url). Definitely worth pursuing and we know for a fact that it will cause the content within the iframe to be cross domained?
Yeah, I've considered Blobs a couple of times. They're definitely more attractive than SafeFrame. As Keith notes, though, the open question is whether they would cause the frame to be cross-domain or not? It's important both for safety of the pub page and creative and to enable advertiser cookies. I haven't found clear docs on how same domain policy is treated in the blob case, and haven't yet taken 20 min to test it. Anybody know offhand?
@tdrl I'm not sure about blobs, but data-URIs are same-origin ONLY in Firefox (which is a major gotcha). Otherwise they should work great if you don't rely on document.cookie inside of that iframe.
I've done some testing in chrome and Safari and it appears that either data-URI or srcdoc+sandbox will work (can guarantee cross domain) but with a couple of caveats:
- PostMessages have a null origin which makes sense given we have no src attribute (and allowing the iframe creator to specify one would allow any JS to run under a given domain). We can likely get around this by adding a new attribute (e.g. data-origin="foo.com") and modify our message listener to look for this value when receiving messages.
- Accessing domain isolated functions throw Errors (e.g. localStorage or document.cookie). Again, this makes logical sense but the question is how many creatives this would break.
I'm guessing these will be issues with any non-safeframe solution where we want cross domain behavior. I suggest we start a low-volume experiment using srcdoc+sandbox (given it is the most straight forward solution IMO) and see the impact.
Thoughts?
@keithwrightbos This sounds good if you use srcdoc+sandbox in Firefox (which makes data-URI same origin).
Discussed with @michaelkleber and decided that potential breakage of creatives using document.cookie or local/session storage (triggering DOMException) is insurmountable at this time as it would be impossible to gauge if/how it breaks the creative. Will instead focus on safeframe implementation being persuded by @tdrl while I focus on creating an AMP-specific safeframe implementation (reads from name attribute and writes creative).
This has been addressed by using document.write safeframe solution.