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