Request with Intent: Caching Strategies in the Age of PWAs

What\\\'s Hot At JVZoo


WordPress plugin that finds customers and automatically follows up with them, tracking prices and selling it to them when their requested products go on sale - or recommended items become available.

Video Auto Clicks

This software lets you put YouTube videos on your timeline, on any fan page you manage or on any Facebook group you are a member of. Harness the power of video and combine it with the social nature of Facebook to drive clicks to your affiliate links, CPA

VisualReel - Ace

VisualReel Ace is a visual content builder with Ace features. It lets you create and share Cinemagraphs, Memes and Quote-Pictures using its massive library of 1000s of videos and images.

myIMUniversity 2.0 Standard

Get this Cutting-Edge Software That Creates Udemy Like Sites With Inbuilt Members Area Packed With HD Video Courses, Support Desk and Video HOSTING

Bitcoin Profit Secrets

Discover how to profit safely, quickly and confidently with Bitcoin and other cryptocurrencies

Once upon a hour, we are dependent upon browsers to handle caching for us; as developers in those epoches, we had very little control. But then came Progressive Web Apps( PWAs ), Service Workers, and the Cache API–and suddenly we have expansive power over what comes put in the cache and how it gets settled there. We can now cache everything we want to … and therein lies a possible problem.

Media files–especially images–make up the bulk of average page weight these days, and it’s getting worse. In order to improve performance, it’s tempting to cache so much better of this material as possible, but should be used? In most cases , no. Even with all this newfangled technology at our fingertips, great performance still hinges on a simple rule: request only what you need and prepare each request as small as is practicable.

To provide the best possible experience for our consumers without abusing their network connection or their hard drive, it’s time to gave a spin on some classic best traditions, experimentation with media caching programmes, and play around with a few Cache API ploys that Service Workers have disguised up their sleeves.

Best planneds

All those lessons “weve learned” optimizing web pages for dial-up became super-useful again when portable took off, and they continue to be applicable in the labor we do for a world-wide audience today. Unreliable or high-pitched latency system acquaintances are still the norm in countless regions throughout the world, reminding us that it’s never safe to acquire a technical baseline elevations evenly or in sync with its corresponding cutting edge. And that’s the thing about accomplishment best practices: record has digest out that approaches that are good for performance now will be pursued being good for performance in the future.

Before the onset of Service Workers, we are also able equip some educations to browsers with respect to how long they should cache a particular resource, but that was about it. Papers and assets downloaded to a user’s machine would be dropped into a directory on their hard drive. When the browser assembled a request for a particular document or asset, it would peek in the cache firstly to see if it previously had what it needed to perhaps shunned thumping the network.

We have considerably more control over structure seeks and the cache these days, but that doesn’t excuse us from being careful about the resources on our web pages.

Request only what you need

As I mentioned, the web today is lousy with media. Personas and videos have become a dominant means of communication. They may alter well when it comes to sales and commerce, but they are hardly performant when it comes to download and making acceleration. With this in mind, each and every image( and video, etc .) should have to fight for its place on the page.

A few years back, a recipe of mine was included in a newspaper story on fix with spirits( booze , not spirit ). I don’t subscribe to the print version of the working paper, so when the section came out I came to the site to take a look at how it turned out. During a recent redesign, the place had decided to load all articles into a practically full-screen modal viewbox layered on top of their homepage. This intended soliciting the commodity necessitated requests for all of the resources are connected with the clause page plus all the contents and assets for the homepage. Oh, and the homepage had video ads–plural. And, yes, they auto-played.

I popped open DevTools and discovered the page had blown past 15 MB in page force. Tim Kadlec had recently propelled What Does My Site Cost ?, so I decided to check out the damage. Turns out that the actual cost to view that page for the average US-based user was more than the cost of the book edition of that day’s newspaper. That’s only messed up.

Sure, I could condemn the kinfolks who built the site for doing their readers such a disservice, but the reality is that none of us go to work with the goal of degenerating our users’ ordeals. This could happen to any of us. We could expend periods scrutinizing the performance of a page merely to have some committee decide to set that carefully crafted page atop a Times Square of auto-playing video ads. Imagine how much worse things “wouldve been” if we were stacking two abysmally-performing sheets on top of each other!

