Registry / devops / koa-static-cache

koa-static-cache

JSON →
library5.1.4jsnpmunverified

Static file serving middleware for Koa with caching, gzip support, and MD5 ETags. Current version 5.1.4, maintained as part of the koajs organization. Unlike koa-static, it caches files on initialization (preload) by default, supports in-memory buffering, pre-compressed gzip files, and alias mappings. No directory listing or index.html support. Suitable for production static asset serving with minimal overhead.

npm install koa-static-cache
INSTALL
IMPORT
SIG · KOA-STATIC-CACHE
K
koa-static-cache
devopsjavascriptv5.1.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import staticCache from 'koa-static-cache'
import { staticCache } from 'koa-static-cache'
Default export only, not a named export.
CommonJS require
const staticCache = require('koa-static-cache')
const { staticCache } = require('koa-static-cache')
CommonJS default import using require.

Sets up a Koa server with static file caching from 'public' directory, including gzip support and in-memory buffering.

import Koa from 'koa'; import staticCache from 'koa-static-cache'; import path from 'path'; const app = new Koa(); const publicDir = path.join(process.cwd(), 'public'); app.use(staticCache(publicDir, { maxAge: 365 * 24 * 60 * 60, gzip: true, buffer: true, preload: true })); app.listen(3000); console.log('Server running on http://localhost:3000');
Debug
Known issues
breakingVersion 5.x dropped support for Node.js < 7.6.0. Ensure your Node.js version is 7.6.0 or higher.
fix
Upgrade Node.js to version 7.6.0 or later.
affects: >=5.0.0
deprecatedThe 'options.dir' option is deprecated in favor of passing 'dir' as first argument.
fix
Pass directory path as first argument instead of using options.dir.
affects: >=4.0.0
gotchaBy default (preload: true), the middleware caches all files on initialization. Changes to files on disk after server start are not served until restart unless dynamic: true is set.
fix
Set options.dynamic = true to enable dynamic file loading for files not cached on initialization.
affects: >=1.0.0
gotchaThe library expects .gz files to be present on disk when usePrecompiledGzip: true. It does not compress on the fly.
fix
Pre-generate .gz versions of your static files, e.g., using a build step.
affects: >=1.0.0
Errors
Common errors & fixes
Error: ENOENT: no such file or directory, stat '/path/to/public'
The specified directory does not exist or the path is incorrect.
fix
Ensure the directory exists and the path is absolute (use path.join(__dirname, 'public') or path.resolve('public')).
TypeError: staticCache is not a function
Using named import instead of default import.
fix
Use `import staticCache from 'koa-static-cache'` or `const staticCache = require('koa-static-cache')`.
Error: Cannot find module 'koa-static-cache'
Package not installed or missing from node_modules.
fix
Run `npm install koa-static-cache` in your project directory.
Upgrade
Version history
5.1.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
koa-static-cache — npm install koa-static-cache · libregistry