diff --git a/README.md b/README.md index 5ffa8ec..2bde91d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 90b0106..afbdb9d 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ const staticify = (root, options) => { let defaultOptions = { includeAll: opts.includeAll || false, shortHash: opts.shortHash || true, + pathPrefix: opts.pathPrefix || '/', sendOptions: opts.sendOptions || {} }; @@ -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..js -> index.js diff --git a/test/index.js b/test/index.js index 73ac87f..6e5cbaf 100644 --- a/test/index.js +++ b/test/index.js @@ -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'); });