Media can be great for be drawn when challenger is high( e.g ., on the homepage of a newspaper ), but when you want books is concentrated in a single enterprise( e.g ., see the actual article ), its evaluate can descent from important to “nice to have.” Yes, studies has been demonstrated that images outdo at proceeding eyeballs, but once a tourist is on the article page, no one cares; we’re merely spawning it make longer to download and more expensive to access. The situation only gets worse as we shove more media into the page.

We must do everything in our ability to reduce the value of our pages, so avoid requests for things that don’t add value. For starters, if you’re writing an article about a data violate, resist the urge to include that farcical furnish photo of some random dude in a hoodie typing on personal computers in a very dark room.

Request the smaller register you are able to

Now that we’ve taken stock of what we do need to include, we must ask ourselves a critical question: How can we deliver it in the fastest way possible? This can be as simple as choosing the most appropriate image format for the contents presented( and optimizing the heck out of it) or as complex as recreating assets wholly( for example, if swapping from raster to vector imagery would be more efficient ).

Offer change formats

When it comes to image formats, we don’t have to choose between performance and reach anymore. We can provide multiple options and let the browser decide which one to use, based on what it can handle.

You can accomplish this by offering multiple informants within a characterization or video part. Start by creating multiple formats of the media asset. For instance, with WebP and JPG, it’s likely that the WebP will have a smaller file size than the JPG( but check to make sure ). With those alternating generators, you can drop them into a photo like this 😛 TAGEND

Descriptive text about the picture.

Browsers that recognize the picture element will check the source element before making a decision about which portrait to request. If the browser supports the MIME form “image/ webp, ” it will kick off a request for the WebP format image. If not( or if the browser doesn’t recognize picture ), it will seek the JPG.

The nice thing about this approach is that you’re serving the smallest image possible to the user without having to resort to any kind of JavaScript hackery.

You can take the same approach with video folders 😛 TAGEND

Your browser doesn’t support native video playback,

but you can download

this video instead.

Browsers that support WebM will request the first source, whereas browsers that don’t–but do understand MP4 videos–will request the second one. Browsers that don’t support the video element will fall back to the paragraph about downloading the file.

The order of your source components substances. Browsers will choose the first usable source, so if you specify an optimized alternative format after a more broadly compatible one, the alternative format may never get are caught up.

Depending on your situation, you might consider bypassing this markup-based approach and treat things on the server instead. For illustration, if a JPG is being requested and the browser aids WebP( which is indicated in the Accept header ), there’s nothing stopping you from replying with a WebP version of the resource. In fact, some CDN services–Cloudinary, for instance–come with this sort of functionality right out of the box.

Offer different sizes

Formats aside, you may want to deliver alternate image sizes optimized for the current size of the browser’s viewport. After all, there’s no part lading an likenes that’s 3-4 times larger than the screen interpreting it; that’s only wasting bandwidth. This is where responsive idols come in.

Here’s an example 😛 TAGEND

Descriptive text about the picture.

There’s a lot going on in this super-charged img element, so I’ll break it down 😛 TAGEND

This img offers three width possible options for a handed JPG: 256 px wide( small.jpg ), 512 px wide( medium.jpg ), and 1024 px wide( large.jpg ). These shall be defined in the srcset attribute with befitting width descriptors.The src defines a default image source, which acts as a fallback for browsers that don’t support srcset. Your choice for the default image will likely depend on the context and general consumption blueprints. Often I’d recommend the smallest image be the default value, but if the majority of your traffic is on older desktop browsers, you might want to go with the medium-sized image.The sizes peculiarity is a presentational hint that informs the browser how the likenes is likely to be made in different scenarios( its extrinsic size) formerly CSS has been applied. This particular example says that the portrait will be the full thicknes of the viewport( 100 vw) until the viewport reaches 30 em in diameter( min-width: 30 em ), at which point the image is likely to be 30 em wide. You can move the sizes ethic as complicated or as simple as you require; skipping it compels browsers to use the default value of 100 vw.

