Skip to content

Commit

Permalink
🙈 initial work for better redirect feature support #8
Browse files Browse the repository at this point in the history
  • Loading branch information
8eecf0d2 committed Oct 17, 2018
1 parent f4517b3 commit 45672d4
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ export class Server {
static: path.join(process.cwd(), String(this.options.netlifyConfig.build.publish)),
lambda: path.join(process.cwd(), String(this.options.netlifyConfig.build.functions)),
}

this.express = express();
this.express.use(bodyParser.raw({ limit: "6mb" }));
this.express.use(bodyParser.text({ limit: "6mb", type: "*/*" }));
this.routeHeaders();
this.routeForcedRedirects();

const hardRedirects = this.options.netlifyConfig.redirects.filter(redirect => redirect.force === true);
const softRedirects = this.options.netlifyConfig.redirects.filter(redirect => redirect.force === false);

this.routeHeaders(this.options.netlifyConfig.headers);
this.routeRedirects(hardRedirects);
this.routeStatic();
this.routeLambdas();
this.routeRedirects();
this.routeLambda();
this.routeRedirects(softRedirects);
}

private routeStatic (): void {
Expand All @@ -48,12 +53,8 @@ export class Server {
Logger.info("netlify-local: static routes initialized");
}

private routeHeaders (): void {
if(!this.options.netlifyConfig.headers) {
return
}

for(const header of this.options.netlifyConfig.headers) {
private routeHeaders (headers: Netlify.Headers[]): void {
for(const header of headers) {
this.handleHeader(header.for, header.values)
}
}
Expand All @@ -67,41 +68,41 @@ export class Server {
})
}

private routeForcedRedirects (): void {
if(!this.options.netlifyConfig.redirects) {
return
}

const forcedRedirects = this.options.netlifyConfig.redirects.filter(redirect => redirect.force === true);

for(const redirect of forcedRedirects) {
this.handleRedirect(redirect);
private routeRedirects (redirects: Netlify.Redirect[]): void {
for(const redirect of redirects) {
if([301, 302, 303].includes(redirect.status)) {
this.handleRedirect(redirect);
} else {
this.handleRewrite(redirect)
}
}
}

private routeRedirects (): void {
if(!this.options.netlifyConfig.redirects) {
return
}
private handleRedirect (redirect: Netlify.Redirect): void {
this.express.all(redirect.from, (request, response, next) => {
this.handleRedirectHeaders(response, redirect);

for(const redirect of this.options.netlifyConfig.redirects) {
this.handleRedirect(redirect);
}
return response.status(redirect.status).redirect(redirect.to);
})
}

private handleRedirect(redirect: Netlify.Redirect): void {
private handleRewrite (redirect: Netlify.Redirect): void {
this.express.all(redirect.from, (request, response, next) => {
if(redirect.headers) {
for(const header in redirect.headers) {
response.setHeader(header, redirect.headers[header]);
}
}
this.handleRedirectHeaders(response, redirect);

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

private routeLambdas (): void {
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 routeLambda (): void {
if(!this.options.routes.lambda) {
return
}
Expand Down

0 comments on commit 45672d4

Please sign in to comment.