Last active
April 6, 2016 17:22
-
-
Save deanhume/ab2f73e404ecbc32657d to your computer and use it in GitHub Desktop.
Save-Data header service worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
this.addEventListener('fetch', event => { | |
// Save Data support | |
if(event.request.headers.get('save-data')){ | |
//Return smaller images | |
if (/\.jpg$|.gif$|.png$/.test(event.request.url)) { | |
let saveDataUrl = event.request.url.substr(0, event.request.url.lastIndexOf(".")) + '-savedata' + event.request.url.substr(event.request.url.lastIndexOf("."), event.request.url.length - 1); | |
event.respondWith( | |
fetch(saveDataUrl, { | |
mode: 'no-cors' | |
}) | |
); | |
} | |
// We want to save data, so restrict icons and fonts too | |
if (event.request.url.includes('fonts.googleapis.com')) { | |
// return nothing | |
event.respondWith(new Response('', {status: 408, statusText: 'Ignore fonts to save data.' })); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment