Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added Expires header (max one year) for #410 #416

Closed
wants to merge 3 commits into from

3 participants

@ryanrolds

Expires header for static. Enforcing maximum of one year per RFC2616 14.21. This wont affect Control-Cache, as control ALWAYS supersedes Expires. This is really legacy support for HTTP/1.0 agents that haven't been patched to support max-age.

@saich

I'd prefer that setting Expires heading can be controlled from options parameter, a boolean...

In my usual workflow, I set "Expires" on CSS, JS & images, (no the browser can use them directly from cache) but not on static HTML pages (these pages changes sometimes, and hence I want the browser contact me with the ETag it have). This is because I don't want to change the URLs of static pages (though I can change the URLs of JS & CSS).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
20 examples/static.js
@@ -3,12 +3,14 @@
* Module dependencies.
*/
-var connect = require('../');
-
-connect(
- connect.static(__dirname + '/public', { maxAge: 0 })
- , function(req, res) {
- res.setHeader('Content-Type', 'text/html');
- res.end('<img src="/tobi.jpeg" />')
- }
-).listen(3000);
+var connect = require('../')
+ , http = require('http');
+
+var app = connect();
+app.use(connect.static(__dirname + '/public', { maxAge: 0}));
+app.use(function(req, res) {
+ res.setHeader('Content-Type', 'text/html');
+ res.end('<img src="/tobi.jpeg" />')
+});
+
+http.createServer(app).listen(3000);
View
17 examples/staticCache.js
@@ -0,0 +1,17 @@
+
+/**
+ * Module dependencies.
+ */
+
+var connect = require('../')
+ , http = require('http');
+
+var app = connect();
+app.use(connect.staticCache());
+app.use(connect.static(__dirname + '/public', { maxAge: 0 }));
+app.use(function(req, res) {
+ res.setHeader('Content-Type', 'text/html');
+ res.end('<img src="/tobi.jpeg" />')
+});
+
+http.createServer(app).listen(3000);
View
11 lib/middleware/static.js
@@ -67,6 +67,12 @@ exports = module.exports = function static(root, options){
exports.mime = mime;
/**
+ * Max allowed value for Expires (RFC2616 14.21)
+ */
+
+var msInAYear = 31536000000;
+
+/**
* Attempt to tranfer the requseted file to `res`.
*
* @param {ServerRequest}
@@ -140,8 +146,13 @@ var send = exports.send = function(req, res, next, options){
return;
}
+ // date for Expires header - cant be more then a year
+ var expiresDate = (maxAge <= msInAYear) ? maxAge : msInAYear;
+ expiresDate = new Date().getTime() + expiresDate;
+
// header fields
if (!res.getHeader('Date')) res.setHeader('Date', new Date().toUTCString());
+ if (!res.getHeader('Expires')) res.setHeader('Expires', new Date(expiresDate).toUTCString());
if (!res.getHeader('Cache-Control')) res.setHeader('Cache-Control', 'public, max-age=' + (maxAge / 1000));
if (!res.getHeader('Last-Modified')) res.setHeader('Last-Modified', stat.mtime.toUTCString());
if (!res.getHeader('Content-Type')) {
Something went wrong with that request. Please try again.