Arrow keys: ← previous · → next 31 of 37
@nodedk/etag
0.1.0 stable server

@nodedk/etag#

Weak ETag response handling for NodeDK servers. No dependencies.

Usage#

Call etag(state) from the server's after hook, once state.result is ready:

var server = require('@nodedk/server')
var etag = require('@nodedk/etag')

server({
  after: function (state) {
    etag(state)
  }
})

The function reads state.req, state.res, and state.result. It generates a weak ETag (W/"...") from the response string using Node's built-in MD5 hash. When If-None-Match exactly matches the generated tag, it sets status 304. The server sends the response without a body.

Only successful (200) GET responses with a string result are processed. Responses with Cache-Control: no-store, or whose headers have been sent, whose response has ended, or whose connection is destroyed are skipped. Existing cache policies are preserved; otherwise private, no-cache is set.

The page still renders before the ETag is checked. This saves response transfer, not rendering work. Matching currently supports a single exact ETag value; comma-separated lists and * are not handled.

Development#

npm test

Created by Vidar Eldøy

Tools for business systems.