From 2cdaad588c82508734d04c2169503782dfe8556a Mon Sep 17 00:00:00 2001 From: daikei Date: Mon, 17 Apr 2023 22:36:22 +0000 Subject: [PATCH] [Fix] Properly replace koa-fix-trailing-slashes with inhouse script (#9870) This follows up #9869, with an awaited ``next()``, fixing the script. > I'm looking to remove another 5kb package, replacing it with an in-house script. The commit i'm proposing translates the entire job of koa-remove-trailing-slashes into a 206 byte script that gets the job done well. (just to show the code does work) ![](https://s3.kitsunes.club/storage/41c7854d-d01e-4f4c-94dd-8b651e2d7367.gif) Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9870 Co-authored-by: daikei Co-committed-by: daikei --- packages/backend/package.json | 1 - packages/backend/src/server/index.ts | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index b02af2c033..97e86cd263 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -78,7 +78,6 @@ "koa-json-body": "5.3.0", "koa-logger": "3.2.1", "koa-mount": "4.0.0", - "koa-remove-trailing-slashes": "2.0.3", "koa-send": "5.0.1", "koa-slow": "2.1.0", "koa-views": "7.0.2", diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index be1f71a7ae..191556c408 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -10,7 +10,6 @@ import Router from "@koa/router"; import mount from "koa-mount"; import koaLogger from "koa-logger"; import * as slow from "koa-slow"; - import { IsNull } from "typeorm"; import config from "@/config/index.js"; import Logger from "@/services/logger.js"; @@ -30,7 +29,6 @@ import proxyServer from "./proxy/index.js"; import webServer from "./web/index.js"; import { initializeStreamingServer } from "./api/streaming.js"; import { koaBody } from "koa-body"; -import removeTrailingSlash from "koa-remove-trailing-slashes"; import { v4 as uuid } from "uuid"; export const serverLogger = new Logger("server", "gray", false); @@ -39,7 +37,11 @@ export const serverLogger = new Logger("server", "gray", false); const app = new Koa(); app.proxy = true; -app.use(removeTrailingSlash()); +// Replace trailing slashes +app.use(async (ctx, next) => { + if (ctx.request.path !== "/" && ctx.request.path.endsWith('/')) return ctx.redirect(ctx.request.path.replace(/\/$/, "")) + else await next() +}); if (!["production", "test"].includes(process.env.NODE_ENV || "")) { // Logger