Skip to content

Build your personal knowledge base with Trilium Notes

feat(server): bootstrap route

Elian Doran76526e0a965c
diff --git a/apps/client/src/desktop.html b/apps/client/src/desktop.html
index e835be4b54..e237fef67d 100644
--- a/apps/client/src/desktop.html
+++ b/apps/client/src/desktop.html
@@ -1,10 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
-
+ <meta charset="utf-8">
+ <link rel="shortcut icon" href="favicon.ico">
+ <meta name="mobile-web-app-capable" content="yes">
+ <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover" />
+ <link rel="manifest" crossorigin="use-credentials" href="manifest.webmanifest">
+ <title>Trilium Notes</title>
</head>
- <body>
- Trilium goes here.
+ <body id="trilium-app">
+ <noscript><%= t("javascript-required") %></noscript>
+
+ <script>
+ // hide body to reduce flickering on the startup. This is done through JS and not CSS to not hide <noscript>
+ document.getElementsByTagName("body")[0].style.display = "none";
+ </script>
+
+ <div class="dropdown-menu dropdown-menu-sm" id="context-menu-container" style="display: none"></div>
+
+ <!-- Required for match the PWA's top bar color with the theme -->
+ <!-- This works even when the user directly changes --root-background in CSS -->
+ <div id="background-color-tracker" style="position: absolute; visibility: hidden; color: var(--root-background); transition: color 1ms;"></div>
+
+ <!-- Required for correct loading of scripts in Electron -->
+ <script>
+ if (typeof module === 'object') {window.module = module; module = undefined;}
+ </script>
</body>
</html>
\ No newline at end of file
diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsx
index 3c852ba140..82cbe013c6 100644
--- a/apps/client/src/widgets/layout/StatusBar.tsx
+++ b/apps/client/src/widgets/layout/StatusBar.tsx
@@ -5,7 +5,7 @@ import { Dropdown as BootstrapDropdown } from "bootstrap";
import clsx from "clsx";
import { type ComponentChildren, RefObject } from "preact";
import { createPortal } from "preact/compat";
-import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
+import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import { CommandNames } from "../../components/app_context";
import NoteContext from "../../components/note_context";
@@ -338,15 +338,19 @@ interface AttributesProps extends StatusBarContext {
function AttributesButton({ note, attributesShown, setAttributesShown }: AttributesProps) {
const [ count, setCount ] = useState(note.attributes.length);
+ const getAttributeCount = useCallback((note: FNote) => {
+ return note.getAttributes().filter(a => !a.isAutoLink).length;
+ }, []);
+
// React to note changes.
useEffect(() => {
- setCount(note.getAttributes().filter(a => !a.isAutoLink).length);
- }, [ note ]);
+ setCount(getAttributeCount(note));
+ }, [ note, getAttributeCount ]);
// React to changes in count.
useTriliumEvent("entitiesReloaded", (({loadResults}) => {
if (loadResults.getAttributeRows().some(attr => attributes.isAffecting(attr, note))) {
- setCount(note.attributes.length);
+ setCount(getAttributeCount(note));
}
}));
diff --git a/apps/server/src/assets/views/desktop.ejs b/apps/server/src/assets/views/desktop.ejs
index 09b3e2473b..96f617bd79 100644
--- a/apps/server/src/assets/views/desktop.ejs
+++ b/apps/server/src/assets/views/desktop.ejs
@@ -1,41 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
- <meta charset="utf-8">
- <link rel="shortcut icon" href="favicon.ico">
- <meta name="mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover" />
- <link rel="manifest" crossorigin="use-credentials" href="manifest.webmanifest">
- <title>Trilium Notes</title>
<style id="trilium-icon-packs">
<%- iconPackCss %>
</style>
<script src="<%= appPath %>/runtime.js" crossorigin type="module"></script>
</head>
<body
- id="trilium-app"
class="desktop heading-style-<%= headingStyle %> layout-<%= layoutOrientation %> platform-<%= platform %> <%= isElectron ? 'electron' : '' %> <%= hasNativeTitleBar ? 'native-titlebar' : '' %> <%= hasBackgroundEffects ? 'background-effects' : '' %>"
lang="<%= currentLocale.id %>" dir="<%= currentLocale.rtl ? 'rtl' : 'ltr' %>"
>
-<noscript><%= t("javascript-required") %></noscript>
-
-<script>
- // hide body to reduce flickering on the startup. This is done through JS and not CSS to not hide <noscript>
- document.getElementsByTagName("body")[0].style.display = "none";
-</script>
-
-<div class="dropdown-menu dropdown-menu-sm" id="context-menu-container" style="display: none"></div>
<%- include("./partials/windowGlobal.ejs", locals) %>
-<!-- Required for match the PWA's top bar color with the theme -->
-<!-- This works even when the user directly changes --root-background in CSS -->
-<div id="background-color-tracker" style="position: absolute; visibility: hidden; color: var(--root-background); transition: color 1ms;"></div>
-
-<!-- Required for correct loading of scripts in Electron -->
-<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
-
<link href="<%= assetPath %>/stylesheets/ckeditor-theme.css" rel="stylesheet">
<link href="api/fonts" rel="stylesheet">
<link href="<%= assetPath %>/stylesheets/theme-light.css" rel="stylesheet">
diff --git a/apps/server/src/routes/assets.ts b/apps/server/src/routes/assets.ts
index 20422ba4b3..d8a67d881d 100644
--- a/apps/server/src/routes/assets.ts
+++ b/apps/server/src/routes/assets.ts
@@ -32,7 +32,12 @@ async function register(app: express.Application) {
css: { devSourcemap: true }
});
app.use(`/${assetUrlFragment}/`, vite.middlewares);
- app.use(`/`, (req, res, next) => {
+ app.get(`/`, (req, res, next) => {
+ // We force the page to not be cached since on mobile the CSRF token can be
+ // broken when closing the browser and coming back in to the page.
+ // The page is restored from cache, but the API call fail.
+ res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
+
req.url = `/${assetUrlFragment}/src/desktop.html`;
vite.middlewares(req, res, next);
});
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts
index b6ae7bf086..30c390c316 100644
--- a/apps/server/src/routes/index.ts
+++ b/apps/server/src/routes/index.ts
@@ -15,10 +15,10 @@ import sql from "../services/sql.js";
import { isDev, isElectron, isWindows11 } from "../services/utils.js";
import { generateToken as generateCsrfToken } from "./csrf_protection.js";
+
type View = "desktop" | "mobile" | "print";
-function index(req: Request, res: Response) {
- const view = getView(req);
+export function bootstrap(req: Request, res: Response) {
const options = optionService.getOptionMap();
//'overwrite' set to false (default) => the existing token will be re-used and validated
@@ -26,17 +26,13 @@ function index(req: Request, res: Response) {
const csrfToken = generateCsrfToken(req, res, false, false);
log.info(`CSRF token generation: ${csrfToken ? "Successful" : "Failed"}`);
- // We force the page to not be cached since on mobile the CSRF token can be
- // broken when closing the browser and coming back in to the page.
- // The page is restored from cache, but the API call fail.
- res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
-
+ const view = getView(req);
const theme = options.theme;
const themeNote = attributeService.getNoteWithLabel("appTheme", theme);
const nativeTitleBarVisible = options.nativeTitleBarVisible === "true";
const iconPacks = getIconPacks();
- res.render(view, {
+ res.send({
device: view,
csrfToken,
themeCssUrl: getThemeCssUrl(theme, themeNote),
@@ -133,7 +129,3 @@ function getThemeCssUrl(theme: string, themeNote: BNote | null) {
function getAppCssNoteIds() {
return attributeService.getNotesWithLabel("appCss").map((note) => note.noteId);
}
-
-export default {
- index
-};
diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts
index 5bbb590063..9e31d1bcac 100644
--- a/apps/server/src/routes/routes.ts
+++ b/apps/server/src/routes/routes.ts
@@ -63,6 +63,7 @@ import totp from './api/totp.js';
// API routes
import treeApiRoute from "./api/tree.js";
import { doubleCsrfProtection as csrfMiddleware } from "./csrf_protection.js";
+import * as indexRoute from "./index.js";
import loginRoute from "./login.js";
import { apiResultHandler, apiRoute, asyncApiRoute, asyncRoute, route, router, uploadMiddlewareWithErrorHandling } from "./route_api.js";
// page routes
@@ -84,6 +85,7 @@ function register(app: express.Application) {
skipSuccessfulRequests: true // successful auth to rate-limited ETAPI routes isn't counted. However, successful auth to /login is still counted!
});
+ route(GET, "/bootstrap", [ auth.checkAuth ], indexRoute.bootstrap);
route(PST, "/login", [loginRateLimiter], loginRoute.login);
route(PST, "/logout", [csrfMiddleware, auth.checkAuth], loginRoute.logout);
route(PST, "/set-password", [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPassword);