Inlining or caching both please! filament group, inc., boston, ma calculate my fuel cost

Last week, I was finishing a section of my slides for a presentation at the performance.Now() conference. Calculate gasoline cost per mile 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. Best price gas fireplace inserts 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.

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. Average cost of gas heating per month 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.

Here’s how it works: the page linked below includes some inline CSS and a caching function much like the example above. Calculate cost of gas for a trip 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 element. Who is responsible for the rising cost of gasoline 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. Deliver an entire website via HTML??