menu

Inlining or Caching? Both Please!

Last week, I was finishing a section of my slides for a presentation at the Performance.Now() conference. In it, I was exploring patterns that enable the browser to render a page as fast as possible by including code alongside the initial HTML so that the browser has everything it needs to start rendering the page, without making additional requests.

Our two go-to options to achieve this goal are inlining and server push (more on how we use those), but each has drawbacks: inlining prevents a file from being cached for reuse, and server push is still a bit experimental, with some browser bugs still being worked out. As I was preparing to describe these caveats, I thought, “I wonder if the new Service Worker and Caching APIs could enable caching for inline code.”

Let’s start with an example. The following code snippet has a style element containing some inline CSS. I’ve added an ID attribute onto the style element so that it’s easy to find it using JavaScript. After the style element, a small piece of JavaScript finds that CSS and uses the new Cache API to store it in a local browser cache (with a content type of text/css) for use on subsequent pages.

You might recognize the caches API above if you’ve worked with Service Workers. Despite its ties to Service Workers however, Caches is actually available in the window global namespace as well, so unlike workers, we are able to use Caches while having access to content from the DOM. That said, in order to ensure a subsequent page on the site will get this local site.css file if the browser requests it, a service worker will still be needed.

Here’s a common Service Worker snippet you might already have running on your site: when a file is requested by the browser, the worker looks in local caches before going to the network. With this worker running, any future requests to site.css will find it in local cache and avoid a network request.

We’re now ready to handle requests to site.css on subsequent pages of the site.

It’s worth noting here that in order to truly take advantage of this pattern in a site’s templates, subsequent pages will need to avoid inlining the files and instead reference them externally (using a stylesheet link, a script tag, etc.). To negotiate that in an HTML template, we often set a cookie on the first visit to a site. Then, on subsequent page views, the server can check for that cookie to infer that it’s safe to assume the browser has files already in cache. If so, it can reference the files externally instead of inlining them.

To demonstrate the pattern in action, I’ve created a simple demo page.

Here’s how it works: The page linked below includes some inline CSS and a caching function much like the example above. It also installs the service worker code above to check for files in local caches when requests are made. That first demo page contains a link to a second page which references site.css using a regular <link rel="stylesheet" href="site.css"> element. If site.css is loaded locally, which should happen in any service worker supporting browser, the second page will have a green background. If site.css is loaded from the server, the background will be red.

One improvement we can make is to set up the script to look multiple inline style and script elements using a common selector, and apply custom attributes to these elements to declare their content-type, file path, and cache name for saving their content to cache. With those attributes in place, a script can find and cache many of these inlined files in a single sweep.

We might also consider ways to use this pattern to improve upon existing critical CSS patterns. For example, we often inline a page’s critical CSS, and then load the site’s full CSS at a later time. In this workflow, the full CSS file typically overlaps many of the CSS rules with the “critical” subset we already delivered. A better approach might be to load a full version of the CSS that does not contain the critical rules at all, and then use the Caches API to combine the critical and full CSS files to create a full site CSS entry in local cache.

If we wanted, we could even try using this pattern to deliver an entire front-end codebase in one HTML response, mimicking a fancy web packaging format. Would it be a good idea? Probably not! But it would be fun to experiment.

For now, we’ll be brainstorming about other potential uses… and possibly updating our site! Thanks for reading.