You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
8.8 KiB

1 month ago
  1. # sirv ![CI](https://github.com/lukeed/sirv/workflows/CI/badge.svg)
  2. > The optimized and lightweight middleware for serving requests to static assets
  3. You may use `sirv` as a *very* fast and lightweight alternative to [`serve-static`](https://www.npmjs.com/package/serve-static).
  4. The massive performance advantage over `serve-static` is explained by **not** relying on the file system for existence checks on every request. These are expensive interactions and must be avoided whenever possible! Instead, when not in "dev" mode, `sirv` performs all its file-system operations upfront and then relies on its cache for future operations.
  5. This middleware will work out of the box for [Polka](https://github.com/lukeed/polka), Express, and other Express-like frameworks. It will also work with the native `http`, `https` and `http2` modules. It requires _very_ little effort to modify/wrap it for servers that don't accept the `(req, res, next)` signature.
  6. :bulb: For a feature-complete CLI application, check out the sibling [`sirv-cli`](https://github.com/lukeed/sirv/tree/master/packages/sirv-cli) package as an alternative to [`zeit/serve`](https://github.com/zeit/serve)~!
  7. ## Install
  8. ```
  9. $ npm install --save sirv
  10. ```
  11. ## Usage
  12. ```js
  13. const sirv = require('sirv');
  14. const polka = require('polka');
  15. const compress = require('compression')();
  16. // Init `sirv` handler
  17. const assets = sirv('public', {
  18. maxAge: 31536000, // 1Y
  19. immutable: true
  20. });
  21. polka()
  22. .use(compress, assets)
  23. .use('/api', require('./api'))
  24. .listen(3000, err => {
  25. if (err) throw err;
  26. console.log('> Ready on localhost:3000~!');
  27. });
  28. ```
  29. ## API
  30. ### sirv(dir, opts={})
  31. Returns: `Function`
  32. The returned function is a middleware in the standard Express-like signature: `(req, res, next)`, where `req` is the [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage), `res` is the [`http.ServerResponse`](https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_class_http_serverresponse), and `next` (in this case) is the function to call if no file was found for the given path.
  33. When defined, a `next()` callback is always called _instead of_ the [`opts.onNoMatch`](#optsonnomatch) callback. However, unlike `onNoMatch`, your `next()` is given no arguments.
  34. #### dir
  35. Type: `String`<br>
  36. Default: `.`
  37. The directory from which to read and serve assets. It is resolved to an absolute path &mdash; you must provide an absolute path yourself if `process.cwd()` is not the correct assumption.
  38. #### opts.dev
  39. Type: `Boolean`<br>
  40. Default: `false`
  41. Enable "dev" mode, which disables/skips caching. Instead, `sirv` will traverse the file system ***on every request***.
  42. Additionally, `dev` mode will ignore `maxAge` and `immutable` as these options generate a production-oriented `Cache-Control` header value.
  43. > **Important:** Do not use `dev` mode in production!
  44. #### opts.etag
  45. Type: `Boolean`<br>
  46. Default: `false`
  47. Generate and attach an `ETag` header to responses.
  48. > **Note:** If an incoming request's [`If-None-Match` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match) matches the `ETag` value, a `304` response is given.
  49. #### opts.dotfiles
  50. Type: `Boolean`<br>
  51. Default: `false`
  52. Allow requests to dotfiles (files or directories beginning with a `.`).
  53. > **Note:** Requests to [`/.well-known/*`](https://tools.ietf.org/html/rfc8615) are always allowed.
  54. #### opts.extensions
  55. Type: `Array<String>`<br>
  56. Default: `['html', 'htm']`
  57. The file extension fallbacks to check for if a pathame is not initially found. For example, if a `/login` request cannot find a `login` filename, it will then look for `login.html` and `login.htm` before giving up~!
  58. > **Important:** Actually, `sirv` will **also** look for `login/index.html` and `login/index.htm` before giving up.
  59. #### opts.gzip
  60. Type: `Boolean`<br>
  61. Default: `false`
  62. Determine if `sirv` look for **precompiled** `*.gz` files.<br>
  63. Must be enabled _and_ the incoming request's [`Accept Encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) must include "gzip" in order for `sirv` to search for the gzip'd alternative.
  64. > **Note:** The `.gz` assumption also applies to the `opts.extensions` list.
  65. ```js
  66. // NOTE: PSEUDO CODE
  67. // Showing lookup logic
  68. // Request: [Accept-Encoding: gzip] "/foobar.jpg"
  69. lookup([
  70. '/foobar.jpg.gz', '/foobar.jpg',
  71. '/foobar.jpg.html.gz', '/foobar.jpg/index.html.gz',
  72. '/foobar.jpg.htm.gz', '/foobar.jpg/index.htm.gz',
  73. '/foobar.jpg.html', '/foobar.jpg/index.html',
  74. '/foobar.jpg.htm', '/foobar.jpg/index.htm',
  75. ]);
  76. // Request: [Accept-Encoding: gzip] "/"
  77. lookup([
  78. '/index.html.gz',
  79. '/index.htm.gz',
  80. '/index.html',
  81. '/index.htm',
  82. ]);
  83. ```
  84. #### opts.brotli
  85. Type: `Boolean`<br>
  86. Default: `false`
  87. Determine if `sirv` look for **precompiled** `*.br` files.<br>
  88. Must be enabled _and_ the incoming request's [`Accept Encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) must include either "br" or "brotli" in order for `sirv` to search for the brotli-compressed alternative.
  89. > **Note:** The `.br` assumption also applies to the `opts.extensions` list.
  90. When both `opts.broli` and `opts.gzip` are enabled &mdash; and all conditions are equal &mdash; then the brotli variant always takes priority.
  91. ```js
  92. // NOTE: PSEUDO CODE
  93. // Showing lookup logic
  94. // Request: [Accept-Encoding: br] "/foobar.jpg"
  95. lookup([
  96. '/foobar.jpg.br', '/foobar.jpg',
  97. '/foobar.jpg.html.br', '/foobar.jpg/index.html.br',
  98. '/foobar.jpg.htm.br', '/foobar.jpg/index.htm.br',
  99. '/foobar.jpg.html', '/foobar.jpg/index.html',
  100. '/foobar.jpg.htm', '/foobar.jpg/index.htm',
  101. ]);
  102. // Request: [Accept-Encoding: br,gz] "/"
  103. lookup([
  104. '/index.html.br'
  105. '/index.htm.br'
  106. '/index.html.gz'
  107. '/index.htm.gz'
  108. '/index.html'
  109. '/index.htm'
  110. ]);
  111. ```
  112. #### opts.maxAge
  113. Type: `Number`<br>
  114. Default: `undefined`
  115. Enables the `Cache-Control` header on responses and sets the `max-age` value (in seconds).<br>
  116. For example, `maxAge: 31536000` is equivalent to one year.
  117. #### opts.immutable
  118. Type: `Boolean`<br>
  119. Default: `false`
  120. Appends the [`immutable` directive](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Revalidation_and_reloading) on your `Cache-Control` header, used for uniquely-named assets that will not change!
  121. > **Important:** Will only work if `opts.maxAge` has a value defined!
  122. #### opts.single
  123. Type: `Boolean` or `String`<br>
  124. Default: `false`
  125. Treat the directory as a single-page application.
  126. When `true`, the directory's index page (default `index.html`) will be sent if the request asset does not exist.<br>
  127. You may pass a `string` value to use a file _instead of_ `index.html` as your fallback.
  128. For example, if "/about" is requested but no variants of that file exist, then the response for "/" is sent instead:
  129. ```js
  130. // Note: This is psuedo code to illustrate what's happening
  131. // Request: "/about"
  132. let file = find(['/about', '/about.html', '/about.htm', '/about/index.html', '/about.htm']);
  133. if (file) {
  134. send(file);
  135. } else if (opts.single === true) {
  136. file = find(['/', '/index.html', '/index.htm']);
  137. send(file);
  138. } else if (typeof opts.single === 'string') {
  139. file = find([opts.single]);
  140. send(file);
  141. } else {
  142. // next() or 404
  143. }
  144. ```
  145. #### opts.ignores
  146. Type: `false` or `Array<String | RegExp>`
  147. Specify paths/patterns that should ignore the fallback behavior that `opts.single` provides.
  148. By default, any asset-like path (URLs that end with an extension) will be ignored. This means that, for example, if `/foobar.jpg` is not found, a `404` response is sent instead of the `index.html` fallback.
  149. Additionally, any `/.well-known/*` pathname ignores the fallback – as do all other dotfile requests when `opts.dotfiles` is enabled.
  150. Any string value(s) will be passed through `new RegExp(value, 'i')` directly.
  151. Finally, you may set `ignores: false` to disable ***all*** ignores, including the defaults. Put differently, this will fallback ***all*** unknown pathnames to your `index.html` (or custom `opts.single` value).
  152. > **Important:** Only has an effect if `opts.single` is enabled.
  153. #### opts.onNoMatch
  154. Type: `Function`
  155. A custom function to run if a file cannot be found for a given request. <br>By default, `sirv` will send a basic `(404) Not found` response.
  156. The function receives the current `req <IncomingMessage>, res <ServerResponse>` pair for as its two arguments.
  157. > **Note:** This won't run if a `next` callback has been provided to the middleware; see [`sirv`](#sirvdir-opts) description.
  158. #### opts.setHeaders
  159. Type: `Function`
  160. A custom function to append or change any headers on the outgoing response. There is no default.
  161. Its signature is `(res, pathname, stats)`, where `res` is the `ServerResponse`, `pathname` is incoming request path (stripped of queries), and `stats` is the file's result from [`fs.statSync`](https://nodejs.org/api/fs.html#fs_fs_statsync_path).
  162. ## License
  163. MIT © [Luke Edwards](https://lukeed.com)