Eruda only in development mode.

This commit is contained in:
Dessalines 2023-10-03 17:53:15 -04:00
parent 5536f5aef8
commit 7a6ebc0048

View file

@ -1,4 +1,4 @@
import express, { RequestHandler } from "express";
import express, { RequestHandler, Request, Response } from "express";
import { StaticRouter } from "inferno-router";
import { renderToString } from "inferno-server";
// import { matchPath } from "inferno-router";
@ -18,13 +18,21 @@ server.use("/static", express.static(path.resolve("./dist")));
server.use("/docs", express.static(path.resolve("./dist/assets/docs")));
server.use("/api", express.static(path.resolve("./dist/assets/api")));
server.get("/*", async (req, res) => {
// const activeRoute = routes.find(route => matchPath(req.path, route)) || {};
const context = {} as any;
function erudaInit(): string {
if (process.env["NODE_ENV"] == "development") {
return `
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
`;
} else {
return "";
}
}
function setLanguage(req: Request, res: Response): string {
// Setting the language for non-js browsers
const cookieLang = getLanguageFromCookie(req.headers.cookie);
var language: string;
let language: string;
if (req.query["lang"] != null) {
language = req.query["lang"].toString();
res.cookie("lang", language, {
@ -37,6 +45,14 @@ server.get("/*", async (req, res) => {
? req.headers["accept-language"].split(",")[0]
: "en";
}
return language;
}
server.get("/*", async (req, res) => {
// const activeRoute = routes.find(route => matchPath(req.path, route)) || {};
const context = {} as any;
const language = setLanguage(req, res);
i18n.changeLanguage(language);
const wrapper = (
@ -50,14 +66,12 @@ server.get("/*", async (req, res) => {
const root = renderToString(wrapper);
const helmet = Helmet.renderStatic();
// TODO make eruda only work in debug mode
res.send(`
<!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()} lang="en">
<head>
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
${erudaInit()}
${helmet.title.toString()}
${helmet.meta.toString()}