npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@epimodev/sarv

v1.1.0

Published

a static file server to serve spa with compressed files, based on polka and inspired by sirv

Downloads

43

Readme

Sarv: another static file server with brotli support

Presentation

Sarv is a new static file server which was developped to serve SPA (single page applications) with compressed files (brotli or gzip). There isn't compression during the request so files should be compressed before server start.

for performance improvements, sarv check files on start (technique inspired by sirv), this can lead to those errors:

  • if a file is deleted, request will send a 500 error instead of a 404
  • if a file is created, request will send a 404 error instead of a 200 with file

Motivation

The main goal of this project is creating a static file server able to serve brotli compressed files. According to this article of web.dev we can easly add brotli support on express static server. Then I discover sirv which seems to be a faster alternative (because it doesn't check file existance on every request). So I tried to use the same technique explained in web.dev article by using polka and sirv, but I've got some issues:

  • content-type is set by sirv depending on file name. So by replacing path app.js by app.js.br, content-type become text/html instead of application/javascript. Even if we can set content-type header in a middleware or in setHeaders option, content-type is overwrite just before sending requested file.
  • sirv has an option to send etag header for browser cache but it doesn’t check if-none-match request header to send a 304 response without the file.

So I create sarv, another static file server to workaround those issues. It's based on polka and most of the code is inspired by sirv but there are some differences:

  • send pre-compressed brolti or gzip files (depending on accept-encoding header)
  • force by default the browser to check if file hasn't change before getting it from cache (by using cache-control and etag headers)
  • send 304 response without file if the browser has already load the file
  • add some info in log (content-encoding and content-length)

Instalation

# install locally in your project
yarn add @epimodev/sarv
# install globally
yarn global add @epimodev/sarv

# or with npm
npm install @epimodev/sarv
# install globally
npm install --global @epimodev/sarv

Usage

# start a server for `public` folder
sarv public

# start a server binded to 8080 port
sarv public --port 8080

# start a server which fallback not found to index.html (for single page application)
sarv public --fallback /index.html

# display all options
sarv --help

Options

Description
  Run a static file server

Usage
  $ sarv [dir] [options]

Options
  -h, --host        Hostname to bind  (default localhost)
  -p, --port        Port to bind  (default 3000)
  -i, --index       Define index file name  (default index.html)
  -f, --fallback    Define fallback file
  -m, --maxage      Define max-age value (in sec) in "Cache-Control" header  (default 1209600)
  -v, --verbose     Display content encoding and content length in logs
  -v, --version     Displays current version
  -h, --help        Displays this message

Examples
  $ sarv public
  $ sarv public --port 8080
  $ sarv public --fallback /index.html