Skip to content

Commit b28067a

Browse files
committed
feat: cache: false now sets no-cache (as do 0 or negative values). null avoids setting any; fixes #117
BREAKING CHANGE: Set to `null` instead to reproduce old behavior
1 parent cf4d11a commit b28067a

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- **Breaking change**: Add `type: 'module'` and `exports` to `package.json`;
99
change internal CJS path
1010
- **Breaking change**: avoid serving hidden files by default (reenable with `--serve-hidden`/`serveHidden`)
11+
- **Breaking change**: `cache: false` now sets `no-cache` (as do 0 or
12+
negative values). Set to `null` instead to reproduce old behavior
1113
- Security: Fix dependency vulnerabilities by switching from `optimist` to
1214
`command-line-basics` (@brettz9)
1315
- Security: Update `mime` and `colors` (@fidian) and pin `colors`

lib/node-static.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function tryStat(p, callback) {
6262
* gzip?: boolean|RegExp,
6363
* headers?: http.OutgoingHttpHeaders,
6464
* serverInfo?: string|null,
65-
* cache?: boolean|number|Record<string, number>,
65+
* cache?: null|boolean|number|Record<string, number>,
6666
* serveHidden?: boolean,
6767
* defaultExtension?: string,
6868
* transform?: TransformCallback
@@ -96,10 +96,14 @@ class Server extends events.EventEmitter {
9696
if ('cache' in this.options) {
9797
if (typeof(this.options.cache) === 'number') {
9898
this.cache = {'**': this.options.cache};
99+
} else if (this.options.cache === null) {
100+
this.cache = {};
99101
} else if (typeof(this.options.cache) === 'object') {
100102
this.cache = this.options.cache;
101103
} else if (!this.options.cache) {
102-
this.cache = {};
104+
this.cache = {
105+
'**': 0
106+
};
103107
}
104108
}
105109

@@ -721,8 +725,12 @@ class Server extends events.EventEmitter {
721725
return _headers;
722726
}
723727
const maxAge = this.getMaxAge(req.url);
724-
if (typeof(maxAge) === 'number') {
725-
_headers['cache-control'] = 'max-age=' + maxAge;
728+
if (typeof maxAge === 'number') {
729+
if (maxAge > 0) {
730+
_headers['cache-control'] = 'max-age=' + maxAge;
731+
} else {
732+
_headers['cache-control'] = 'no-cache';
733+
}
726734
}
727735
return _headers;
728736
}
@@ -731,14 +739,11 @@ class Server extends events.EventEmitter {
731739
* @param {string} requestUrl
732740
*/
733741
getMaxAge (requestUrl) {
734-
if (this.cache) {
735-
for (const pattern in this.cache) {
736-
if (minimatch(requestUrl, pattern)) {
737-
return this.cache[pattern];
738-
}
742+
for (const pattern in this.cache) {
743+
if (minimatch(requestUrl, pattern)) {
744+
return this.cache[pattern];
739745
}
740746
}
741-
return false;
742747
}
743748
}
744749

test/integration/node-static-test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,11 @@ describe('node-static', function () {
869869
});
870870
});
871871

872-
describe('once an http server is listening with false custom cache configuration', function () {
872+
describe('once an http server is listening with null custom cache configuration', function () {
873873
beforeEach(async function () {
874874
await setupStaticServer(this);
875875
fileServer = new statik.Server(__dirname + '/../fixtures', {
876-
cache: false
876+
cache: null
877877
});
878878
});
879879
afterEach(async function () {
@@ -897,4 +897,33 @@ describe('node-static', function () {
897897
assert.equal(response.headers.get('cache-control'), null, 'should not respond with cache-control');
898898
});
899899
});
900+
901+
describe('once an http server is listening with false custom cache configuration', function () {
902+
beforeEach(async function () {
903+
await setupStaticServer(this);
904+
fileServer = new statik.Server(__dirname + '/../fixtures', {
905+
cache: false
906+
});
907+
});
908+
afterEach(async function () {
909+
this.server.close();
910+
});
911+
912+
it('requesting custom cache index file', async function () {
913+
const response = await fetch(this.getTestServer() + '/');
914+
assert.equal(response.status, 200, 'should respond with 200');
915+
assert.equal(response.headers.get('cache-control'), 'no-cache', 'should respond with cache-control');
916+
});
917+
it('requesting custom cache text file', async function () {
918+
const response = await fetch(this.getTestServer() + '/hello.txt');
919+
920+
assert.equal(response.status, 200, 'should respond with 200');
921+
assert.equal(response.headers.get('cache-control'), 'no-cache', 'should respond with cache-control');
922+
});
923+
it('requesting custom cache un-cached file', async function () {
924+
const response = await fetch(this.getTestServer() + '/empty.css');
925+
assert.equal(response.status, 200, 'should respond with 200');
926+
assert.equal(response.headers.get('cache-control'), 'no-cache', 'should respond with cache-control');
927+
});
928+
});
900929
});

0 commit comments

Comments
 (0)