Accelerating Node.js applications with HTTP/2 Server Push ========================================================= In April, we [announced support for HTTP/2 Server][3] Push via the HTTP Link header. My coworker John has demonstrated how easy it is to [add Server Push to an example PHP application][4]. ![](https://blog.cloudflare.com/content/images/2016/08/489477622_594bf9e3d9_z.jpg) We wanted to make it easy to improve the performance of contemporary websites built with Node.js. we developed the netjet middleware to parse the generated HTML and automatically add the Link headers. When used with an example Express application you can see the headers being added: ![](https://blog.cloudflare.com/content/images/2016/08/2016-08-11_13-32-45.png) We use Ghost to power this blog, so if your browser supports HTTP/2 you have already benefited from Server Push without realizing it! More on that below. In netjet, we use the PostHTML project to parse the HTML with a custom plugin. Right now it is looking for images, scripts and external stylesheets. You can implement this same technique in other environments too. Putting an HTML parser in the response stack has a downside: it will increase the page load latency (or "time to first byte"). In most cases, the added latency will be overshadowed by other parts of your application, such as database access. However, netjet includes an adjustable LRU cache keyed by ETag headers, allowing netjet to insert Link headers quickly on pages already parsed. If you are designing a brand new application, however, you should consider storing metadata on embedded resources alongside your content, eliminating the HTML parse, and possible latency increase, entirely. Netjet is compatible with any Node.js HTML framework that supports Express-like middleware. Getting started is as simple as adding netjet to the beginning of your middleware chain. ``` var express = require('express'); var netjet = require('netjet'); var root = '/path/to/static/folder'; express() .use(netjet({ cache: { max: 100 } })) .use(express.static(root)) .listen(1337); ``` With a little more work, you can even use netjet without frameworks. ``` var http = require('http'); var netjet = require('netjet'); var port = 1337; var hostname = 'localhost'; var preload = netjet({ cache: { max: 100 } }); var server = http.createServer(function (req, res) { preload(req, res, function () { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('