Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify paths to ignore #22

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,15 @@ history({
});
```

### ignorePaths
Ignore a list of directories so requests for your assets don't result in a redirect and fetch unwanted content

```javascript
history({
ignorePaths:['/lib/','/app/']
});
```

### rewrites
Override the index when the request url matches a regex pattern. You can either rewrite to a static string or use a function to transform the incoming request.

Expand Down
21 changes: 21 additions & 0 deletions lib/index.js
Expand Up @@ -66,13 +66,34 @@ exports = module.exports = function historyApiFallback(options) {
return next();
}

var ignorePaths = options.ignorePaths;
if (ignorePaths && pathShouldBeIgnored(parsedUrl, ignorePaths)) {
logger(
'Not rewriting',
req.method,
req.url,
'because the path is listed as a path that should be ignored. ',
ignorePaths
);
return next();
}

rewriteTarget = options.index || '/index.html';
logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
req.url = rewriteTarget;
next();
};
};

function pathShouldBeIgnored(parsedUrl, ignoredPaths) {
for (var i = 0; i < ignoredPaths.length; i++) {
//a little basic because as it's using contains, but probably sufficient.
if (parsedUrl.path.indexOf(ignoredPaths[i]) > -1){
return true;
}
}
}

function evaluateRewriteRule(parsedUrl, match, rule) {
if (typeof rule === 'string') {
return rule;
Expand Down