Skip to content

Commit

Permalink
perf(redirect): Don't get content from GitHub when redirection
Browse files Browse the repository at this point in the history
Got content will never be used.
  • Loading branch information
5ouma committed Jun 18, 2024
1 parent 9c45275 commit 5e93d78
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/libs/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function redirect<R extends string>(
ctx: RouterContext<R>,
userAgent: UserAgent,
ref: string = "master",
): void {
): boolean {
const repository = getRepository();
const url = join(
new URL("https://github.com"),
Expand All @@ -19,8 +19,9 @@ export function redirect<R extends string>(
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;
}
6 changes: 2 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ export const router = new Router();
router
.get("/", async <R extends string>(ctx: RouterContext<R>) => {
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 <R extends string>(ctx: RouterContext<R>) => {
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);
});

0 comments on commit 5e93d78

Please sign in to comment.