Skip to content

Commit

Permalink
fix(web-server): correct caching headers for SHAs
Browse files Browse the repository at this point in the history
This was introduced by 6e31cb2 when we switched to using SHA hashes rather than timestamps.
  • Loading branch information
vojtajina committed Dec 3, 2013
1 parent 5ee886b commit bf27e80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/middleware/source_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var createSourceFilesMiddleware = function(filesPromise, serveFile,

return function(request, response, next) {
var requestedFilePath = querystring.unescape(request.url)
.replace(/\?.*/, '')
.replace(/\?.*$/, '')
.replace(/^\/absolute/, '')
.replace(/^\/base/, basePath);

Expand All @@ -38,7 +38,7 @@ var createSourceFilesMiddleware = function(filesPromise, serveFile,

if (file) {
serveFile(file.contentPath || file.path, response, function() {
if (/\?\d+/.test(request.url)) {
if (/\?\w+/.test(request.url)) {
// files with timestamps - cache one year, rely on timestamps
common.setHeavyCacheHeaders(response);
} else {
Expand Down
17 changes: 15 additions & 2 deletions test/unit/middleware/source_files.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe 'middleware.source_files', ->
callHandlerWith '/base/a.js?123345'


it 'should send strict caching headers for js source files with timestamps', (done) ->
it 'should send strict caching headers for js source files with sha', (done) ->
servedFiles [
new File('/src/some.js')
]
Expand All @@ -78,7 +78,20 @@ describe 'middleware.source_files', ->
expect(response._headers['Cache-Control']).to.deep.equal ['public', 'max-age=31536000']
done()

callHandlerWith '/absolute/src/some.js?12323'
callHandlerWith '/absolute/src/some.js?df43b8acf136389a8dd989bda397d1c9b4e048be'


it 'should send strict caching headers for js source files with sha (in basePath)', (done) ->
servedFiles [
new File('/base/path/a.js')
]

response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response._headers['Cache-Control']).to.deep.equal ['public', 'max-age=31536000']
done()

callHandlerWith '/base/a.js?df43b8acf136389a8dd989bda397d1c9b4e048be'


it 'should send no-caching headers for js source files without timestamps', (done) ->
Expand Down

0 comments on commit bf27e80

Please sign in to comment.