Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const loadFile = (fileName) => {
resolve();
};
stylesheet.onerror = () => {
Log.error("Error on loading stylesheet:", fileName);
Log.error(`Could not load stylesheet '${fileName}'.`);
stylesheet.remove();
resolve();
};
Expand Down
11 changes: 11 additions & 0 deletions js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ class Server {
res.status(200).send("OK");
});

// Run after express.static: missing CSS gets a CSS-typed 404 instead of
// Express's default HTML 404, which browsers report as a MIME mismatch.
const handleMissingStylesheet = (req, res, next) => {
if (path.extname(req.path).toLowerCase() !== ".css") {
return next();
}

res.status(404).type("text/css").send("");
};
app.use(handleMissingStylesheet);

this.#server.on("listening", () => {
resolve({
app,
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/serveronly_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe("App environment", () => {
const res = await fetch("http://localhost:8080/nothing");
expect(res.status).toBe(404);
});

it("missing stylesheets should return 404 with a CSS content type", async () => {
const res = await fetch("http://localhost:8080/config/missing.css");
expect(res.status).toBe(404);
expect(res.headers.get("content-type")).toMatch(/^text\/css/);
});
});

describe("Check config", () => {
Expand Down
Loading