browserify-cache
Advanced tools
+28
| browserify-cache - Strong and weak caching for Browserify bundles | ||
| ##Background | ||
| "Last-Modified" and "If-Modified-Since" are "weak" caching. If the "If-Modified-Since" header is set, then Browserify can send 304 Not Modified, if appropriate, which saves bandwidth. | ||
| "Expires" or "Cache-Control: max-age" are "strong" caching. The browser can simply pull from its own cache in certain cases to save an entire HTTP request. | ||
| In the client HTML, your Browserify bundle URL should contain the UNIX timestamp that matches the last modified date of the bundle. | ||
| https://github.com/substack/node-browserify | ||
| http://code.google.com/speed/page-speed/docs/caching.html | ||
| ##Usage | ||
| ```javascript | ||
| var browserify = require('browserify'); | ||
| var browserifyCache = require('browserify-cache'); | ||
| var browserifyMiddleware = browserify(config.browserify); | ||
| app.use(express.browserifyCache(config.browserify, browserifyMiddleware) ); | ||
| //Now you can expose this URL to your views using Express | ||
| app.dynamicHelpers({ | ||
| 'browserifyMount': function() { | ||
| return config.browserify.mount + '.' + browserifyMiddleware.modified.getTime() + '.js'; | ||
| } | ||
| }); | ||
| ``` |
+9
-4
@@ -1,6 +0,8 @@ | ||
| var browserify = require('browserify'); | ||
| module.exports = function(browserifyOpts, browserifyMiddleware) { | ||
| var lastModified = browserifyMiddleware.modified.getTime(); | ||
| return function(req, res, next) { | ||
| //Pass a fake request through browserify so that we can update the bundle if it has changed | ||
| browserifyMiddleware({'url': browserifyOpts.mount}, {'setHeader': function() { | ||
| console.log("browserify:setHeader", arguments); | ||
| }, 'end': function() {}} ); | ||
| if(lastModified != browserifyMiddleware.modified.getTime() ) | ||
@@ -14,3 +16,3 @@ { | ||
| { | ||
| //Add expires header | ||
| //Add expires header - maximum recommended expiration is one year | ||
| var d = new Date(); | ||
@@ -24,3 +26,3 @@ d.setFullYear(d.getFullYear() + 1); | ||
| //Check If-Modified-Since request header | ||
| if(new Date(req.headers["if-modified-since"]).getTime() == browserifyMiddleware.modified.getTime() ) | ||
| if(Math.floor(new Date(req.headers["if-modified-since"]).getTime() / 1000) == Math.floor(browserifyMiddleware.modified.getTime() / 1000) ) | ||
| res.send(304); | ||
@@ -31,2 +33,5 @@ else | ||
| req.url = browserifyOpts.mount; //Trick browserify | ||
| console.log("Browserify is handling this request"); | ||
| console.log("\tif-modified-since:", req.headers["if-modified-since"], new Date(req.headers["if-modified-since"]).getTime() ); | ||
| console.log("\tbrowserify modified date:", browserifyMiddleware.modified, browserifyMiddleware.modified.getTime()); | ||
| browserifyMiddleware(req, res, next); | ||
@@ -33,0 +38,0 @@ } |
+2
-2
@@ -5,3 +5,3 @@ { | ||
| "description": "Easily cache Browserify bundles", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "repository": { | ||
@@ -13,3 +13,3 @@ "type": "git", | ||
| "engines": { | ||
| "node": "~0.4.12" | ||
| "node": ">0.4.9" | ||
| }, | ||
@@ -16,0 +16,0 @@ "dependencies": {}, |
-8
| browserify-cache - Strong and weak caching for Browserify bundles | ||
| "Last-Modified" and "If-Modified-Since" are "weak" caching. If the "If-Modified-Since" header is set, then Browserify can send 304 Not Modified, if appropriate, which saves bandwidth. | ||
| "Expires" or "Cache-Control: max-age" are "strong" caching. The browser can simply pull from its own cache in certain cases to save an entire HTTP request. In the client HTML, if the Browserify bundle URL contains a UNIX timestamp that matches the last modified date of the bundle. This allows a user to easily implement URL fingerprinting by simply appending the last modified date of the Browserify bundle (b.modified) to the mount URL. The full Browserify mount URL is made available via req.browserifyMount. Finally, if Express is being utilitzed, the 'browserifyMount' local is available to views via res.locals(...). | ||
| https://github.com/substack/node-browserify | ||
| http://code.google.com/speed/page-speed/docs/caching.html |
3418
27.92%39
18.18%29
222.22%