Skip to content

Commit

Permalink
💫 moved redirect headers implementation to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
8eecf0d2 committed Oct 18, 2018
1 parent ce4fa28 commit f37692c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,27 @@ export class Server {
}

private handleRedirect (redirect: Netlify.Redirect): void {
this.express.all(redirect.from, (request, response, next) => {
this.handleRedirectHeaders(response, redirect);
this.express.all(redirect.from, this.handleRedirectHeaders(redirect), (request, response, next) => {

return response.status(redirect.status).redirect(redirect.to);
})
}

private handleRewrite (redirect: Netlify.Redirect): void {
this.express.all(redirect.from, (request, response, next) => {
this.handleRedirectHeaders(response, redirect);
this.express.all(redirect.from, this.handleRedirectHeaders(redirect), (request, response, next) => {

return response.status(redirect.status).sendFile(path.join(this.paths.static, redirect.to));
});
}

private handleRedirectHeaders (response: express.Response, redirect: Netlify.Redirect): void {
if(redirect.headers) {
for(const header in redirect.headers) {
response.setHeader(header, redirect.headers[header]);
private handleRedirectHeaders (redirect: Netlify.Redirect): express.Handler {
return (request, response, next) => {
if(redirect.headers) {
for(const header in redirect.headers) {
response.setHeader(header, redirect.headers[header]);
}
}
next();
}
}

Expand Down

0 comments on commit f37692c

Please sign in to comment.