in src/middleware/redirects.js [104:147]
module.exports = function() {
return function(req, res, next) {
const config = _.get(req, "superstatic.redirects");
if (!config) {
return next();
}
const redirects = [];
if (_.isArray(config)) {
config.forEach((redir) => {
const glob = redir.glob || redir.source;
redirects.push(
new Redirect(glob, redir.regex, redir.destination, redir.type)
);
});
} else {
throw new Error("redirects provided in an unrecognized format");
}
const matcher = function(url) {
for (let i = 0; i < redirects.length; i++) {
const result = redirects[i].test(url);
if (result) {
return result;
}
}
return undefined;
};
const match = matcher(req.url);
if (!match) {
return next();
}
// Remove leading slash of a url
const redirectUrl = formatExternalUrl(match.destination);
return res.superstatic.handle({
redirect: redirectUrl,
status: match.type
});
};
};