diff --git a/README.md b/README.md index 4e8892b..ddc76cd 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The version hashes are the md5 of the contents of the static asset. Thus, every var path = require('path'); var staticify = require('staticify')(path.join(__dirname, 'public')); -... +// ... app.use(staticify.middleware); app.helpers({getVersionedPath: staticify.getVersionedPath}); @@ -46,6 +46,12 @@ And in your template: ## Options +Options are specified as the second parameter to `staticify`: + +```js +var staticify = require('staticify')(path.join(__dirname, 'public'), options); +``` + ### includeAll Include all files when scanning the public directory. By default, the directories from [ignore-by-default](https://github.com/novemberborn/ignore-by-default/blob/master/index.js) are ignored. @@ -75,6 +81,15 @@ var staticify = require('staticify')(path.join(__dirname, 'public'), options); app.use('/assets', staticify.middleware); // `app` is your express instance ``` +### maxAgeNonHashed + +* Type: String | Number +* Default: `0` + +`maxAge` for assets without a hash such as `/image.png` passed to [send](https://github.com/pillarjs/send). + +Can be defined as a number of milliseconds or string accepted by [ms](https://www.npmjs.org/package/ms#readme) module (eg. `'5d'`, `'1y'`, etc.) + ### sendOptions * Type: Object @@ -86,11 +101,11 @@ You can pass any [send](https://github.com/pillarjs/send) options; used in `midd Install from npm: -``` +```sh npm install staticify ``` -Initialise the staticify helper with the path of your public directory: +Initialize the staticify helper with the path of your public directory: ```js var path = require('path'); diff --git a/index.js b/index.js index a634c0c..ee6ac01 100644 --- a/index.js +++ b/index.js @@ -19,15 +19,21 @@ const staticify = (root, options) => { includeAll: opts.includeAll || false, shortHash: opts.shortHash || true, pathPrefix: opts.pathPrefix || '/', + maxAgeNonHashed: opts.maxAgeNonHashed || 0, sendOptions: opts.sendOptions || {} }; defaultOptions = Object.assign(defaultOptions, opts); + defaultOptions.sendOptions.root = root; + defaultOptions.sendOptions.maxAge = defaultOptions.sendOptions.maxAge || MAX_AGE; + return defaultOptions; }; const opts = setOptions(options); + const sendOptsNonVersioned = Object.assign({}, opts.sendOptions); + sendOptsNonVersioned.maxAge = opts.maxAgeNonHashed; const cachedMakeHash = memoizee(filePath => { const fileStr = fs.readFileSync(filePath, 'utf8'); @@ -106,11 +112,9 @@ const staticify = (root, options) => { const serve = req => { const filePath = stripVersion(url.parse(req.url).pathname); + const sendOpts = (filePath === req.url ? sendOptsNonVersioned : opts.sendOptions); - opts.sendOptions.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); - opts.sendOptions.root = root; - - return send(req, filePath, opts.sendOptions); + return send(req, filePath, sendOpts); }; const middleware = (req, res, next) => { diff --git a/test/index.js b/test/index.js index 6e5cbaf..3bff7f7 100644 --- a/test/index.js +++ b/test/index.js @@ -81,8 +81,9 @@ describe('.serve', () => { let server; before(done => { + const staticifyObj = staticify(ROOT); server = http.createServer((req, res) => { - staticify(ROOT).serve(req).pipe(res); + staticifyObj.serve(req).pipe(res); }); server.listen(12321, done); }); @@ -119,8 +120,9 @@ describe('.serve', () => { let server; before(done => { + const staticifyObj = staticify(ROOT, {shortHash: false}); server = http.createServer((req, res) => { - staticify(ROOT, {shortHash: false}).serve(req).pipe(res); + staticifyObj.serve(req).pipe(res); }); server.listen(12321, done); }); @@ -157,12 +159,14 @@ describe('.serve', () => { let server; before(done => { + const staticifyObj = staticify(ROOT, { + maxAgeNonHashed: 7200 * 1000, + sendOptions: { + maxAge: 3600 * 1000 // milliseconds + } + }); server = http.createServer((req, res) => { - staticify(ROOT, { - sendOptions: { - maxAge: 3600 * 1000 // milliseconds - } - }).serve(req).pipe(res); + staticifyObj.serve(req).pipe(res); }); server.listen(12321, done); }); @@ -173,7 +177,7 @@ describe('.serve', () => { it('should serve files without a hash tag', done => { http.get('http://localhost:12321/index.js', res => { - res.headers['cache-control'].includes('max-age=0').should.be.true(); + res.headers['cache-control'].includes('max-age=7200').should.be.true(); res.statusCode.should.equal(200); done(); });