Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

⚡ perf: Keep the Mermaid Chunk Off the Boot Path (#16493)

Marco Beretta9689ebf30c4a
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);
+ });
+});