Skip to content

Commit

Permalink
fix(server): Inter wouldn't load on Linux 🐧 (#11491)
Browse files Browse the repository at this point in the history
fixes loading any file with mixed case on a case sensitive filesystem
  • Loading branch information
LeoMcA authored Jul 18, 2024
1 parent 8d06483 commit 5ceaaa1
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions server/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

0 comments on commit 5ceaaa1

Please sign in to comment.