HTTP/2 webserver for static files
Service-Worker-Allowedheader to enable Service Workers on any path.
cache-controlheader for revved files.
npm i -g http2server
http2server <path> [options]
<path> is the directory to serve. Defaults to
./public if it exists, otherwise the current directory.
HTML fallback for single page apps. This path is either absolute, or relative to
Files to push alongside the fallback. Default:
Files not to push. Default:
Port to listen on. Defaults to
process.env.PORT if the
PORT environment variable is set, otherwise
Path to ssl cert file. Default:
cert.pem See: HTTPS Certificate
Path to ssl key file. Default:
key.pem See: HTTPS Certificate
One or more intermadiate certificates. Default:
ca.pem See: HTTPS Certificate
One or more regular expressions to match file paths of assets that never change. Default:
hex emoji See: Caching Policy
Service worker source files. These are not pushed with the fallback. Default:
Set the value of the
Service-Worker-Allowed header. Default:
Open browser window after starting the server. Default:
Suppress log messages from output. Default:
Show version number
Page load time is a largely function of latency (round trip time × delays) and aggregate volume (number × size of assets).
Latency is minimised by using HTTP/2 Server Push to deliver any necessary assets to the browser alongside the HTML. When the browser parses the HTML it does not need to make a round trip request to fetch styles, scripts, and other assets. They are already in its cache or actively being pushed by the server as quickly as network conditions allow.
Volume is reduced by using strong compression (HPACK, Brotli, etc), and by avoiding sending redundant data.
The HTTP/1 approach was to use file signatures (Etags) and timestamps to invalidate cached responses. This requires many expensive round trips where the browser checks with the server if any files have been modified.
Cache Digests to the rescue! Using a clever technique, called Golomb-Rice Coded Bloom Filters, a compressed list of cached responses is sent by the browser to the server. Now the server can avoid pushing assets that are fresh in the browser's cache.
With Server Push and Cache Digests the best practice is to have many small files that can be cached and updated atomically, instead of large, concatenated blobs.
Browsers do not yet support cache digests natively so Service Workers and the Cache API are used to implement them. A cookie-based fallback is available for browsers that lack Service Worker support.
While the HTTP/2 specification allows unencrypted connections, web browsers strictly enforce HTTPS.
If no certificate and key are provided, one pair will be auto-generated. The generated certificate is only valid for
localhost. It is stored in
~/.http2server. As a developer convenience, the certificate is added as trusted to the operating system so browsers will accept the certificate. A password dialog may appear to confirm. This is currently only supported on macOS and Linux.
In production use Let's Encrypt or any other trusted, signed certificate.
Intermediate certificates are stapled to the OCSP response to speed up the TLS handshake.
By default files are served with the header
cache-control: public, must-revalidate.
File paths that match the patterns set by the
--immutable option are considered to never, ever change their contents. They are served with the header
cache-control: public, max-age=31536000, immutable. This tells browsers never to revalidate these resources.
The default immutable patterns are:
hex— Matches hexadecimal hash revved files. Example:
emoji— Matches emoji revved files. Example: