An introduction to how one can modify the images being served to the users of a website based on their current network condition or bandwidth available. The technique can be extended to other items on the page like JS, CSS (and even the HTML). It helps speed up page load time by reducing the amount of data that has to be downloaded on the user's device on slow networks.
2. More than 55% of all usage on the
internet is from mobile
In India, 78% of all usage is from
mobile.
Source -
Times of India Report
3. Mobile Networks can be
Slow & Inconsistent
Data representing number of users
on different network speeds as
reported in Chrome using NetInfo
API
Collected in partnership with 91mobiles.com
9. Real-time network information
Network Information API
var effectiveType = navigator.connection.effectiveType
- Browser determines this using a combination of recently
observed round-trip times and downlink values
//Returns “4g”, “3g”, “2g” or “slow-2g”
10. Deliver lighter images
on slower networks
4G > 3G > 2G > Slow
2GFastest
=
Max.
image size
Slowest
=
Min. image
size
Covers WiFi
speeds as
well
20. Taking it further
Problem with browser-caching
- Browser doesn’t know that the URL changed in
the service worker
- Will cache a low quality response as well
21. Taking it further
Implement Network-based caching
- Do not rely on browser cache; Skip it completely
- Different caches for different network types
- Waterfall-like cache lookup; prefer better cached
image
22. Code Sample
Network-based cache maintenance
The ‘returnURL’ should be such that it responds with
cache-control: max-age=0; to skip browser cache
Respond with a cache
lookup instead of return
URL
25. - If we are adding objects to cache, we are
responsible for clearing them as well
- Use max cache time and max object count for
each network type cache
- Cache clear based on above rules regularly
Last Step
Cache Management using Google
Workbox
28. - Content must adapt to network
speed
- Images is the easiest to optimize &
biggest contributor to page size
Other content can be changed as well using this idea
Summary
29. Summary
A real-time network detection & cache
response & maintenance process
- Network Information API
- Service Workers
- Cache Storage
30. A real-time image optimization & transformation
product
ImageKit.io (https://imagekit.io)
- 30% bandwidth reduction (image optimization) in minutes
- Real-time resizing, cropping, face detection, overlays etc.
- Integrated CDN and storage
- Can use with own server or storage as well
Summary
31. Links related to this talk
Demo
https://imagekit.io/demo/sw-opt-1
Code
https://github.com/imagekit-developer/network-
based-image-optimization