2024-07-18 21:35:17 +03:00
|
|
|
import assetPath from "../services/asset_path.js";
|
2024-07-18 21:37:45 +03:00
|
|
|
import path from "path";
|
2024-07-19 00:18:35 +03:00
|
|
|
import { fileURLToPath } from "url";
|
2024-07-18 21:37:45 +03:00
|
|
|
import express from "express";
|
2025-04-24 14:55:11 +03:00
|
|
|
import { getResourceDir, isDev } from "../services/utils.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type serveStatic from "serve-static";
|
2024-12-14 10:05:38 +02:00
|
|
|
|
2025-03-08 16:01:53 +01:00
|
|
|
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<unknown, Record<string, unknown>>>) => {
|
2025-01-22 19:08:38 +01:00
|
|
|
if (!isDev) {
|
2023-05-07 15:23:46 +02:00
|
|
|
options = {
|
2025-01-09 18:07:02 +02:00
|
|
|
maxAge: "1y",
|
2023-05-07 15:23:46 +02:00
|
|
|
...options
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return express.static(root, options);
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-19 18:16:46 +02:00
|
|
|
async function register(app: express.Application) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
2025-04-24 14:55:11 +03:00
|
|
|
const resourceDir = getResourceDir();
|
|
|
|
|
|
2025-01-22 19:08:38 +01:00
|
|
|
if (isDev) {
|
2025-04-23 10:24:05 +03:00
|
|
|
const publicUrl = process.env.TRILIUM_PUBLIC_SERVER;
|
|
|
|
|
if (!publicUrl) {
|
|
|
|
|
throw new Error("Missing TRILIUM_PUBLIC_SERVER");
|
|
|
|
|
}
|
2024-12-14 10:10:10 +02:00
|
|
|
} else {
|
2025-04-24 14:55:11 +03:00
|
|
|
const clientStaticCache = persistentCacheStatic(path.join(resourceDir, "public"));
|
2025-04-24 13:18:06 +03:00
|
|
|
app.use(`/${assetPath}/app`, clientStaticCache);
|
|
|
|
|
app.use(`/${assetPath}/app-dist`, clientStaticCache);
|
2025-04-24 14:55:11 +03:00
|
|
|
app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(resourceDir, "public", "stylesheets")));
|
|
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(resourceDir, "public", "libraries")));
|
2025-04-24 22:36:10 +03:00
|
|
|
app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(resourceDir, "public", "fonts")));
|
2025-04-25 14:06:33 +03:00
|
|
|
app.use(`/${assetPath}/translations/`, persistentCacheStatic(path.join(resourceDir, "public", "translations")));
|
2025-04-24 14:55:11 +03:00
|
|
|
app.use(`/${assetPath}/images`, persistentCacheStatic(path.join(resourceDir, "assets", "images")));
|
2025-05-02 19:23:29 +03:00
|
|
|
app.use(`/${assetPath}/app-dist/doc_notes`, persistentCacheStatic(path.join(resourceDir, "assets", "doc_notes")));
|
2025-05-03 03:14:00 +03:00
|
|
|
app.use(`/${assetPath}/app-dist/excalidraw/fonts`, persistentCacheStatic(path.join(resourceDir, "node_modules/@excalidraw/excalidraw/dist/prod/fonts")));
|
2024-12-14 10:10:10 +02:00
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/fonts`, express.static(path.join(srcRoot, "public/fonts")));
|
2025-04-23 10:06:37 +03:00
|
|
|
app.use(`/assets/vX/images`, express.static(path.join(srcRoot, "..", "images")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/stylesheets`, express.static(path.join(srcRoot, "public/stylesheets")));
|
2025-04-18 21:59:14 +03:00
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(srcRoot, "public/libraries")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/libraries`, express.static(path.join(srcRoot, "..", "libraries")));
|
2025-04-18 13:31:59 +03:00
|
|
|
|
2025-04-24 14:55:11 +03:00
|
|
|
const nodeModulesDir = isDev ? path.join(srcRoot, "..", "node_modules") : path.join(resourceDir, "node_modules");
|
2025-04-18 13:31:59 +03:00
|
|
|
|
|
|
|
|
app.use(`/node_modules/@excalidraw/excalidraw/dist/fonts/`, express.static(path.join(nodeModulesDir, "@excalidraw/excalidraw/dist/prod/fonts/")));
|
|
|
|
|
app.use(`/${assetPath}/node_modules/@excalidraw/excalidraw/dist/fonts/`, persistentCacheStatic(path.join(nodeModulesDir, "@excalidraw/excalidraw/dist/prod/fonts/")));
|
2023-11-09 18:21:53 -03:00
|
|
|
|
|
|
|
|
// KaTeX
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/katex.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/katex.min.js")));
|
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/contrib/mhchem.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/contrib/mhchem.min.js")));
|
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/contrib/auto-render.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/contrib/auto-render.min.js")));
|
2023-11-09 18:21:53 -03:00
|
|
|
// expose the whole dist folder
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/node_modules/katex/dist/`, express.static(path.join(nodeModulesDir, "katex/dist/")));
|
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/")));
|
2023-11-22 19:58:54 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery/dist/")));
|
2023-11-22 20:22:16 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery-hotkeys/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery-hotkeys/")));
|
2023-11-22 20:22:16 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery.fancytree/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery.fancytree/dist/")));
|
2023-11-22 19:58:54 +01:00
|
|
|
}
|
2023-05-07 15:23:46 +02:00
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2023-05-07 15:23:46 +02:00
|
|
|
register
|
|
|
|
|
};
|