From 5e93d78ea54ec7666f1b75558c1913789ffa25c2 Mon Sep 17 00:00:00 2001 From: 5ouma <101255979+5ouma@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:03:03 +0900 Subject: [PATCH] perf(redirect): Don't get content from GitHub when redirection Got content will never be used. --- src/libs/redirect.ts | 11 ++++++----- src/router.ts | 6 ++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libs/redirect.ts b/src/libs/redirect.ts index 586de08..b2c9aa7 100644 --- a/src/libs/redirect.ts +++ b/src/libs/redirect.ts @@ -8,7 +8,7 @@ export function redirect( ctx: RouterContext, userAgent: UserAgent, ref: string = "master", -): void { +): boolean { const repository = getRepository(); const url = join( new URL("https://github.com"), @@ -19,8 +19,9 @@ export function redirect( repository.path, ); - if (userAgent?.browser.name) { - ctx.response.status = STATUS_CODE.PermanentRedirect; - ctx.response.redirect(url); - } + if (!userAgent?.browser.name) return false; + + ctx.response.status = STATUS_CODE.PermanentRedirect; + ctx.response.redirect(url); + return true; } diff --git a/src/router.ts b/src/router.ts index 0664a00..a9f7f51 100644 --- a/src/router.ts +++ b/src/router.ts @@ -5,12 +5,10 @@ export const router = new Router(); router .get("/", async (ctx: RouterContext) => { ctx.response.type = "text/plain"; - await getContent(ctx); - redirect(ctx, ctx.request.userAgent); + if (!redirect(ctx, ctx.request.userAgent)) await getContent(ctx); }) .get("/:ref", async (ctx: RouterContext) => { const ref: string | undefined = ctx.params.ref; ctx.response.type = "text/plain"; - await getContent(ctx, ref); - redirect(ctx, ctx.request.userAgent, ref); + if (!redirect(ctx, ctx.request.userAgent, ref)) await getContent(ctx, ref); });