Skip to content

Commit

Permalink
Make /combo requests cacheable.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Jun 15, 2010
1 parent c6b4a60 commit 847de55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions sandbox/performance/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ exports.Response = function Response(res) {
headers[name.toLowerCase()] = [name, value];
};

// Default headers.
res.addHeader('Date', new Date().toUTCString());

return res;
};

Expand Down
23 changes: 21 additions & 2 deletions sandbox/performance/perf-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ var fs = require('fs'),

// Combo URLs.
server.get('/combo/([^/]+)/?', function (root) {
var fullPath,
var expires,
fullPath,
lastModified,
mimeType,
mtime,
relativePath,
response = '',
rootPath = config.roots[root];
rootPath = config.roots[root],
stat;

if (!rootPath) {
this.response.send404();
Expand All @@ -41,17 +45,32 @@ server.get('/combo/([^/]+)/?', function (root) {

try {
response += fs.readFileSync(fullPath, 'utf8') + "\n";
mtime = new Date(fs.statSync(fullPath).mtime);
} catch (ex) {
this.response.send404();
this.end();
return;
}

if (!lastModified || mtime > lastModified) {
lastModified = mtime;
}
}

expires = new Date();
expires.setUTCFullYear(expires.getUTCFullYear() + 10);

// TODO: Currently, the last file extension is what determines the
// Content-Type. Need to look into what the real ComboHandler does when
// multiple file types are requested in a single request.
this.response.setHeader('Cache-Control', 'public;max-age=315569260');
this.response.setHeader('Content-Type', mimeType + ';charset=utf-8');
this.response.setHeader('Expires', expires.toUTCString());

if (lastModified) {
this.response.setHeader('Last-Modified', lastModified.toUTCString());
}

this.end(response + "\n");
});

Expand Down

0 comments on commit 847de55

Please sign in to comment.