Compare commits

..
Author SHA1 Message Date
paseo-bot[bot] 5e6284b099 fix(app): explain when an HTML file is too large to preview
Electron leaves the preview blank when the encoded document URL exceeds its 2 MiB limit. Show the existing file size message before trying to load a URL that Electron will reject.
2026-11-30 20:22:20 +00:00
2 changed files with 36 additions and 1 deletions
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { render } from "@testing-library/react";
import { render, within } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { FileHtmlPreview } from "./html-preview.web";
@@ -15,4 +15,22 @@ describe("FileHtmlPreview on web", () => {
expect(changedFrame).not.toBe(firstFrame);
expect(decodeURIComponent(changedFrame.getAttribute("src") ?? "")).toContain("Changed plan");
});
it("reports oversized desktop previews using the encoded URL length", () => {
window.paseoDesktop = { platform: "linux" };
try {
// Percent encoding puts this document over the limit even though its source is smaller.
const html = `<h1>Slides</h1><!--${" ".repeat(750_000)}-->`;
const view = render(<FileHtmlPreview html={html} testID="preview" />);
const preview = within(view.container);
expect(preview.getByRole("status").textContent).toBe("panels.file.tooLargeToDisplay");
expect(preview.queryByTestId("preview")).toBeNull();
view.rerender(<FileHtmlPreview html="<h1>Slides</h1>" testID="preview" />);
expect(preview.getByTestId("preview").getAttribute("sandbox")).toBe("allow-scripts");
} finally {
delete window.paseoDesktop;
}
});
});
@@ -1,5 +1,6 @@
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { isElectronRuntime } from "@/desktop/host";
import { withPreviewCsp } from "./html-preview-csp";
// `allow-scripts` alone: the file gets an opaque origin, so a plan page can run
@@ -16,6 +17,9 @@ import { withPreviewCsp } from "./html-preview-csp";
// over with a directive browsers ignore.
const SANDBOX = "allow-scripts";
// Electron rejects iframe navigations above 2 MiB of encoded URL bytes.
const MAX_DESKTOP_PREVIEW_URL_LENGTH = 2 * 1024 * 1024;
const iframeStyle = {
flex: 1,
minHeight: 0,
@@ -23,6 +27,12 @@ const iframeStyle = {
backgroundColor: "white",
} as const;
const statusStyle = {
flex: 1,
display: "grid",
placeItems: "center",
} as const;
export function FileHtmlPreview({ html, testID }: { html: string; testID?: string }) {
const { t } = useTranslation();
// A document URL lets the browser resolve fragment links within the preview.
@@ -31,6 +41,13 @@ export function FileHtmlPreview({ html, testID }: { html: string; testID?: strin
() => `data:text/html;charset=utf-8,${encodeURIComponent(withPreviewCsp(html))}`,
[html],
);
if (source.length > MAX_DESKTOP_PREVIEW_URL_LENGTH && isElectronRuntime()) {
return (
<div role="status" style={statusStyle}>
{t("panels.file.tooLargeToDisplay")}
</div>
);
}
return (
// Each document loads in a new frame. The Source view keeps this preview mounted and hidden,
// and it gets a changed document in the same render that shows it again. Chromium can leave a