Skip to content

Commit

Permalink
feat(middleware) no cache by default, enable using cache opt, Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Jan 13, 2015
1 parent 20423ad commit fba72c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Enable log for requests, default `false`.

Map pathname for debug. e.g. `paths: [['/a/b/c/', '']]`

### cache

Enable 304 cache.

## LICENSE

Copyright (c) 2014 sorrycc. Licensed under the MIT license.
14 changes: 8 additions & 6 deletions lib/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ module.exports = function(root, opts) {
return yield* next;
}

var modifiedTime = util.getModifiedTime(file);
this.set('Last-Modified', modifiedTime);
if (!util.isModified(this.header, modifiedTime)) {
debug('file %s is not modified', file.path);
this.status = 304;
return;
if (opts.cache) {
var modifiedTime = util.getModifiedTime(file);
this.set('Last-Modified', modifiedTime);
if (!util.isModified(this.header, modifiedTime)) {
debug('file %s is not modified', file.path);
this.status = 304;
return;
}
}

log('>> ServeSPM %s < ./%s',
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,19 @@ function wrap(server, middleware) {
});
});

it('isModified', function(done) {
it('isModified disable', function(done) {
app = server();
app.use(middleware(join(fixtures, 'parser')));
request(app.listen())
.get('/index.js')
.expect(200, done);
});

it('isModified enable', function(done) {
app = server();
app.use(middleware(join(fixtures, 'parser'), {cache:true}));
request(app.listen())
.get('/index.js')
.set('if-modified-since', '2046 8-14 13:52:38')
.expect(304, done);
});
Expand Down

0 comments on commit fba72c4

Please sign in to comment.