Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Inter wouldn't load on Linux 🐧 #11491

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;