You can even blend this approach with alternate formats and cultivates within a single representation.

All of this is to say that you have a number of tools at your disposal for delivering fast-loading media, so use them!

Defer solicits( when possible)

Years ago, Internet Explorer 11 initiated a brand-new feature that enabled developers to de-prioritize specific img points to speed up page interpreting: lazyload. That dimension never exited anywhere, standards-wise, but it was a solid attempt to defer image loading until likeness are in view( or close to it) without having to involve JavaScript.

There have been countless JavaScript-based implementations of slothful loading epitomes since then, but recently Google also took a thrust at a more declarative approaching, squandering a different aspect: loading.

The loading attribute buoys three appraises( “auto, ” “lazy, ” and “eager”) to define how a resource should be brought in. For our purposes, the “lazy” value is the most interesting because it shelves loading the resource until it contacts a calculated distance from the viewport.

Adding that into the mix…

Descriptive text about the picture.

This attribute proposals a little of a recital enhance in Chromium-based browsers. Hopefully it will become a standard and get picked up by other browsers in the future, but in the meantime there’s no trauma in including it because browsers that don’t understand the feature will simply ignore it.

This approach complements a media prioritization policy really well, but before I get to that, I was intended to take a closer look at Service Workers.

Manipulate requests in a Service Worker

Service Workers are a special type of Web Worker with the capacity required to intercept, modify, and is submitted in response to all network petitions via the Fetch API. They also have access to the Cache API, as well as other asynchronous client-side data stores like IndexedDB for resource storage.

When a Service Worker is installed, you can hook into that incident and prime the cache with resources you want to use last-minute. Many kinfolks use this opportunity to squirrel away copies of world-wide assets, including modes, scripts, mottoes, and the like, but you can also use it to cache epitomes for implementation when network entreaties fail.

Keep a fallback persona in your back pocket

Assuming you want to use a fallback in more than one networking recipe, you can set up a identified function that will respond with that resource 😛 TAGEND

run respondWithFallbackImage()

return caches.match( “/ i/ fallbacks/ offline.svg” );

Then, within a fetch event handler, you can use that function to provide that fallback image when requests for portrait flunk at the network 😛 TAGEND

