Skip to content

Build your personal knowledge base with Trilium Notes

perf(share): compile each share template once

Elian Doran5266080bba3b
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index a5efb27374..934bb12ad0 100644
--- a/apps/standalone/vite.config.mts
+++ b/apps/standalone/vite.config.mts
@@ -373,8 +373,9 @@ export default defineConfig(() => ({
replacement: join(__dirname, "src/stubs/empty.ts")
},
// EJS renders the share pages. Its ESM entry imports `node:fs` and `node:path` for the
- // file-loading it only reaches without an `includer`; the share renderer always passes
- // one, so the package's own browser build serves it and resolves in this bundle.
+ // file-loading it only reaches through its own `include()` without an `includer`; the
+ // share renderer resolves includes itself or passes an `includer`, so the package's own
+ // browser build serves it and resolves in this bundle.
{
find: /^ejs$/,
replacement: join(__dirname, "../../node_modules/ejs/ejs.min.js")
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index ce89b6bed5..3a55dcc80c 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -16,8 +16,8 @@ import * as utils from "../services/utils/index.js";
import { buildNote } from "../test/becca_easy_mocking.js";
import { buildShareNote, buildShareNotes } from "../test/shaca_mocking.js";
import {
- assetUrlFragment, ensureShareHighlighting, getContent, preparePageContent, readShareTemplate,
- renderCode, renderNoteContent, renderNoteForExport, type Result
+ assetUrlFragment, ensureShareHighlighting, getContent, preparePageContent, renderCode,
+ renderNoteContent, renderNoteForExport, renderShareTemplate, type Result
} from "./content_renderer.js";
import type SNote from "./shaca/entities/snote.js";
import shaca from "./shaca/shaca.js";
@@ -1622,7 +1622,7 @@ describe("content_renderer", () => {
const note = shaca.getNote(noteId);
const { header, content, isEmpty } = getContent(note);
- const html = ejs.render(readShareTemplate("page"), {
+ const html = renderShareTemplate("page", {
note,
header,
content,
@@ -1660,15 +1660,34 @@ describe("content_renderer", () => {
headings: [],
toc: [],
isPageInNavigation: false
- }, {
- includer: (path: string) => ({
- template: readShareTemplate(path)
- })
});
return parse(html).querySelectorAll("#childLinks a");
}
});
+
+ describe("renderShareTemplate", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("compiles a template once per text, passing include data down nested includes", () => {
+ const templates: Record<string, string> = {
+ outer: `<%= title %>:<%- include("item", { depth: 1 }) %>`,
+ item: `[<%= depth %><% if (depth < 3) { %><%- include("item", { depth: depth + 1 }) %><% } %>]`
+ };
+ vi.spyOn(getShareProvider(), "readTemplate").mockImplementation((name) => templates[name]);
+ const compile = vi.spyOn(ejs, "compile");
+
+ expect(renderShareTemplate("outer", { title: "T" })).toBe("T:[1[2[3]]]");
+ expect(renderShareTemplate("outer", { title: "U" })).toBe("U:[1[2[3]]]");
+ expect(compile).toHaveBeenCalledTimes(2);
+
+ templates.item = `(<%= title %>)`;
+ expect(renderShareTemplate("outer", { title: "V" })).toBe("V:(V)");
+ expect(compile).toHaveBeenCalledTimes(3);
+ });
+ });
});
describe("content_renderer pages", () => {
diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.ts
index e0464ba740..ceff22af00 100644
--- a/packages/trilium-core/src/share/content_renderer.ts
+++ b/packages/trilium-core/src/share/content_renderer.ts
@@ -372,9 +372,7 @@ function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs)
toc: getTableOfContents(headings),
isPageInNavigation: hasActiveItem(navigation)
};
- return ejs.render(readShareTemplate("page"), pageOpts, {
- includer: (path) => ({ template: readShareTemplate(path) })
- });
+ return renderShareTemplate("page", pageOpts);
}
/**
@@ -440,6 +438,30 @@ export function readShareTemplate(name: string) {
return getShareProvider().readTemplate(name);
}
+/**
+ * Renders the share theme's EJS template of that name with `data`. Each template text compiles
+ * once, so a template edited in development compiles again. The templates' `include()` resolves
+ * to the `include` passed in `data`, which the template's `with (locals)` block finds before EJS's
+ * own: EJS compiles an included template again on every call, and its `filename` cache needs
+ * `fs.existsSync`, which the browser build of EJS lacks.
+ */
+export function renderShareTemplate(name: string, data: ejs.Data): string {
+ const template = readShareTemplate(name);
+ let render = compiledShareTemplates.get(template);
+ if (!render) {
+ render = ejs.compile(template);
+ compiledShareTemplates.set(template, render);
+ }
+
+ return render({
+ ...data,
+ include: (path: string, includeData?: ejs.Data) =>
+ renderShareTemplate(path, { ...data, ...includeData })
+ });
+}
+
+const compiledShareTemplates = new Map<string, ejs.TemplateFunction>();
+
/**
* Returns the prefix the share theme resolves built-in assets against. The share pages live one
* path segment deeper than the app, so outside dev the prefix climbs back out of `/share/`.