/hasset

Append unique hashes to assets referenced in your views

Primary LanguageJavaScriptMIT LicenseMIT

Append unique hashes to assets referenced in your views to aggressively cache them while guaranteeing that clients receive the most recent versions.

Installation

npm i hasset

Usage

Running the following command will scan asset files found in the roots path(s) and replace their references with hashed versions in the refs path(s):

npx hasset --roots="path/to/scan/assets1/,path/to/scan/assets2/" --refs="views/path/to/append/hashes1/,views/path/to/append/hashes2/"

Example (Fastify)

Register @fastify/static:

await fastify.register(import("@fastify/static"), {
  root: new URL("assets/", import.meta.url).pathname,
  prefix: "/p/assets/",
  wildcard: false,
  index: false,
  immutable: true,
  maxAge: process.env.NODE_ENV === "production" ? 31536000 * 1000 : 0,
});

Add the hasset command to the build script:

"scripts": {
  "build": "npx hasset --roots=assets/ --refs=views/,routes/",
},

Make sure to npm run build in Dockerfile:

FROM node:latest

WORKDIR /app

COPY package*.json ./

RUN npm ci --include=dev

COPY . .

RUN npm run build

RUN npm prune --production

CMD ["npm", "start"]

Demo

A full project that uses hasset can be found in the example folder:

cd example

npm i

npm run build

node .