Skip to content

Commit

Permalink
feat(sirv): allow --single to accept path;
Browse files Browse the repository at this point in the history
- Closes #35
  • Loading branch information
lukeed committed Jul 27, 2019
1 parent 5671577 commit fd55eca
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/sirv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ module.exports = function (dir, opts={}) {
let extensions = opts.extensions || ['html', 'htm'];
let setHeaders = opts.setHeaders || noop;

let fallback = '/';
let isSPA = !!opts.single;
if (typeof opts.single === 'string') {
let idx = opts.single.lastIndexOf('.');
fallback += !!~idx ? opts.single.substring(0, idx) : opts.single;
}

if (opts.dev) {
return function (req, res, next) {
let stats, file, uri = req.path || parser(req, true).pathname;
let arr = [uri].concat(
toAssume(uri, extensions),
opts.single && uri !== '/' ? toAssume('/', extensions) : []
isSPA && uri !== fallback ? toAssume(fallback, extensions) : []
).map(x => join(dir, x)).filter(fs.existsSync);
while (file = arr.shift()) {
stats = fs.statSync(file);
Expand Down Expand Up @@ -119,7 +126,7 @@ module.exports = function (dir, opts={}) {

return function (req, res, next) {
let pathname = req.path || parser(req, true).pathname;
let data = FILES[pathname] || find(pathname, extensions) || opts.single && find('/', extensions);
let data = FILES[pathname] || find(pathname, extensions) || isSPA && find(fallback, extensions);
if (!data) return next ? next() : isNotFound(req, res);

setHeaders(res, pathname, data.stats);
Expand Down

0 comments on commit fd55eca

Please sign in to comment.