self.addEventListener( “fetch”, affair =>

const entreaty= event.request;

if( request.headers.get( “Accept” ). includes( “image”))


return retrieve( solicit, mode: ‘no-cors’)

. then( response =>

return response;


. catch(





When the network is available, consumers get the expected behavior 😛 TAGEND

Screenshot of a component showing a series of user profile images of users who have liked something
Social media avatars are interpreted as anticipated when the network is available.

But when the network is interrupted, portraits will be swapped automatically for a fallback, and the user event is still acceptable 😛 TAGEND

Screenshot showing a series of identical generic user images in place of the individual ones which have not loaded
A generic fallback avatar is rendered when the network is unavailable.

On the surface, this approach may not seem all that helpful in terms of performance since you’ve basically lent an additional image download into the mix. With this system in place, nonetheless, some that amazing possibilities open up to you.

Respect a user’s choice to save data

Some users reduce their data consumption by participate a “lite” mode or turning on a “data saver” feature. When this happens, browsers will often send a Save-Data header with their network applications.

Within your Service Worker, you can look for this header and adjust your responses accordingly. First, you look for the header 😛 TAGEND

let save_data= fraudulent; if( ‘connection’ in sailor)

save_data= navigator.connection.saveData;

Then, within your fetch handler for idols, you might choose to preemptively respond with the fallback image instead of going to the network at all 😛 TAGEND

self.addEventListener( “fetch”, happening =>

const seek= event.request;

if( request.headers.get( “Accept” ). includes( “image”))


if( save_data)

return respondWithFallbackImage ();

// system you insured previously



You could even take this a step farther and adjust respondWithFallbackImage() to provide intersperse likeness based on what the original petition was for. To do that you’d define various fallbacks globally in the Service Worker 😛 TAGEND

const fallback_avatar= “/ i/ fallbacks/ avatar.svg”,

fallback_image= “/ i/ fallbacks/ image.svg”;

Both of those files should then be cached during the Service Worker invest phenomenon 😛 TAGEND

return cache.addAll([


fallback_image ]);

Finally, within respondWithFallbackImage() you could dish up the relevant likenes based on the URL being fetched. In my place, the avatars are gathered from, so I experiment for that.

capacity respondWithFallbackImage( url)

const likenes= avatars.test(/ webmention \. io/)? fallback_avatar

: fallback_image;

return caches.match( idol );

With that change, I’ll need to update the fetch handler to pass in request.url as an rationale to respondWithFallbackImage (). Once that’s done, when the network comes interrupted I be brought to an end reading something like this 😛 TAGEND

Screenshot showing a blog comment with a generic user profile image and image placeholder where the network could not load the actual images
A webmention that contains both an avatar and an embedded persona will render with two different fallbacks when the Save-Data header is present.

Next, we need to establish some general guidelines for handling media assets–based on the situation, of course.

The caching policy: prioritize sure-fire media

In my experience, media–especially images–on the web tend to fall into three categories of necessity. At one discontinue of the range are elements that don’t computed meaningful evaluate. At the other end of the range are critical resources that do add value, such as charts and graphs that are essential to understanding the bordering content. Somewhere in the middle are what I would call “nice-to-have” media. They do add value to the core experience of a page but are not critical to understanding the content.

If you consider your media with this division in judgment, you can establish some general guidelines for handling each, based on the situation. In other oaths, a caching approach.

Media loading strategy, broken down by how critical an resource is to understanding an interface

Media category

Fast connection


Slow connection

No system


Load media

Replace with placeholder


Load media

Replace with placeholder


Remove from content absolutely

When it comes to disambiguating the critical from the nice-to-have, it’s helpful to have those resources organized into separate directories( or same ). That mode we can add some logic into the Service Worker that can help it decide which is which. For sample, on my own personal site, critical epitomes are either self-hosted or come from the website for my book. Knowing that, I can write regular faces that equal those regions 😛 TAGEND

const high_priority=[

/ aaron \ -gustafson \. com /,

/ adaptivewebdesign \. info/


With that high_priority variable defined, I can create a function that will let me know if a granted persona application( for example) is a key priority entreaty or not 😛 TAGEND

purpose isHighPriority( url)

// how many high priority links are we dealing with?

cause i= high_priority.length;

// curve through each

while( i–)

// does the request URL match this regular formulation?

if( high_priority[ i ]. test( url))

// yes, it’s a high priority request

return true-blue;

// no joins , not high priority

return incorrect;

Adding support for prioritizing media requests exclusively requires adding a brand-new conditional into the fetch event handler, like we did with Save-Data. Your specific recipe for network and cache touch will probably differ, but now was how I taken the decision to mix in this reasoning within idol applications 😛 TAGEND

// Check the cache firstly

// Return the cached portrait if we have one

// If the image is not in the cache, continue

// Is this image high priority? if( isHighPriority( url))

// Fetch the idol

// If the fetch replaces, save a imitation in the cache

// If not, react with an “offline” placeholder

// Not high priority else

// Should I save data?

if( save_data)

// Respond with a “saving data” placeholder

// Not saving data


// Fetch the likenes

// If the retrieve replaces, save a replica in the cache

// If not, react with an “offline” placeholder

We can apply this prioritized coming to many kinds of resources. We could even use it to control which pages are sufficed cache-first vs. network-first.

Keep the cache straighten

The ability to control which aids are cached to disk is a huge opportunity, but it also carries with it an evenly huge responsibility not to mistreat it.

Every caching strategy is likely to differ, at least a little bit. If we’re publishing a diary online, for instance, it might make sense to cache all of the chapters, portraits, etc. for offline considering. There’s a determined amount of content and–assuming there aren’t a ton of ponderous personas and videos–users will benefit from not having to download each chapter separately.

On a story area, however, caching every article and photo will swiftly fill up our users’ hard drives. If a site volunteers an undetermined number of sheets and resources, it’s critical to have a caching strategy that throws hard-handed limits on how many resources we’re caching to disk.

One way to do this is to create several different blocks associated with caching different forms of content. The more fleeting material caches can have strict restraints around how many entries is likely to be accumulated. Sure, we’ll still be bound to the storage limits of the design, but do we are willing to our website to take up 2 GB of someone’s hard drive?

Here’s an example, again from my own place 😛 TAGEND

const sw_caches=


honour: `$ account static`



identify: `$ explanation images `,

limit: 75



appoint: `$ explanation pages `,

limit: 5



appoint: `$ account other `,

limit: 50

Here I’ve defined several caches, each with a specify used for addressing it in the Cache API and a form prefix. The explanation is defined elsewhere in the Service Worker, and allows me to purge all caches at once if necessary.

With the exception of the static cache, which is used for static assets, every cache has a limit to the number of items that is able to accumulated. I simply cache the latest 5 pages someone has visited, for example. Epitomes are limited to the most recent 75, and so on. This is an approach that Jeremy Keith sketches in his fantastic book Going Offline( which you should really spoke if you haven’t already–here’s a test ).

With these cache interpretations in place, I can clean up my caches sporadically and prune the oldest components. Here’s Jeremy’s recommended code for this approach 😛 TAGEND

capacity trimCache( cacheName, maxItems)

// Open the cache cacheName)

. then( cache =>

// Get the keys and count them


. then( keys =>

// Do we have more than we should?

if( keys.length> maxItems)

// Delete the oldest entry and operate trim again

cache.delete( keys[ 0 ])

. then(() =>

trimCache( cacheName, maxItems)




We can prompt this code to run whenever a brand-new sheet loadings. By trot it in the Service Worker, it runs in a separate thread and won’t drag down the site’s responsiveness. We trigger it by affixing a message( using postMessage ()) to the Service Worker from the prime JavaScript thread 😛 TAGEND

// First check to see if you have an active service work if( navigator.serviceWorker.controller)

// Then add an incident listener

window.addEventListener( “load”, role ()

// Tell the service worker to clean up

navigator.serviceWorker.controller.postMessage( “clean up” );


The final step in wiring it all up opens up the Service Worker to receive the theme 😛 TAGEND

addEventListener( “message”, messageEvent =>

if( == “clean up”)

// loop-the-loop though the caches

for( cause key in sw_caches)

// if the cache has a limit

if( sw_caches[ key ]. limit !== undefined)

// trim it to that restraint

trimCache( sw_caches[ key ]. appoint, sw_caches[ key ]. restriction );


Here, the Service Worker listens for inbound words and responds to the “clean up” request by ranging trimCache() on each of the cache barrels with a defined limit.

This approach is by no means elegant, but it slogs. It would be far better to make decisions about ousting cached responses based on how frequently each item is accessed and/ or how much office it takes up on disk.( Removing cached items based purely on when they were cached isn’t nearly as helpful .) Sadly, we don’t have that level of detail when it comes to inspecting the caches…yet. I’m actually working to address this limitation in the Cache API right now.

Your useds always come first

The engineerings underlying Progressive Web Apps are continuing to mature, but even if you aren’t interested in turning your website into a PWA, there’s so much you can do today to improve your users’ knowledge when it comes to media. And, as with every other form of inclusive scheme, it starts with centering on your users who are most at risk of having an unpleasant experience.

Draw importances between critical, nice-to-have, and superfluous media. Remove the cruft, then optimize the bejeezus out of each remaining resource. Serve your media in multiple formats and lengths, prioritizing the smaller versions first to start the most of high latency and gradual ties. If your users say they want to save data, respect that and have a fallback plan in place. Cache wisely and with the utmost respect for your users’ disk space. And, eventually, examine your caching strategies regularly–especially when it is necessary to vast media files.Follow these guidelines, and every one of your users–from kinfolks rocking a JioPhone on a agricultural mobile network in India to beings on a high-end gaming laptop wired to a 10 Gbps fiber line in Silicon Valley–will thank you.

Read more:

Robert F
Author: Robert F


    Powered By Trivia Blast 2.0

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Affiliate Debug : No cookies are currently set - I am looking for cookies at : /