Skip to content

Build your personal knowledge base with Trilium Notes

perf(spreadsheet): export xlsx as a plain download

Elian Doran2cff7f9baf22
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
index 8bfcd7ef1b..d47be1724e 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
@@ -97,8 +97,8 @@ function SpreadsheetEditor({ note, noteContext, readOnly, locale }: TypeWidgetPr
useClampEdgeNavigation(apiRef);
useAnchorCellEditorOnScroll(apiRef);
useDarkMode(apiRef);
- usePersistence(note, noteContext, apiRef, containerRef);
- useSpreadsheetExport(apiRef, note, noteContext);
+ const spacedUpdate = usePersistence(note, noteContext, apiRef, containerRef);
+ useSpreadsheetExport(apiRef, note, noteContext, spacedUpdate);
useSearchIntegration(apiRef, noteContext);
useDismissDialogsOnNoteSwitch(apiRef);
useFixRadixPortals();
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
index 68559430ad..04408ce122 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
@@ -4,12 +4,13 @@ import { MutableRef } from "preact/hooks";
import NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
-import server from "../../../services/server";
+import open from "../../../services/open";
+import type SpacedUpdate from "../../../services/spaced_update";
import toast from "../../../services/toast";
import utils from "../../../services/utils";
+import type { SavedData } from "../../react/hooks";
import { useTriliumEvent } from "../../react/hooks";
-const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const CSV_MIME = "text/csv;charset=utf-8";
const ZIP_MIME = "application/zip";
// Excel on Windows only auto-detects UTF-8 in a CSV when it starts with a byte-order mark.
@@ -23,10 +24,10 @@ const UTF8_BOM = "\uFEFF";
* `@triliumnext/commons`, dynamically imported so they are only fetched on export; XLSX is
* rendered by the backend, which keeps exceljs out of the client bundle entirely.
*/
-export default function useSpreadsheetExport(apiRef: MutableRef<FUniver | undefined>, note: FNote, noteContext: NoteContext | null | undefined) {
+export default function useSpreadsheetExport(apiRef: MutableRef<FUniver | undefined>, note: FNote, noteContext: NoteContext | null | undefined, spacedUpdate: SpacedUpdate<SavedData | undefined>) {
useTriliumEvent("exportXlsx", ({ ntxId }) => {
if (ntxId !== noteContext?.ntxId) return;
- void exportToXlsx(apiRef.current, note);
+ void exportToXlsx(note, spacedUpdate);
});
useTriliumEvent("exportCsv", ({ ntxId }) => {
if (ntxId !== noteContext?.ntxId) return;
@@ -34,16 +35,13 @@ export default function useSpreadsheetExport(apiRef: MutableRef<FUniver | undefi
});
}
-async function exportToXlsx(univerAPI: FUniver | undefined, note: FNote) {
- const json = serializeWorkbook(univerAPI);
- if (json == null) return;
-
+async function exportToXlsx(note: FNote, spacedUpdate: SpacedUpdate<SavedData | undefined>) {
try {
- // The backend renders the workbook and resolves its attachment images, so the client never
- // loads exceljs. It answers with base64, which is what the data-URL download wants anyway.
- const body = { content: json };
- const { base64 } = await server.post<{ base64: string }>("spreadsheet/xlsx", body);
- utils.triggerDownload(downloadName(note, "xlsx"), `data:${XLSX_MIME};base64,${base64}`);
+ // The backend renders from the note's stored content, so a pending edit has to reach the
+ // database before the download is requested. `open.download` navigates rather than clicking
+ // an anchor, which keeps the workbook off the wire in both directions.
+ await spacedUpdate.updateNowIfNecessary();
+ open.download(open.getUrlForDownload(`api/spreadsheet/${note.noteId}/xlsx`));
} catch (e) {
console.error("[spreadsheet-export] xlsx failed", e);
toast.showError(t("spreadsheet.export-failed"));
@@ -84,13 +82,6 @@ function countVisibleSheets(wbData: { sheetOrder?: string[]; sheets?: Record<str
return ids.filter((id) => sheets[id] && !sheets[id].hidden).length;
}
-/** Serializes the live workbook in the same shape the note is persisted in. */
-function serializeWorkbook(univerAPI: FUniver | undefined): string | null {
- const workbook = univerAPI?.getActiveWorkbook();
- if (!workbook) return null;
- return JSON.stringify({ version: 1, workbook: workbook.save() });
-}
-
async function download(note: FNote, extension: string, blob: Blob) {
// Download via a data URL (utils.triggerDownload): a blob-URL anchor click after these
// awaits gets silently blocked once the user-activation is consumed, whereas the
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
index 6caf09d602..4446f2b046 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
@@ -307,6 +307,9 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n
lifecycleDisposable?.dispose();
};
}, [ apiRef ]);
+
+ // Returned so the export can flush a pending save before the backend reads the note's content.
+ return spacedUpdate;
}
/**
diff --git a/packages/trilium-core/src/routes/api/spreadsheet.spec.ts b/packages/trilium-core/src/routes/api/spreadsheet.spec.ts
index bcd586fe7b..ab60af8581 100644
--- a/packages/trilium-core/src/routes/api/spreadsheet.spec.ts
+++ b/packages/trilium-core/src/routes/api/spreadsheet.spec.ts
@@ -6,11 +6,13 @@ import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
/**
- * Drives the shared spreadsheet render route through {@link CoreApiTester} (no Express), so this
+ * Drives the shared spreadsheet export route through {@link CoreApiTester} (no Express), so this
* spec runs under both the node and standalone (WASM) suites.
*/
let api: CoreApiTester;
+const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+
/** A 1x1 transparent PNG, small enough to be written out in full. */
const PIXEL_PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAA"
+ "C0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
@@ -20,17 +22,25 @@ describe("Spreadsheet API (core)", () => {
api = CoreApiTester.build();
});
- it("renders a workbook to a readable xlsx, and rejects a non-string content", async () => {
+ it("renders the note's stored workbook to a readable xlsx", async () => {
const cellData = { "0": { "0": { v: "Hello", t: 1 }, "1": { v: 42, t: 2 } } };
const ws = (await renderWorkbook(workbookJson(cellData))).getWorksheet("Ledger");
expect(ws?.getCell("A1").value).toBe("Hello");
expect(ws?.getCell("B1").value).toBe(42);
+ });
+
+ it("names the download after the note and refuses a note that is not a spreadsheet", async () => {
+ const noteId = await createSpreadsheetNote("Quarterly Ledger", workbookJson({}));
+ const res = await api.get(`/api/spreadsheet/${noteId}/xlsx`);
- const rejected = await api.post("/api/spreadsheet/xlsx", {
- body: { content: { not: "a string" } }
- });
- expect(rejected.status).toBe(400);
+ expect(res.status).toBe(200);
+ expect(res.headers["Content-Disposition"]).toContain("Quarterly%20Ledger.xlsx");
+ expect(res.headers["Content-Type"]).toBe(XLSX_MIME);
+
+ const { noteId: textNoteId } = await createTextNote(api, { title: "Not a spreadsheet" });
+ expect((await api.get(`/api/spreadsheet/${textNoteId}/xlsx`)).status).toBe(404);
+ expect((await api.get("/api/spreadsheet/nosuchnote/xlsx")).status).toBe(404);
});
it("embeds an image stored as an attachment", async () => {
@@ -65,21 +75,28 @@ describe("Spreadsheet API (core)", () => {
});
});
-/** Posts the workbook JSON to the route and reads the answer back as a real workbook. */
+/** Stores the workbook JSON on a spreadsheet note, downloads it, and reads it back as a workbook. */
async function renderWorkbook(json: string): Promise<ExcelJS.Workbook> {
- const res = await api.post<{ base64: string }>("/api/spreadsheet/xlsx", {
- body: { content: json }
- });
+ const noteId = await createSpreadsheetNote("Ledger note", json);
+ const res = await api.get<Uint8Array>(`/api/spreadsheet/${noteId}/xlsx`);
expect(res.status).toBe(200);
- // The decoder can hand back a view into a larger pooled buffer, so copy the bytes into an
+ // The response body can be a view into a larger pooled buffer, so copy the bytes into an
// ArrayBuffer of their own before exceljs unzips it.
- const bytes = decodeBase64(res.body.base64);
const wb = new ExcelJS.Workbook();
- await wb.xlsx.load(new Uint8Array(bytes).buffer);
+ await wb.xlsx.load(new Uint8Array(res.body).buffer);
return wb;
}
+/** Creates a spreadsheet note holding `content`, returning its id. */
+async function createSpreadsheetNote(title: string, content: string): Promise<string> {
+ const res = await api.post<{ note: { noteId: string } }>("/api/notes/root/children?target=into", {
+ body: { title, type: "spreadsheet", mime: "application/json", content }
+ });
+ expect(res.status).toBe(200);
+ return res.body.note.noteId;
+}
+
/** Wraps cell data into a complete single-sheet workbook payload. */
function workbookJson(cellData: Record<string, Record<string, unknown>>): string {
return JSON.stringify({
diff --git a/packages/trilium-core/src/routes/api/spreadsheet.ts b/packages/trilium-core/src/routes/api/spreadsheet.ts
index f4023234d3..2b000b2510 100644
--- a/packages/trilium-core/src/routes/api/spreadsheet.ts
+++ b/packages/trilium-core/src/routes/api/spreadsheet.ts
@@ -1,34 +1,49 @@
import type { ResolvedImage } from "@triliumnext/commons/src/lib/spreadsheet/render_to_xlsx.js";
-import type { Request } from "express";
+import type { Request, Response } from "express";
import becca from "../../becca/becca.js";
-import { ValidationError } from "../../errors.js";
-import { encodeBase64 } from "../../services/utils/binary.js";
+import { decodeUtf8, encodeBase64 } from "../../services/utils/binary.js";
+import { getContentDisposition } from "../../services/utils/index.js";
+
+const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// A drawing whose image lives in an attachment, as `persistence.tsx` rewrites it on save.
const ATTACHMENT_IMAGE_SOURCE = /attachments\/([A-Za-z0-9_]+)\/image\//;
/**
- * Renders a spreadsheet workbook to an `.xlsx` file, returned base64-encoded so the client can
- * hand it straight to a data-URL download. The client posts the live editor state instead of a
- * `noteId`, so an export includes edits the debounced save has not flushed yet.
+ * Renders a spreadsheet note to an `.xlsx` file and sends it as a download. The client flushes its
+ * pending save before navigating here, so the stored content it renders is the state the user sees.
*
* `exceljs` is dynamically imported, which both keeps it off the startup path and keeps it out of
* the client bundle: rendering here is the only reason the browser used to need the library.
*/
-async function renderXlsx(req: Request) {
- const { content } = req.body;
+async function exportXlsx(req: Request<{ noteId: string }>, res: Response) {
+ const { noteId } = req.params;
+ const note = becca.getNote(noteId);
+
+ if (!note || note.type !== "spreadsheet") {
+ return res.setHeader("Content-Type", "text/plain").status(404)
+ .send(`Note '${noteId}' is not a spreadsheet.`);
+ }
- if (typeof content !== "string") {
- throw new ValidationError("'content' must be the workbook JSON.");
+ if (!note.isContentAvailable()) {
+ return res.setHeader("Content-Type", "text/plain").status(401)
+ .send("Protected session not available");
}
+ const content = note.getContent();
const { renderSpreadsheetToXlsx } = await import(
"@triliumnext/commons/src/lib/spreadsheet/render_to_xlsx.js"
);
- const buffer = await renderSpreadsheetToXlsx(content, { resolveImage });
+ const buffer = await renderSpreadsheetToXlsx(
+ typeof content === "string" ? content : decodeUtf8(content),
+ { resolveImage }
+ );
- return { base64: encodeBase64(new Uint8Array(buffer)) };
+ res.setHeader("Content-Disposition", getContentDisposition(`${note.title}.xlsx`));
+ res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
+ res.setHeader("Content-Type", XLSX_MIME);
+ res.send(buffer);
}
/**
@@ -90,5 +105,5 @@ function imageExtensionForMime(mime: string): ResolvedImage["extension"] | null
}
export default {
- renderXlsx
+ exportXlsx
};
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index 4d5d8bf54c..5966ef4f35 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -170,7 +170,7 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTrans
// signature, and a query string ends up in every access log along the way.
asyncApiRoute(PST, "/api/link-embed/metadata", linkEmbedRoute.getMetadata);
- asyncApiRoute(PST, "/api/spreadsheet/xlsx", spreadsheetRoute.renderXlsx);
+ asyncRoute(GET, "/api/spreadsheet/:noteId/xlsx", [checkApiAuthOrElectron], spreadsheetRoute.exportXlsx);
// group of the services below are meant to be executed from the outside
// Not transactional: a status read needs no transaction, and one is unopenable during the moment