Skip to content

Commit

Permalink
change etag and lastModified setting
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 9, 2016
1 parent 8ef3a64 commit bd011d4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ app.use(serve(staticPath, {
},
dotfiles: 'allow',
denyQuerystring: true,
disableETag: true,
disableLastModified: true,
etag: false,
lastModified: false,
'404': 'next',
}));
const port = process.env.PORT || 10000;
Expand All @@ -50,23 +50,23 @@ console.dir('server listen on:' + port);

### options

- `maxAge` static file's http response header, Cache-Control max-age
- `maxAge` Static file's http response header, Cache-Control max-age, default is 0.

- `sMaxAge` static file's http response header, Cache-Control s-maxage for cache application(eg. varnish)
- `sMaxAge` Static file's http response header, Cache-Control s-maxage for cache application(eg. varnish). If not set, it will be Math.min(3600, maxAge).

- `headers` default header
- `headers` The default header.

- `dotfiles` dot file access permission, it can be 'allow', 'deny', 'ignore'. Default is 'ignore'
- `dotfiles` Dot file access permission, it can be 'allow', 'deny', 'ignore'. Default is 'ignore'.

- `denyQuerystring` deny query string, default is `false`. If using a http cache server(varnish) for the static files, query string should be denied.
- `denyQuerystring` Deny query string, default is `false`. If using a http cache server(varnish) for the static files, query string should be denied. Otherwise there will be different cache for the same file.

- `charset` default content charset
- `charset` Default content charset.

- `disableETag` disable etag header
- `etag` Enable or disable etag generation, default is true.

- `disableLastModified` disable last-modified header
- `lastModified` Set the Last-Modified header to the last modified date of the file on the OS, default is true.

- `404` set not found handler. If set 'next', it will call next when not found, otherwise will throw an error (404).
- `404` Set not found handler. If set 'next', it will call next when not found, otherwise will throw an error (404).

## License

Expand Down
4 changes: 2 additions & 2 deletions examples/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ app.use(serve(staticPath, {
},
dotfiles: 'allow',
denyQuerystring: true,
disableETag: true,
disableLastModified: true,
etag: false,
lastModified: false,
'404': 'next',
}));
const port = process.env.PORT || 10000;
Expand Down
34 changes: 15 additions & 19 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ function exists(file) {
* @return {[type]} [description]
*/
function serve(staticPath, options) {
const opts = options || {};
const opts = Object.assign({
etag: true,
lastModified: true,
}, options);
debug('static path:%s, opts:%j', staticPath, opts);
if (options && (options.disableETag || options.disableLastModified)) {
/* eslint max-len:0 no-console:0 */
console.warn('disableETag and disableLastModified is deprecated, please use etag and lastModified.');
}
const maxAge = opts.maxAge || 0;
const sMaxAge = opts.sMaxAge || Math.min(3600, maxAge);
const root = path.resolve(staticPath);
Expand All @@ -46,17 +53,6 @@ function serve(staticPath, options) {
const defaultCharset = opts.charset || 'utf-8';
opts.dotfiles = accessList.indexOf(opts.dotfiles) === -1 ? 'ignore' : opts.dotfiles;

const getHeaders = () => {
const headers = {};
if (!defaultHeaders) {
return headers;
}
Object.keys(defaultHeaders).forEach((key) => {
headers[key] = defaultHeaders[key];
});
return headers;
};

return (ctx, next) => {
if (ctx.status !== 404) {
return next();
Expand All @@ -67,10 +63,10 @@ function serve(staticPath, options) {
const file = path.join(root, ctx.path);
const basename = path.basename(file);
let access = accessList[0];
if (basename.charAt(0) === '.') {
access = opts.dotfiles;
} else if (file.indexOf(root) !== 0) {
if (file.indexOf(root) !== 0) {
access = 'deny';
} else if (basename.charAt(0) === '.') {
access = opts.dotfiles;
}
switch (access) {
case 'allow':
Expand All @@ -84,11 +80,11 @@ function serve(staticPath, options) {
return exists(file).then((stats) => {
const type = mime.lookup(file);
const charset = mime.charsets.lookup(type) || defaultCharset;
const headers = getHeaders();
if (!options || !options.disableETag) {
const headers = Object.assign({}, defaultHeaders);
if (opts.etag) {
headers.ETag = etag(stats);
}
if (!options || !options.disableLastModified) {
if (opts.lastModified) {
headers['Last-Modified'] = stats.mtime.toUTCString();
}
headers['Content-Length'] = stats.size;
Expand All @@ -109,7 +105,7 @@ function serve(staticPath, options) {
}
return next();
}).catch(err => {
if (options && options['404'] === 'next') {
if (opts['404'] === 'next') {
return next();
}
throw err;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koa-static-serve",
"description": "static serve middlware for koa",
"version": "2.0.9",
"version": "2.1.0",
"author": "Tree Xie <vicansocanbico@gmail.com>",
"keywords": [
"koa",
Expand Down
4 changes: 2 additions & 2 deletions test/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('serve', function() {
it('should disable ETag', done => {
const app = new Koa();
app.use(serve(assets, {
disableETag: true,
etag: false,
}));
request(app.listen())
.get('/test.js')
Expand All @@ -206,7 +206,7 @@ describe('serve', function() {
it('should disable Last-Modified', done => {
const app = new Koa();
app.use(serve(assets, {
disableLastModified: true,
lastModified: false,
}));
request(app.listen())
.get('/test.js')
Expand Down

0 comments on commit bd011d4

Please sign in to comment.