Skip to content

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

fix(bundle): isolate atomic codemirror chunk and lazy-load markdown editor

tianma-if521af07e2a07
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 4917c63b..c578cac2 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -1,4 +1,4 @@
-import { useRef, useState, useEffect, useCallback, useMemo, type CSSProperties, type FocusEvent as ReactFocusEvent, type MouseEvent as ReactMouseEvent, type ReactNode } from "react";
+import { useRef, useState, useEffect, useCallback, useMemo, lazy, Suspense, type CSSProperties, type FocusEvent as ReactFocusEvent, type MouseEvent as ReactMouseEvent, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { useQuery, useQueryClient, useMutation } from "@tanstack/react-query";
import { useEditor, EditorContent, type Editor } from "@tiptap/react";
@@ -94,7 +94,13 @@ import {
import { WeChatIcon } from "./WeChatIcon";
import { ThemeToggle } from "./ThemeToggle";
import { useEditorTheme, useMarkdownTheme } from "./ThemeProvider";
-import { MarkdownSourceEditor, type MarkdownSourceEditorRef } from "./editor/MarkdownSourceEditor";
+import type { MarkdownSourceEditorRef } from "./editor/MarkdownSourceEditor";
+
+const MarkdownSourceEditor = lazy(() =>
+ import("./editor/MarkdownSourceEditor").then((module) => ({
+ default: module.MarkdownSourceEditor,
+ })),
+);
import { sanitizeAndScopeCss } from "@/lib/css-sandbox";
import { RevisionHistoryDialog } from "./dialogs/RevisionHistoryDialog";
import { ExternalLinkDialog } from "./dialogs/ExternalLinkDialog";
@@ -4748,20 +4754,22 @@ const RichEditorPane = ({
</>
) : useMarkdownSourceEditor ? (
<div className="relative min-h-0 flex-1">
- <MarkdownSourceEditor
- ref={markdownSourceEditorRef}
- value={markdownSource}
- onChange={handleMarkdownSourceChange}
- themeName={markdownTheme}
- readOnly={effectiveReadOnly}
- placeholder={`# ${t("editor.placeholder")}`}
- ariaLabel={t("editor.markdownSourceAria")}
- onSlashCommandTrigger={() => {
- openAiAssistant();
- }}
- onLinkShortcut={openExternalLinkDialog}
- className="absolute inset-0 h-full w-full"
- />
+ <Suspense fallback={<div className="h-full w-full" />}>
+ <MarkdownSourceEditor
+ ref={markdownSourceEditorRef}
+ value={markdownSource}
+ onChange={handleMarkdownSourceChange}
+ themeName={markdownTheme}
+ readOnly={effectiveReadOnly}
+ placeholder={`# ${t("editor.placeholder")}`}
+ ariaLabel={t("editor.markdownSourceAria")}
+ onSlashCommandTrigger={() => {
+ openAiAssistant();
+ }}
+ onLinkShortcut={openExternalLinkDialog}
+ className="absolute inset-0 h-full w-full"
+ />
+ </Suspense>
</div>
) : (
<div
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 7e2db9b5..b773d00a 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -199,6 +199,7 @@ export default defineConfig({
"**/*mermaid.core-*.js",
"**/vendor-mermaid-*.js",
"**/*Diagram-*.js",
+ "**/vendor-codemirror-*.js",
],
navigateFallback: null,
navigationPreload: true,
@@ -234,7 +235,7 @@ export default defineConfig({
},
},
{
- urlPattern: ({ url }) => /\/assets\/(?:.*beautiful-mermaid|vendor-mermaid|.*mermaid\.core|.*Diagram-)/.test(url.pathname),
+ urlPattern: ({ url }) => /\/assets\/(?:.*beautiful-mermaid|vendor-mermaid|.*mermaid\.core|.*Diagram-|vendor-codemirror)/.test(url.pathname),
handler: "CacheFirst",
options: {
cacheName: "edgeever-optional-diagrams",
@@ -281,7 +282,7 @@ export default defineConfig({
? false
: {
resolveDependencies: (_filename, dependencies) => dependencies.filter((dependency) =>
- !/(?:vendor-code-highlight|vendor-(?:mermaid|D3|tiptap|prosemirror|floating)|ui-primitives)/.test(dependency),
+ !/(?:vendor-code-highlight|vendor-(?:mermaid|D3|tiptap|prosemirror|floating|codemirror|zod)|vendor-radix(?!-slot)|ui-primitives|ui-button-tooltip)/.test(dependency),
),
},
rolldownOptions: {
@@ -323,6 +324,11 @@ export default defineConfig({
test: /node_modules[\\/](prosemirror-|orderedmap|rope-sequence)[\\/]/,
priority: 38,
},
+ {
+ name: "vendor-codemirror",
+ test: /[\\/]node_modules[\\/](?:@codemirror|@lezer|@uiw[\\/](?:react-)?codemirror|@uiw[\\/]codemirror-themes|codemirror)[\\/]/,
+ priority: 37,
+ },
{
name: "vendor-tiptap-pm",
test: /node_modules[\\/]@tiptap[\\/]pm[\\/]/,
@@ -340,7 +346,7 @@ export default defineConfig({
},
{
name: "vendor-tiptap-extensions",
- test: /node_modules[\\/]@tiptap[\\/](extension-|extensions)[\\/]/,
+ test: /node_modules[\\/](?:@tiptap[\\/](?:extension-|extensions)|tiptap-)[\\/]/,
priority: 30,
},
{
@@ -363,6 +369,16 @@ export default defineConfig({
test: /node_modules[\\/]@tanstack[\\/]react-query[\\/]/,
priority: 25,
},
+ {
+ name: "vendor-zod",
+ test: /node_modules[\\/]zod[\\/]/,
+ priority: 22,
+ },
+ {
+ name: "vendor-i18n",
+ test: /node_modules[\\/](i18next|react-i18next)[\\/]/,
+ priority: 21,
+ },
{
name: "vendor-storage",
test: /node_modules[\\/](dexie|workbox-window)[\\/]/,
diff --git a/scripts/verify-web-performance.mjs b/scripts/verify-web-performance.mjs
index aadc5acb..42f6dc74 100644
--- a/scripts/verify-web-performance.mjs
+++ b/scripts/verify-web-performance.mjs
@@ -21,7 +21,7 @@ assert.ok(precacheStart >= 0 && precacheEnd > precacheStart, "Web service worker
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 = /(?:beautiful-mermaid|vendor-mermaid|mermaid\.core|[^"']*Diagram-)[^"']*\.js/;
+const optionalDiagramPattern = /(?:beautiful-mermaid|vendor-mermaid|mermaid\.core|vendor-codemirror|[^"']*Diagram-)[^"']*\.js/;
assert.doesNotMatch(precacheManifest, optionalDiagramPattern, "Optional diagram chunks must remain out of the initial PWA precache");
assert.doesNotMatch(precacheManifest, /noto-sans-sc-[^"']*\.woff2/, "Print-only Noto Sans SC shards must remain out of the PWA precache");
@@ -35,7 +35,7 @@ const precacheBytes = precacheURLs
const PRECACHE_BUDGET = 5 * 1024 * 1024;
assert.ok(precacheBytes <= PRECACHE_BUDGET, `PWA precache budget exceeded: ${precacheBytes} > ${PRECACHE_BUDGET}`);
const modulePreloads = indexHtml.match(/<link rel="modulepreload"[^>]+>/g)?.join("\n") ?? "";
-const initialOptionalPattern = /vendor-code-highlight|vendor-D3|beautiful-mermaid|vendor-(?:mermaid|tiptap|prosemirror|floating)|ui-primitives|mermaid\.core|[^"']*Diagram-/;
+const initialOptionalPattern = /vendor-code-highlight|vendor-D3|beautiful-mermaid|vendor-(?:mermaid|tiptap|prosemirror|floating|codemirror)|ui-primitives|mermaid\.core|[^"']*Diagram-/;
assert.doesNotMatch(modulePreloads, initialOptionalPattern, "Optional editor and diagram chunks must remain out of the initial HTML modulepreload list");
assert.doesNotMatch(modulePreloads, /ui-button-tooltip/, "Button tooltips must load only when a titled button is rendered");
assert.doesNotMatch(modulePreloads, /vendor-radix(?!-slot)/, "Radix overlays must remain out of the initial HTML modulepreload list");
@@ -46,7 +46,7 @@ const INITIAL_MODULE_PRELOAD_BUDGET = 700 * 1024;
assert.ok(initialModulePreloadBytes <= INITIAL_MODULE_PRELOAD_BUDGET, `Initial modulepreload budget exceeded: ${initialModulePreloadBytes} > ${INITIAL_MODULE_PRELOAD_BUDGET}`);
const DEFAULT_CHUNK_WARNING_BYTES = 500 * 1024;
-const allowedLargeChunkPattern = /^(?:vendor-(?:code-highlight|beautiful-mermaid|mermaid-(?:layout|render))|.*Diagram-).*\.js$/;
+const allowedLargeChunkPattern = /^(?:vendor-(?:code-highlight|beautiful-mermaid|mermaid-(?:layout|render)|codemirror)|.*Diagram-).*\.js$/;
const largeChunks = readdirSync(join(distDirectory, "assets"))
.filter((name) => name.endsWith(".js"))
.map((name) => ({ name, size: statSync(join(distDirectory, "assets", name)).size }))