Created
June 28, 2019 15:13
-
-
Save magarcia/b14753dc5bb34497bba91319148f1b9f to your computer and use it in GitHub Desktop.
Adaptive Media Serving using Service Workers
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
self.addEventListener('fetch', function(event) { | |
if (/\.jpg$|.png$|.webp$/.test(event.request.url)) { | |
const url = event.request.url + `?quality=${getMediaQuality()}`; | |
event.respondWith(fetch(url)); | |
} | |
}); |
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
function getMediaQuality() { | |
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection; | |
if (!connection) { | |
return 'medium'; | |
} | |
switch (connection.effectiveType) { | |
case 'slow-2g': | |
case '2g': | |
return 'low'; | |
case '3g': | |
return 'medium'; | |
case '4g': | |
return 'high'; | |
default: | |
return 'low'; | |
} | |
} |
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
const images = document.querySelectorAll('img'); | |
images.forEach(img => { | |
img.src = img.src.replace('low', getMediaQuality()); | |
}); |
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
<img src=“http://images.magarcia.io/cute_cat” alt=“Cute cat”/> |
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
<img src="http://images.magarcia.io/cute_cat?quality=low" alt="Cute cat" /> |
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
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', function() { | |
navigator.serviceWorker.register('/sw.js').then( | |
function(registration) { | |
console.log('ServiceWorker registration successful with scope: ', registration.scope); | |
}, | |
function(err) { | |
console.log('ServiceWorker registration failed: ', err); | |
} | |
); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment