Gathering detailed insights and metrics for @eggjs/koa-static-cache
Gathering detailed insights and metrics for @eggjs/koa-static-cache
Gathering detailed insights and metrics for @eggjs/koa-static-cache
Gathering detailed insights and metrics for @eggjs/koa-static-cache
npm install @eggjs/koa-static-cache
Typescript
Module System
Min. Node Version
Node Version
NPM Version
72.6
Supply Chain
97.9
Quality
85.5
Maintenance
100
Vulnerability
100
License
TypeScript (99.83%)
JavaScript (0.17%)
Total Downloads
5,307
Last Day
16
Last Week
60
Last Month
598
Last Year
5,307
MIT License
1 Stars
165 Commits
1 Forks
1 Branches
22 Contributors
Updated on Mar 12, 2025
Minified
Minified + Gzipped
Latest Version
6.1.0
Package Id
@eggjs/koa-static-cache@6.1.0
Unpacked Size
59.49 kB
Size
13.11 kB
File Count
11
NPM Version
10.8.2
Node Version
20.18.3
Published on
Mar 12, 2025
Cumulative downloads
Total Downloads
Last Day
-30.4%
16
Compared to previous day
Last Week
25%
60
Compared to previous week
Last Month
-57.3%
598
Compared to previous month
Last Year
0%
5,307
Compared to previous year
Static cache middleware for koa.
Differences between this library and other libraries such as static:
index.html
support..gz
files if present on disk, like nginx gzip_static moduleForked from https://github.com/koajs/static-cache, refactor with TypeScript to support CommonJS and ESM both.
1npm install @eggjs/koa-static-cache
1const path = require('path'); 2const { staticCache } = require('@eggjs/koa-static-cache'); 3 4app.use(staticCache(path.join(__dirname, 'public'), { 5 maxAge: 365 * 24 * 60 * 60 6}));
options.dir
(str) - the directory you wish to serve, default to process.cwd
.options.maxAge
(int) - cache control max age for the files, 0
by default.options.cacheControl
(str) - optional cache control header. Overrides options.maxAge
.options.buffer
(bool) - store the files in memory instead of streaming from the filesystem on each request.options.gzip
(bool) - when request's accept-encoding include gzip, files will compressed by gzip.options.usePrecompiledGzip
(bool) - try use gzip files, loaded from disk, like nginx gzip_staticoptions.alias
(obj) - object map of aliases. See below.options.prefix
(str) - the url prefix you wish to add, default to ''
.options.dynamic
(bool) - dynamic load file which not cached on initialization.options.filter
(function | array) - filter files at init dir, for example - skip non build (source) files. If array set - allow only listed filesoptions.preload
(bool) - caches the assets on initialization or not, default to true
. always work together with options.dynamic
.options.files
(obj) - optional files object. See below.For example, if you have this alias
object:
1const options = { 2 alias: { 3 '/favicon.png': '/favicon-32.png' 4 } 5}
Then requests to /favicon.png
will actually return /favicon-32.png
without redirects or anything.
This is particularly important when serving favicons as you don't want to store duplicate images.
You can pass in an optional files object. This allows you to do two things:
Instead of doing:
1app.use(staticCache('/public/js')) 2app.use(staticCache('/public/css'))
You can do this:
1const files = {};
2
3// Mount the middleware
4app.use(staticCache('/public/js', {}, files));
5
6// Add additional files
7staticCache('/public/css', {}, files);
The benefit is that you'll have one less function added to the stack as well as doing one hash lookup instead of two.
For example, if you want to change the max age of /package.json
, you can do the following:
1const files = {}; 2 3app.use(staticCache('/public', { 4 maxAge: 60 * 60 * 24 * 365 5}, files)); 6 7files['/package.json'].maxAge = 60 * 60 * 24 * 30;
You can pass in a lru cache instance which has tow methods: get(key)
and set(key, value)
.
1const LRU = require('lru-cache'); 2const files = new LRU({ max: 1000 }); 3 4app.use(staticCache({ 5 dir: '/public', 6 dynamic: true, 7 files, 8}));
Made with contributors-img.
No vulnerabilities found.
No security vulnerabilities found.