From 5ceaaa1eff34d031cf2878203122d7808e9bbbc5 Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Thu, 18 Jul 2024 10:28:23 +0100 Subject: [PATCH] =?UTF-8?q?fix(server):=20Inter=20wouldn't=20load=20on=20L?= =?UTF-8?q?inux=20=F0=9F=90=A7=20(#11491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes loading any file with mixed case on a case sensitive filesystem --- server/middlewares.ts | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/server/middlewares.ts b/server/middlewares.ts index 3485becb95f2..0cae1c942f14 100644 --- a/server/middlewares.ts +++ b/server/middlewares.ts @@ -8,20 +8,29 @@ import { STATIC_ROOT } from "../libs/env/index.js"; import { resolveFundamental } from "../libs/fundamental-redirects/index.js"; import { getLocale } from "../libs/locale-utils/index.js"; -// Lowercase every request because every possible file we might have -// on disk is always in lowercase. -// This only helps when you're on a filesystem (e.g. Linux) that is case -// sensitive. -const slugRewrite = (req, res, next) => { +const slugLowercase = (req, res, next) => { req.url = req.url.toLowerCase(); next(); }; +const staticServer = express.static(STATIC_ROOT, { + setHeaders: (res) => { + if (res.req.path.endsWith("/runner.html")) { + res.setHeader("Content-Security-Policy", PLAYGROUND_UNSAFE_CSP_VALUE); + } else { + res.setHeader("Content-Security-Policy", CSP_VALUE); + } + }, +}); + +// This is necessary for case-sensitive filesystems, such as on Linux 🐧 +export const staticMiddlewares = [staticServer, slugLowercase, staticServer]; + /** * This function is returns an object with {url:string, status:number} * if there's some place to redirect to, otherwise an empty object. */ -const originRequest = (req, res, next) => { +export const originRequestMiddleware = (req, res, next) => { const { url: fundamentalRedirectUrl, status } = resolveFundamental(req.url); if (fundamentalRedirectUrl && status) { res.redirect(status, fundamentalRedirectUrl); @@ -50,17 +59,3 @@ const originRequest = (req, res, next) => { next(); } }; - -export const staticMiddlewares = [ - slugRewrite, - express.static(STATIC_ROOT, { - setHeaders: (res) => { - if (res.req.path.endsWith("/runner.html")) { - res.setHeader("Content-Security-Policy", PLAYGROUND_UNSAFE_CSP_VALUE); - } else { - res.setHeader("Content-Security-Policy", CSP_VALUE); - } - }, - }), -]; -export const originRequestMiddleware = originRequest;