Skip to content

Commit

Permalink
Merge b4f7aa0 into da19604
Browse files Browse the repository at this point in the history
  • Loading branch information
ceecko committed Nov 19, 2018
2 parents da19604 + b4f7aa0 commit 3bed395
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) => {
Expand Down
20 changes: 12 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand All @@ -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();
});
Expand Down

0 comments on commit 3bed395

Please sign in to comment.