Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Created October 21, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffposnick/5e45336189dbb476ec74 to your computer and use it in GitHub Desktop.
Save jeffposnick/5e45336189dbb476ec74 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Video src issue</title>
</head>
<body>
<p>
Playing the video will trigger a network request for
<code>https://ia800502.us.archive.org/10/items/WebmVp8Vorbis/webmvp8.webm</code>.
The <code>ia800502.us.archive.org</code> server doesn't support CORS, but when there is no SW
involvement, the video plays without issue.
</p>
<p>
When there is a SW (like there is on this page), if the <code>fetch</code> handler intercepts
the request, it has a <code>mode: cors</code>, presumably because the request contains the
<code>Accept</code> and <code>Range</code> headers, which trigger CORS mode.
</p>
<p>
If you attempt to respond from within the <code>fetch</code> handler with an opaque response,
a <code>NetworkError</code> is triggered on the page, since an opaque response can't be used
for what was, from the page's perspective, a CORS request.
</p>
<video src="https://ia800502.us.archive.org/10/items/WebmVp8Vorbis/webmvp8.webm" preload="none" controls></video>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js');
}
</script>
</body>
</html>
(global => {
'use strict';
// Boilerplate to ensure our service worker takes control of the page as soon as possible.
global.addEventListener('install', event => event.waitUntil(global.skipWaiting()));
global.addEventListener('activate', event => event.waitUntil(global.clients.claim()));
global.addEventListener('fetch', event => {
if (event.request.url.endsWith('.webm')) {
console.log(`Original request's mode: ${event.request.mode}`);
var noCORSRequest = new Request(event.request.url, {mode: 'no-cors'});
console.log(`No-CORS request's mode: ${noCORSRequest.mode}`);
event.respondWith(global.fetch(noCORSRequest).then(response => {
console.log(`Response's type: ${response.type}`);
return response;
}));
}
});
})(self);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment