mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 23:43:35 +00:00
⚡ perf: Keep the Mermaid Chunk Off the Boot Path (#16493)
* 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.
This commit is contained in:
parent
75bb1899fc
commit
4332187ada
2 changed files with 78 additions and 0 deletions
|
|
@ -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, '/');
|
||||
|
|
|
|||
61
e2e/specs/mock/scenarios/lazy-mermaid.spec.ts
Normal file
61
e2e/specs/mock/scenarios/lazy-mermaid.spec.ts
Normal file
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue