Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(fonts): emit Inter once, not twice
Elian Doranf85462034e48
diff --git a/apps/client/src/widgets/type_widgets/file/PdfViewer.spec.tsx b/apps/client/src/widgets/type_widgets/file/PdfViewer.spec.tsxindex 35c55a5ffb..46ce907f84 100644--- a/apps/client/src/widgets/type_widgets/file/PdfViewer.spec.tsx+++ b/apps/client/src/widgets/type_widgets/file/PdfViewer.spec.tsx@@ -1,6 +1,9 @@+import { existsSync } from "node:fs";+import path from "node:path";+import { afterEach, describe, expect, it } from "vitest";-import { getPdfUrl } from "./PdfViewer";+import { getFontFaceCss, getPdfUrl } from "./PdfViewer";describe("getPdfUrl", () => {afterEach(() => history.replaceState({}, "", "/"));@@ -16,3 +19,37 @@ describe("getPdfUrl", () => {expect(getPdfUrl("revisions/rev1/download")).not.toContain("..");});});++describe("getFontFaceCss", () => {+ const originalAssetPath = window.glob.assetPath;++ afterEach(() => {+ window.glob.assetPath = originalAssetPath;+ history.replaceState({}, "", "/");+ });++ it("resolves every face against the deployment root, never relative to the viewer", () => {+ // Standalone serves the client from the root, so its asset path is a bare `.`.+ window.glob.assetPath = ".";+ expect(getFontFaceCss()).toContain("url('/fonts/Inter/Inter-VariableFont_opsz,wght.woff2')");++ // The server version-stamps the prefix, and keeps it relative so a subpath deployment works.+ window.glob.assetPath = "assets/v1.2.3";+ history.replaceState({}, "", "/trilium/#root/abc123");+ expect(getFontFaceCss()).toContain("url('/trilium/assets/v1.2.3/fonts/Inter/");++ expect(getFontFaceCss()).not.toContain("..");+ });++ it("names files the client actually ships", () => {+ window.glob.assetPath = ".";++ const fontsDir = path.join(__dirname, "../../../fonts");+ const files = [...getFontFaceCss().matchAll(/url\('\/fonts\/([^']+)'\)/g)].map(([, file]) => file);++ expect(files.length).toBeGreaterThan(0);+ for (const file of files) {+ expect(existsSync(path.join(fontsDir, file)), `${file} is missing from ${fontsDir}`).toBe(true);+ }+ });+});diff --git a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsxindex 299730ee17..5e05e96f04 100644--- a/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx+++ b/apps/client/src/widgets/type_widgets/file/PdfViewer.tsx@@ -2,15 +2,15 @@ import type { HTMLAttributes, RefObject } from "preact";import { useCallback, useEffect, useRef } from "preact/hooks";import { useSyncedRef, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";-import Inter from "./../../../fonts/Inter/Inter-VariableFont_opsz,wght.woff2";interface FontDefinition {name: string;- url: string;+ /** Path under the client's `fonts` directory, resolved by {@link getFontFaceCss}. */+ path: string;}const FONTS: FontDefinition[] = [- {name: "Inter", url: Inter},+ {name: "Inter", path: "Inter/Inter-VariableFont_opsz,wght.woff2"},];interface PdfViewerProps extends Pick<HTMLAttributes<HTMLIFrameElement>, "tabIndex"> {@@ -73,7 +73,7 @@ function useStyleInjection(iframeRef: RefObject<HTMLIFrameElement>, disableSelecdoc.head.appendChild(style);const fontStyles = doc.createElement("style");- fontStyles.textContent = FONTS.map(injectFont).join("\n");+ fontStyles.textContent = getFontFaceCss();doc.head.appendChild(fontStyles);if (disableSelection) {@@ -120,19 +120,35 @@ function cssVarsToString(vars: Record<string, string>) {/*** Resolves an API path such as `attachments/<id>/open` to a root-relative URL for- * {@link PdfViewerProps.pdfUrl}. A URL relative to the viewer needs `../../` to climb out of- * /pdfjs/web, which proxies that filter path traversal reject before the request reaches- * Trilium (Nginx Proxy Manager's "Block Common Exploits" answers 403). See #8877.+ * {@link PdfViewerProps.pdfUrl}.*/export function getPdfUrl(apiPath: string) {- return new URL(`${window.glob.baseApiUrl}${apiPath}`, window.location.href).pathname;+ return resolveFromDeployment(`${window.glob.baseApiUrl}${apiPath}`);}-function injectFont(font: FontDefinition) {- return `+/**+ * The `@font-face` rules injected into the viewer, so its chrome matches the app's typography.+ * Each face points at the copy `viteStaticCopy` ships, which is the one `theme-next/base.css`+ * loads, so the page and the viewer share a single cache entry.+ */+export function getFontFaceCss() {+ return FONTS.map(({ name, path }) => {+ const url = resolveFromDeployment(`${window.glob.assetPath}/fonts/${path}`);++ return `@font-face {- font-family: '${font.name}';- src: url('${font.url}');+ font-family: '${name}';+ src: url('${url}');}`;+ }).join("\n");+}++/**+ * Resolves a URL against the deployment root rather than the viewer. A URL relative to the viewer+ * needs `../../` to climb out of /pdfjs/web, which proxies that filter path traversal reject before+ * the request reaches Trilium (Nginx Proxy Manager's "Block Common Exploits" answers 403). See #8877.+ */+function resolveFromDeployment(url: string) {+ return new URL(url, window.location.href).pathname;}