Superstatic is an enhanced static web server that was built to power. It has fantastic support for HTML5 pushState applications, clean URLs, caching, and many other goodies.
Superstatic should be installed globally using npm:
For use via CLI
$ npm install -g superstatic
For use via API
npm install superstatic --save
By default, Superstatic will simply serve the current directory on port
3474. This works just like any other static server:
You can optionally specify the directory, port and hostname of the server:
$ superstatic public --port 8080 --host 127.0.0.1
Superstatic reads special configuration from a JSON file (either
firebase.json by default, configurable with
-c). This JSON file enables
enhanced static server functionality beyond simply serving files.
public: by default, Superstatic will serve the current working directory (or the
ancestor of the current directory that contains the configuration json being used).
This configuration key specifies a directory relative to the configuration file that
should be served. For example, if serving a Jekyll app, this might be set to
A directory passed as an argument into the command line app supercedes this configuration
.html files will automatically have their extensions
.html is used at the end of a filename, it will perform a 301 redirect
to the same path with
All paths have clean urls
Only specific paths get clean urls
rewrites: you can specify custom route recognition for your application by supplying
an object to the routes key. Use a single star
* to replace one URL segment or a
double star to replace an arbitrary piece of URLs. This works great for single page
apps. An example:
redirects: you can specify certain url paths to be redirected to another url by supplying configuration to the
redirects key. Path matching is similar to using custom routes.
redirects use the
301 HTTP status code by default, but this can be overridden by configuration.
Route segments are also supported in the
redirects configuration. Segmented
redirects also support custom status codes (see above):
In this example,
/old/custom-segment/path redirects to
headers: Superstatic allows you to set the response headers for certain paths as well:
trailingSlash: Have full control over whether or not your app has or doesn't have trailing slashes. By default, Superstatic will make assumptions for on the best times to add or remove the trailing slash. Other options include
true, which always adds a trailing slash, and
false, which always removes the trailing slash.
Superstatic is available as a middleware and a standalone Connect server. This means you can plug this into your current server or run your own static server using Superstatic's server.
var superstatic =var connect = ;var app =;app;
Instantiates middleware. See an example for detail on real world use.
options- Optional configuration:
false, render a 404 page from within Superstatic rather than calling through to the next middleware. Defaults to
config- A file path to your application's configuration file (see Configuration) or an object containing your application's configuration. If an object is provided, it will be merged into existing config in a
protect- Adds HTTP basic auth. Example:
env- A file path your application's environment variables file or an object containing values that are made available at the urls
/__/env.js. See the documentation detail on environment variables.
cwd- The current working directory to set as the root. Your application's
publicconfiguration option will be used relative to this.
compression- An option which controls superstatic's response compression. Pass in a standard
compression(req, res, next)Express middleware function to override the default compression behavior (for example, require shrink-ray to enable advanced compression schemes such as brotli, or require node.js' stock compression middleware yourself to change the compression quality and caching behavior). Any other truthy value will default to the stock node.js middleware.
var superstatic = server;var app = ;var server = app;
Since Superstatic's server is a barebones Connect server using the Superstatic middleware, see the Connect documentation on how to correctly instantiate, start, and stop the server.
Instantiates a Connect server, setting up Superstatic middleware, port, host, debugging, compression, etc.
options- Optional configuration. Uses the same options as the middleware, plus a few more options:
port- The port of the server. Defaults to
hostname- The hostname of the server. Defaults to
errorPage- A file path to a custom error page. Defaults to Superstatic's error page.
debug- A boolean value that tells Superstatic to show or hide network logging in the console. Defaults to
compression- A boolean value that tells Superstatic to serve gzip/deflate compressed responses based on the request Accept-Encoding header and the response Content-Type header. Defaults to
gzip[DEPRECATED] - A boolean value which is now equivalent in behavior to
compression. Defaults to
Superstatic reads content from providers. The default provider for Superstatic reads from the local filesystem. Other providers can be substituted when initializing Superstatic:
Implementing a new provider is quite simple. You simply need to create a function that takes a request and pathname and returns a Promise. The Promise should:
nullwhen content isn't found (i.e. a 404 response).
The metadata object returned by a provider needs the following properties:
A simple in-memory store provider can be found at
this repo as a simple reference example of a provider.
Note: The pathname will be URL-encoded. You should make sure your provider properly handles files with non-standard characters (spaces, unicode, etc).
In superstatic module directory:
npm install npm test
We LOVE open source and open source contributors. If you would like to contribute to Superstatic, please review our contributing guidelines before you jump in and get your hands dirty.