let saveData = false; if (request.headers['save-data'] == 'on') { saveData = true; } // OR if ("connection" in navigator) { if (navigator.connection.saveData === true) { }
}
Slide 62
Slide 63
Slide 64
this.addEventListener('fetch', event => { if(navigator.connection.saveData === true){ if (/.jpg$|.gif$|.png$/.test(event.request.url)) { //return a highly compressed or smaller image event.respondWith( fetch(saveDataUrl, { mode: 'no-cors' }) ); } if (event.request.url.includes('fonts.googleapis.com')) { // return nothing and use fallback fonts instead event.respondWith( new Response('', { status: 408, statusText: 'Ignore fonts to save data.' }) ); } } }); https://bit.ly/2I3cJxs