Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ app.use(staticCache(path.join(__dirname, 'public'), {
- `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_static
- `options.setHeaders` (function) - optional `function (ctx, file)` hook to set custom response headers for matched files.
- `options.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.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function staticCache(dir, options, files) {
dir = path.normalize(dir)
var enableGzip = !!options.gzip
var filePrefix = path.normalize(options.prefix.replace(/^\//, ''))
var setHeaders = typeof options.setHeaders === 'function'
? options.setHeaders
: null

// option.filter
var fileFilter = function () { return true }
Expand Down Expand Up @@ -99,6 +102,7 @@ module.exports = function staticCache(dir, options, files) {
ctx.length = file.zipBuffer ? file.zipBuffer.length : file.length
ctx.set('cache-control', file.cacheControl || 'public, max-age=' + file.maxAge)
if (file.md5) ctx.set('content-md5', file.md5)
if (setHeaders) setHeaders(ctx, file)

if (ctx.method === 'HEAD') return

Expand Down
53 changes: 53 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,59 @@ describe('Static Cache', function () {
.expect(200, done)
})

it('should set custom headers for streamed files', function (done) {
var app = new Koa()
var seenFile
app.use(staticCache(path.join(__dirname, '..'), {
setHeaders: function (ctx, file) {
seenFile = file
ctx.set('X-Static-Cache', file.type)
},
filter(file) {
return !file.includes('node_modules')
}
}))

request(app.listen())
.get('/index.js')
.expect('X-Static-Cache', /javascript/)
.expect(200, function (err) {
if (err)
return done(err)

seenFile.type.should.match(/javascript/)
seenFile.path.should.endWith('index.js')
done()
})
})

it('should set custom headers for buffered files', function (done) {
var app = new Koa()
var seenFile
app.use(staticCache(path.join(__dirname, '..'), {
buffer: true,
setHeaders: function (ctx, file) {
seenFile = file
ctx.set('X-Static-Length', String(file.length))
},
filter(file) {
return !file.includes('node_modules')
}
}))

request(app.listen())
.get('/index.js')
.expect('X-Static-Length', String(fs.statSync('index.js').size))
.expect(200, function (err) {
if (err)
return done(err)

should.exist(seenFile.buffer)
seenFile.path.should.endWith('index.js')
done()
})
})

it('should set Last-Modified if file modified and not buffered', function (done) {
setTimeout(function () {
var readme = fs.readFileSync('README.md', 'utf8')
Expand Down