Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ Generate a short (7-digit) md5 hash instead of the full (32-digit) one.
* Type: Boolean
* Default: `true`

### pathPrefix

If you are using the staticify convenience middleware through a specific route, it is necessary to indicate the route in this option.

* Type: String
* Default: "/"

```js
var path = require('path');
var options = { pathPrefix: '/assets' };
var staticify = require('staticify')(path.join(__dirname, 'public'), options);

app.use('/assets', staticify.middleware); // `app` is your express instance
```

### sendOptions

* Type: Object
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const staticify = (root, options) => {
let defaultOptions = {
includeAll: opts.includeAll || false,
shortHash: opts.shortHash || true,
pathPrefix: opts.pathPrefix || '/',
sendOptions: opts.sendOptions || {}
};

Expand Down Expand Up @@ -74,7 +75,7 @@ const staticify = (root, options) => {

fileNameParts.push(versions[p], fileNameParts.pop());

return path.posix.join(path.dirname(p), fileNameParts.join('.'));
return path.posix.join(opts.pathPrefix, path.dirname(p), fileNameParts.join('.'));
};

// index.<hash>.js -> index.js
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ describe('.getVersionedPath', () => {
/^[0-9a-f]{32}$/i.exec(versioned[1])[0].should.equal(versioned[1]);
});

it('should add a prefix route to the path', () => {
let versioned = staticify(ROOT, {pathPrefix: '/prefix'}).getVersionedPath('/index.js');

versioned = versioned.split('.');
versioned.should.have.a.lengthOf(3);
versioned[0].should.equal('/prefix/index');
versioned[2].should.equal('js');
versioned[1].should.have.a.lengthOf(7);
/^[0-9a-f]{7}$/i.exec(versioned[1])[0].should.equal(versioned[1]);
});

it('shouldn\'t add a hash if the path isn\'t known', () => {
staticify(ROOT).getVersionedPath('/unknown.js').should.equal('/unknown.js');
});
Expand Down