From 5d6375d845360ea0a721c33b682f4062d88d2279 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 12 May 2024 16:14:40 +0300 Subject: [PATCH] #12: Fix static serve of manifest & robots --- electron/src/electron.ts | 6 +++++- server/src/app.ts | 5 ++--- server/src/index.ts | 12 ++++++++++-- server/src/types.ts | 8 ++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/electron/src/electron.ts b/electron/src/electron.ts index 69b0b95b4..f53707513 100644 --- a/electron/src/electron.ts +++ b/electron/src/electron.ts @@ -85,6 +85,10 @@ const app = startTrilium({ getInitialTheme() { return electron.nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; - } + }, + + registerAdditionalMiddleware(app) { + // No additional express middleware required on Electron. + }, }); electronRouting(app); \ No newline at end of file diff --git a/server/src/app.ts b/server/src/app.ts index 03e9eb338..92276d684 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -33,11 +33,10 @@ function buildApp(appConfig: AppConfig) { app.use(express.raw({ limit: '500mb' })); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); - app.use(express.static(path.join(__dirname, 'public/root'))); - app.use(`/manifest.webmanifest`, express.static(path.join(__dirname, 'public/manifest.webmanifest'))); - app.use(`/robots.txt`, express.static(path.join(__dirname, 'public/robots.txt'))); + app.use(express.static(path.join(__dirname, 'public/root'))); app.use(sessionParser); app.use(favicon(`${__dirname}/../../common/images/app-icons/win/icon.ico`)); + appConfig.registerAdditionalMiddleware(app); require('./routes/assets').register(app); require('./routes/routes').register(app, appConfig); diff --git a/server/src/index.ts b/server/src/index.ts index 032638bf0..0b37a0e69 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,6 @@ import startTrilium = require("./www"); +import path = require("path"); +import express = require("express"); startTrilium({ setupCompleteCallback: (res) => { @@ -8,5 +10,11 @@ startTrilium({ getInitialTheme() { // default based on the poll in https://github.com/zadam/trilium/issues/2516 return "dark"; - } -}); \ No newline at end of file + }, + + registerAdditionalMiddleware(app) { + const assetsDir = path.join(__dirname, "..", "..", "client", "assets"); + app.use(`/manifest.webmanifest`, express.static(path.join(assetsDir, 'manifest.webmanifest'))); + app.use(`/robots.txt`, express.static(path.join(assetsDir, 'robots.txt'))); + }, +}); diff --git a/server/src/types.ts b/server/src/types.ts index 5d7eb9d54..e14d811ba 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -1,4 +1,5 @@ import { Response } from "express"; +import type { Express } from "express"; export type InitialThemeCallback = () => "dark" | "light"; @@ -18,6 +19,13 @@ export interface InitDbOptions { * Handles differences between clients, for example allowing different behaviour when running from the web server versus the desktop application. */ export interface AppConfig extends InitDbOptions { + /** + * Called when the express app is built, in order to define additional middleware that might be specific to a web or desktop application. + * + * @param app the express app to add the middleware on. + */ + registerAdditionalMiddleware: (app: Express) => void; + /** Callback to be invoked when first setup is complete. */ setupCompleteCallback: SetupCompleteCallback; }