From 1d6bcc1d99100380e56c36926f5ab1ce228c3e9e Mon Sep 17 00:00:00 2001 From: ceecko Date: Sat, 17 Nov 2018 21:44:13 +0100 Subject: [PATCH 1/8] Fix issue with send options being overwritten --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a634c0c..f10fb9a 100644 --- a/index.js +++ b/index.js @@ -107,10 +107,11 @@ const staticify = (root, options) => { const serve = req => { const filePath = stripVersion(url.parse(req.url).pathname); - opts.sendOptions.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); - opts.sendOptions.root = root; + const sendOpts = Object.assign({}, opts.sendOptions); + sendOpts.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); + sendOpts.root = root; - return send(req, filePath, opts.sendOptions); + return send(req, filePath, sendOpts); }; const middleware = (req, res, next) => { From a20bdb5a03fe39bbec3606eb7d6759f802a66081 Mon Sep 17 00:00:00 2001 From: ceecko Date: Sun, 18 Nov 2018 10:54:37 +0100 Subject: [PATCH 2/8] Tests, performance improvements and maxAge for non-hashed assets --- README.md | 16 ++++++++++++++++ index.js | 11 +++++++---- test/index.js | 20 ++++++++++++-------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f84f3ed..f2a8b2d 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,14 @@ And in your template: ## Options +Options are specified as a second parameter to staticify: +```js +let 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. @@ -74,6 +82,14 @@ 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 hash such as `/image.png` passed to [send](https://github.com/pillarjs/send). +Can be defined as number of miliseconds or string accepted by [ms](https://www.npmjs.org/package/ms#readme) module (eg. `"5d"`, `"1y"`, etc.) + ### sendOptions * Type: Object diff --git a/index.js b/index.js index f10fb9a..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,10 +112,7 @@ const staticify = (root, options) => { const serve = req => { const filePath = stripVersion(url.parse(req.url).pathname); - - const sendOpts = Object.assign({}, opts.sendOptions); - sendOpts.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); - sendOpts.root = root; + const sendOpts = (filePath === req.url ? sendOptsNonVersioned : opts.sendOptions); return send(req, filePath, sendOpts); }; 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(); }); From 417e6f79cfa0f38cc2fee9e557152b8c4d842b6a Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 19 Nov 2018 13:08:11 +0200 Subject: [PATCH 3/8] Update README.md --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f2a8b2d..2af945d 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,10 @@ And in your template: ## Options -Options are specified as a second parameter to staticify: +Options are specified as the second parameter to `staticify`: + ```js -let staticify = require('staticify')( - path.join(__dirname, 'public'), - options -); +var staticify = require('staticify')(path.join(__dirname, 'public'), options); ``` ### includeAll @@ -87,8 +85,9 @@ app.use('/assets', staticify.middleware); // `app` is your express instance * Type: String | Number * Default: `0` -`maxAge` for assets without hash such as `/image.png` passed to [send](https://github.com/pillarjs/send). -Can be defined as number of miliseconds or string accepted by [ms](https://www.npmjs.org/package/ms#readme) module (eg. `"5d"`, `"1y"`, etc.) +`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 @@ -101,11 +100,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'); From fd7ae59a6bc13e8d38429de4bf1d7af5f379ecd1 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 19 Nov 2018 13:12:18 +0200 Subject: [PATCH 4/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2af945d..93b62f2 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,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}); From 30e9ea7de652fe34c049f49e8cb2831bd7104aa1 Mon Sep 17 00:00:00 2001 From: ceecko Date: Sat, 17 Nov 2018 21:44:13 +0100 Subject: [PATCH 5/8] Fix issue with send options being overwritten --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a634c0c..f10fb9a 100644 --- a/index.js +++ b/index.js @@ -107,10 +107,11 @@ const staticify = (root, options) => { const serve = req => { const filePath = stripVersion(url.parse(req.url).pathname); - opts.sendOptions.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); - opts.sendOptions.root = root; + const sendOpts = Object.assign({}, opts.sendOptions); + sendOpts.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); + sendOpts.root = root; - return send(req, filePath, opts.sendOptions); + return send(req, filePath, sendOpts); }; const middleware = (req, res, next) => { From a234068de008f17d08782d49c789ddff67e7e59b Mon Sep 17 00:00:00 2001 From: ceecko Date: Sun, 18 Nov 2018 10:54:37 +0100 Subject: [PATCH 6/8] Tests, performance improvements and maxAge for non-hashed assets --- README.md | 16 ++++++++++++++++ index.js | 11 +++++++---- test/index.js | 20 ++++++++++++-------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4e8892b..50a4aa8 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,14 @@ And in your template: ## Options +Options are specified as a second parameter to staticify: +```js +let 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 +83,14 @@ 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 hash such as `/image.png` passed to [send](https://github.com/pillarjs/send). +Can be defined as number of miliseconds or string accepted by [ms](https://www.npmjs.org/package/ms#readme) module (eg. `"5d"`, `"1y"`, etc.) + ### sendOptions * Type: Object diff --git a/index.js b/index.js index f10fb9a..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,10 +112,7 @@ const staticify = (root, options) => { const serve = req => { const filePath = stripVersion(url.parse(req.url).pathname); - - const sendOpts = Object.assign({}, opts.sendOptions); - sendOpts.maxAge = filePath === req.url ? 0 : (opts.sendOptions.maxAge ? opts.sendOptions.maxAge : MAX_AGE); - sendOpts.root = root; + const sendOpts = (filePath === req.url ? sendOptsNonVersioned : opts.sendOptions); return send(req, filePath, sendOpts); }; 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(); }); From d7fce081a6ac4cb7bc34b9c8d9d89b10515d6413 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 19 Nov 2018 13:08:11 +0200 Subject: [PATCH 7/8] Update README.md --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 50a4aa8..f720cb2 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,10 @@ And in your template: ## Options -Options are specified as a second parameter to staticify: +Options are specified as the second parameter to `staticify`: + ```js -let staticify = require('staticify')( - path.join(__dirname, 'public'), - options -); +var staticify = require('staticify')(path.join(__dirname, 'public'), options); ``` ### includeAll @@ -88,8 +86,9 @@ app.use('/assets', staticify.middleware); // `app` is your express instance * Type: String | Number * Default: `0` -`maxAge` for assets without hash such as `/image.png` passed to [send](https://github.com/pillarjs/send). -Can be defined as number of miliseconds or string accepted by [ms](https://www.npmjs.org/package/ms#readme) module (eg. `"5d"`, `"1y"`, etc.) +`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 @@ -102,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'); From 578aa6ecc6976595af1a204862eb3b2f4565d73b Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 19 Nov 2018 13:12:18 +0200 Subject: [PATCH 8/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f720cb2..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});