Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

perf: refine web chunk splitting

tianma-if1d8e7b316193
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 93e71b0d..60a78d58 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -57,6 +57,8 @@ const buildId = process.env.WORKERS_CI_COMMIT_SHA?.slice(0, 12)
?? "local";
const gitDescription = readGitDescription();
const appVersion = resolveAppVersion(readPackageVersion(), gitDescription);
+const OPTIONAL_CHUNK_WARNING_LIMIT_KB = 1_700;
+const TARGET_VENDOR_CHUNK_BYTES = 450 * 1024;
const releaseTimestamp = resolveReleaseTimestamp(process.env.EDGE_EVER_RELEASED_AT) || readLatestReleaseCommitTimestamp();
const deploymentTrigger = resolveDeploymentTrigger(
process.env.EDGE_EVER_DEPLOYMENT_TRIGGER
@@ -161,9 +163,9 @@ export default defineConfig({
],
globIgnores: [
"index.html",
- "**/vendor-beautiful-mermaid-*.js",
+ "**/*beautiful-mermaid*.js",
+ "**/*mermaid.core-*.js",
"**/vendor-mermaid-*.js",
- "**/mermaid.core-*.js",
"**/*Diagram-*.js",
],
navigateFallback: null,
@@ -200,7 +202,7 @@ export default defineConfig({
},
},
{
- urlPattern: ({ url }) => /\/assets\/(?:vendor-(?:beautiful-mermaid|mermaid)|mermaid\.core|.*Diagram-)/.test(url.pathname),
+ urlPattern: ({ url }) => /\/assets\/(?:.*beautiful-mermaid|vendor-mermaid|.*mermaid\.core|.*Diagram-)/.test(url.pathname),
handler: "CacheFirst",
options: {
cacheName: "edgeever-optional-diagrams",
@@ -238,6 +240,11 @@ export default defineConfig({
build: {
outDir: "dist",
emptyOutDir: true,
+ // ELK is distributed as one ~1.6 MiB module by beautiful-mermaid. It is
+ // loaded only when a diagram is rendered and is excluded from HTML
+ // modulepreload and PWA precache; verify-web-performance.mjs enforces
+ // those constraints for every chunk above Vite's default 500 KiB limit.
+ chunkSizeWarningLimit: OPTIONAL_CHUNK_WARNING_LIMIT_KB,
modulePreload: isDesktopBuild
? false
: {
@@ -259,6 +266,7 @@ export default defineConfig({
name: "vendor-code-highlight",
test: /node_modules[\\/](?:lowlight|highlight\.js|@tiptap[\\/]extension-code-block-lowlight)[\\/]/,
priority: 50,
+ maxSize: TARGET_VENDOR_CHUNK_BYTES,
},
{
name: "vendor-react",
@@ -339,16 +347,19 @@ export default defineConfig({
name: "vendor-mermaid-layout",
test: /[\\/](?:cytoscape(?:-[^\\/@]+)?|dagre-d3-es|graphlib|roughjs|khroma|@upsetjs[\\/]venn\.js)(?:@|[\\/])/,
priority: 11,
+ maxSize: TARGET_VENDOR_CHUNK_BYTES,
},
{
name: "vendor-mermaid-render",
test: /[\\/](?:@mermaid-js[\\/](?:parser|tiny)|katex|dompurify|stylis|dayjs|@iconify[\\/]utils)(?:@|[\\/])/,
priority: 11,
+ maxSize: TARGET_VENDOR_CHUNK_BYTES,
},
{
name: "vendor-beautiful-mermaid",
test: /[\\/](?:beautiful-mermaid|elkjs|entities)(?:@|[\\/])/,
priority: 13,
+ maxSize: TARGET_VENDOR_CHUNK_BYTES,
},
{
name: "ui-primitives",
@@ -358,9 +369,13 @@ export default defineConfig({
{
name: "vendor",
// Keep Mermaid's internally lazy-loaded diagram modules out of the
- // catch-all vendor chunk so they remain on-demand.
+ // catch-all vendor chunk so they remain on-demand. Entry-aware
+ // splitting also prevents dependencies used only by lazy settings,
+ // export, and template screens from leaking into the app entry.
test: /^(?!.*(?:[\\/]mermaid@|node_modules[\\/]mermaid[\\/])).*node_modules[\\/]/,
priority: 5,
+ entriesAware: true,
+ maxSize: TARGET_VENDOR_CHUNK_BYTES,
},
],
},
diff --git a/scripts/verify-web-performance.mjs b/scripts/verify-web-performance.mjs
index bcc52f65..e86e6f93 100644
--- a/scripts/verify-web-performance.mjs
+++ b/scripts/verify-web-performance.mjs
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
-import { readFileSync, statSync } from "node:fs";
+import { readFileSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
const distDirectory = join(process.cwd(), "apps", "web", "dist");
@@ -15,23 +15,55 @@ assert.doesNotMatch(serviceWorker, /NavigationRoute/, "PWA navigation must not a
assert.match(staticHeaders, /\/sw\.js\s+Cache-Control: no-cache, no-store, must-revalidate/, "Service worker updates must not use stale browser cache");
assert.match(staticHeaders, /\/assets\/\*\s+Cache-Control: public, max-age=31536000, immutable/, "Fingerprinted assets must use immutable browser caching");
const precacheStart = serviceWorker.indexOf("precacheAndRoute(");
-const precacheEnd = serviceWorker.indexOf(");", precacheStart);
+const precacheEnd = serviceWorker.indexOf("cleanupOutdatedCaches", precacheStart);
assert.ok(precacheStart >= 0 && precacheEnd > precacheStart, "Web service worker must contain a precache manifest");
const precacheManifest = serviceWorker.slice(precacheStart, precacheEnd);
assert.doesNotMatch(precacheManifest, /\{url:"index\.html",/, "Current HTML must not be served by a cache-first precache route");
assert.match(precacheManifest, /index\.html\?edgeever-offline-shell=/, "PWA must retain a versioned offline HTML shell");
-const optionalDiagramPattern = /(?:vendor-(?:beautiful-mermaid|mermaid)|mermaid\.core|[^"']*Diagram-)[^"']*\.js/;
+const optionalDiagramPattern = /(?:beautiful-mermaid|vendor-mermaid|mermaid\.core|[^"']*Diagram-)[^"']*\.js/;
assert.doesNotMatch(precacheManifest, optionalDiagramPattern, "Optional diagram chunks must remain out of the initial PWA precache");
const entryCount = (precacheManifest.match(/\{url:/g) ?? []).length;
assert.ok(entryCount > 0, "Web service worker precache manifest must not be empty");
const modulePreloads = indexHtml.match(/<link rel="modulepreload"[^>]+>/g)?.join("\n") ?? "";
-const initialOptionalPattern = /vendor-code-highlight|vendor-D3|vendor-(?:beautiful-mermaid|mermaid|tiptap|prosemirror|floating)|ui-primitives|mermaid\.core|[^"']*Diagram-/;
+const initialOptionalPattern = /vendor-code-highlight|vendor-D3|beautiful-mermaid|vendor-(?:mermaid|tiptap|prosemirror|floating)|ui-primitives|mermaid\.core|[^"']*Diagram-/;
assert.doesNotMatch(modulePreloads, initialOptionalPattern, "Optional editor and diagram chunks must remain out of the initial HTML modulepreload list");
const initialModulePreloadBytes = [...indexHtml.matchAll(/<link rel="modulepreload"[^>]+href="([^"]+)"[^>]*>/g)]
.map((match) => statSync(join(distDirectory, match[1].replace(/^\//, ""))).size)
.reduce((total, size) => total + size, 0);
const INITIAL_MODULE_PRELOAD_BUDGET = 700 * 1024;
assert.ok(initialModulePreloadBytes <= INITIAL_MODULE_PRELOAD_BUDGET, `Initial modulepreload budget exceeded: ${initialModulePreloadBytes} > ${INITIAL_MODULE_PRELOAD_BUDGET}`);
-console.log(JSON.stringify({ ok: true, precacheEntries: entryCount, initialModulePreloadBytes, initialModulePreloadBudget: INITIAL_MODULE_PRELOAD_BUDGET, resourceBytesCache: "cache-first", optionalDiagramChunksDeferred: true, optionalInitialChunksDeferred: true }));
+
+const DEFAULT_CHUNK_WARNING_BYTES = 500 * 1024;
+const allowedLargeChunkPattern = /^(?:vendor-(?:beautiful-mermaid|mermaid-(?:layout|render))|.*Diagram-).*\.js$/;
+const largeChunks = readdirSync(join(distDirectory, "assets"))
+ .filter((name) => name.endsWith(".js"))
+ .map((name) => ({ name, size: statSync(join(distDirectory, "assets", name)).size }))
+ .filter(({ size }) => size > DEFAULT_CHUNK_WARNING_BYTES);
+assert.ok(
+ largeChunks.every(({ name }) => allowedLargeChunkPattern.test(name)),
+ `Unexpected JavaScript chunks exceed 500 KiB: ${largeChunks
+ .filter(({ name }) => !allowedLargeChunkPattern.test(name))
+ .map(({ name, size }) => `${name} (${size})`)
+ .join(", ")}`,
+);
+assert.ok(
+ largeChunks.every(({ name }) => !modulePreloads.includes(name)),
+ "Large optional diagram chunks must not be module-preloaded by the app entry",
+);
+assert.ok(
+ largeChunks.every(({ name }) => !precacheManifest.includes(name)),
+ "Large optional diagram chunks must not be included in the PWA precache",
+);
+
+console.log(JSON.stringify({
+ ok: true,
+ precacheEntries: entryCount,
+ initialModulePreloadBytes,
+ initialModulePreloadBudget: INITIAL_MODULE_PRELOAD_BUDGET,
+ largeDeferredChunks: largeChunks,
+ resourceBytesCache: "cache-first",
+ optionalDiagramChunksDeferred: true,
+ optionalInitialChunksDeferred: true,
+}));