Trick: Solving HTTP/2 Server Push caching issues with Service Workers!

When a browser requests a page, the server is sending the HTML as a first response. Then the browser starts working and parses the HTML to find all kinds of embedded assets like JavaScript and CSS. The browsers needs to go back to the server again and ask for these files. Server Push allows the server avoid this round trip and push the files directly together with HTML so no valuable time is lost. Many cloud hosting providers like Cloudflare start supporting HTTP/2 Server Push already: Announcing Support for HTTP/2 Server Push

Sounds great right? There is only one small problem: caching doesn’t work properly since Server Push is not aware of previous visits and will not check cached resources.  Server Push always tells the browser that it is should receive the files from the server, whatever happened before.

Service Workers to the rescue!

If you want to know what Service Works are, read the Google Web Fundamental guide: Service Workers: an Introduction

“A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don’t need a web page or user interaction. Today, they already include features like push notifications and background sync. In the future service workers will support other things like periodic sync or geofencing.”

Using Service Workers you can easily cache static files too and it is easy to understand since they are written in JavaScript! We are going to intercept the request for an asset and check if it is cached already on the device or not. If it is, just serve it from the local source instead of downloading it from the server.

Create a basic HTML template, containing an image and a JavaScript file we want to test on and register a new Service Worker: serviceworker.html

<!DOCTYPE html>
<html lang="nl">
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>HTTP/2 Server Push testing page</title>
	<meta name="description" content="Let me rank for Server Push, Google please?! Or I don't allow you caching this page :-)">

	<h1>I always want to rank for Server Push queries</h1>
	<p>Some random text that is not important right now</p>

	<img src="./images/very-important-image.jpg" alt="Server Push Example 1" width="500" height="500" />
	<script async src="./js/very-advanced-analytics-tracking.js"></script>

	if ('serviceWorker' in navigator) {
		navigator.serviceWorker.register('./serviceworker-serverpush-example.js').then(function(registration) { // If you did the right thing
			console.log('ServiceWorker registration successful with scope: ', registration.scope);
		}).catch(function(err) { // If you failed installing the library or did something else wrong
		console.log('ServiceWorker registration failed: ', err);

Next step is to make sure you have installed SW Toolbox: which is a small library to create your own service workers. Then use the following code to listen to anything happening on /push and the image and JS folders: serviceworker-serverpush-example.js

(global => {'use strict';


  // The obvious route for any image or js requests
  toolbox.router.get('/push', global.toolbox.fastest);
  toolbox.router.get('/images/(.*)', global.toolbox.fastest);
  toolbox.router.get('/js/(.*)', global.toolbox.fastest);

  // lets listen in on all page events
  global.addEventListener('install', event => event.waitUntil(global.skipWaiting()));
  global.addEventListener('activate', event => event.waitUntil(global.clients.claim()));

This will do the following:

  • On the first load it listen to everything that is pushed via HTTP/2 Server Push and caches on user device via the launched Service Worker
  • Next visit the Service Worker listen in again and will intercept if it recognize any cached resources
  • The result will be a smaller Waterfall charts in your loading speed tools 🙂

Leave a Comment.