From 4332187ada41a184f07990867a1c2624a78eaddb Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Tue, 29 Sep 2026 18:39:15 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20Keep=20the=20Mermaid=20Chun?= =?UTF-8?q?k=20Off=20the=20Boot=20Path=20(#16493)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: keep the mermaid chunk off the boot path The chunk groups capture each module's dependencies with it, and mermaid depends on Vite's dynamic-import preload helper, the Buffer and process shims, DOMPurify, uuid and dayjs. Those landed in the 3 MB mermaid chunk, and because the boot chunks import them, every page preloaded and evaluated mermaid before its first API request, on the path to the largest contentful paint. Two higher-priority groups claim them first (runtime-shims and shared-libs), so mermaid loads only through useMermaid's dynamic import. A new mock scenario checks that a chat without diagrams never fetches the chunk and that a diagram reply still loads and renders it. * test: wait on the reply and the composer instead of network idle The app keeps connections open, so network idle never arrives. --- client/vite.config.ts | 17 ++++++ e2e/specs/mock/scenarios/lazy-mermaid.spec.ts | 61 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 e2e/specs/mock/scenarios/lazy-mermaid.spec.ts diff --git a/client/vite.config.ts b/client/vite.config.ts index fa1f017082..3a923b214b 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -222,6 +222,23 @@ export default defineConfig(({ command }) => ({ output: { codeSplitting: { groups: [ + /** + * The boot chunks import Vite's dynamic-import preload helper, the Buffer and + * process shims, DOMPurify, uuid and dayjs. The catch-all group below captures each + * module's dependencies with it, so these used to land in the mermaid chunk, which + * depends on them too, and every page then downloaded and evaluated that whole chunk + * before its first request. Claiming them first keeps mermaid lazy. + */ + { + name: 'runtime-shims', + test: /vite[\\/]preload-helper|node_modules[\\/]vite-plugin-node-polyfills[\\/]/, + priority: 1, + }, + { + name: 'shared-libs', + test: /node_modules[\\/](dompurify|uuid|dayjs)[\\/]/, + priority: 1, + }, { name(id: string) { const normalizedId = id.replace(/\\/g, '/'); diff --git a/e2e/specs/mock/scenarios/lazy-mermaid.spec.ts b/e2e/specs/mock/scenarios/lazy-mermaid.spec.ts new file mode 100644 index 0000000000..be0308a862 --- /dev/null +++ b/e2e/specs/mock/scenarios/lazy-mermaid.spec.ts @@ -0,0 +1,61 @@ +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { + MOCK_ENDPOINTS, + NEW_CHAT_PATH, + messagesView, + selectMockEndpoint, + sendMessage, + sendMessageAndWaitForCompletion, +} from '../helpers'; + +/** + * Mermaid is about 3 MB of JavaScript, and `useMermaid` imports it on demand. The chunk used to + * load at boot anyway: the build's chunk groups captured shared modules the boot chunks import + * (Vite's preload helper, the Buffer shim, DOMPurify, uuid, dayjs) into the mermaid chunk, so + * every page downloaded and evaluated it before its first API request, on the path to the + * largest contentful paint. + */ + +const MERMAID_CHUNK = /\/assets\/mermaid\.[\w-]+\.js$/; + +function recordMermaidRequests(page: Page): string[] { + const requested: string[] = []; + page.on('request', (request) => { + if (MERMAID_CHUNK.test(new URL(request.url()).pathname)) { + requested.push(request.url()); + } + }); + return requested; +} + +test.describe('the mermaid chunk', () => { + test('a chat without diagrams never downloads mermaid @scenario:a-chat-without-diagrams-never-downloads-mermaid', async ({ + page, + }) => { + const requested = recordMermaidRequests(page); + await page.goto(NEW_CHAT_PATH, { timeout: 15000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + + const response = await sendMessageAndWaitForCompletion(page, 'Hello there'); + expect(response.ok()).toBeTruthy(); + await expect(messagesView(page).getByText('Hello there')).toBeVisible(); + + expect(requested).toEqual([]); + }); + + test('a diagram reply loads mermaid on demand and renders it @scenario:a-diagram-reply-loads-mermaid-on-demand', async ({ + page, + }) => { + const requested = recordMermaidRequests(page); + await page.goto(NEW_CHAT_PATH, { timeout: 15000 }); + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible(); + expect(requested, 'the boot must not fetch mermaid').toEqual([]); + + const response = await sendMessage(page, 'E2E_MERMAID_ARTIFACT_REPLY'); + expect(response.ok()).toBeTruthy(); + await expect(messagesView(page).getByRole('img', { name: 'Mermaid diagram' })).toBeVisible(); + expect(requested).not.toHaveLength(0); + }); +});