Skip to content

Commit fc7a1b2

Browse files
committed
Optimize lazy-loading
1 parent d763387 commit fc7a1b2

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

src/components/SplitEditor.tsx

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import type * as monaco from "monaco-editor";
2-
import { Component, createRef } from "react";
2+
import { Component, createRef, lazy, Suspense } from "react";
33
import { ErrorBoundary } from "react-error-boundary";
4+
import { BarLoader } from "react-spinners";
45
import SplitPane from "react-split-pane";
56

67
import { getSplitConfig, saveSplitConfig } from "../config.js";
78
import type { SupportedEngine, SupportedFormat } from "../rendering.js";
8-
import Editor from "./Editor.js";
99
import type EditorPane from "./EditorPane.js";
10-
import GraphPane from "./GraphPane.js";
10+
11+
const EditorLazy = lazy(() => import("./Editor.js"));
12+
const GraphPaneLazy = lazy(() => import("./GraphPane.js"));
1113

1214
type ErrorList = monaco.editor.IMarkerData[];
1315

@@ -30,6 +32,14 @@ type ErroredState = {
3032
lastKnownGoodSrc?: string;
3133
};
3234

35+
const loadingStyle = {
36+
position: "absolute",
37+
display: "flex",
38+
inset: "0",
39+
justifyContent: "center",
40+
alignItems: "center",
41+
} as const;
42+
3343
export default class SplitEditor extends Component<
3444
SplitEditorProps,
3545
SplitEditorState
@@ -94,19 +104,35 @@ export default class SplitEditor extends Component<
94104
{...({} as any)}
95105
>
96106
<ErrorBoundary fallback="Could not load editor">
97-
<Editor
98-
initialValue={s.dotSrc}
99-
onChangeValue={() => []}
100-
onValueError={n => void n}
101-
/>
107+
<Suspense
108+
fallback={
109+
<div style={loadingStyle}>
110+
<BarLoader />
111+
</div>
112+
}
113+
>
114+
<EditorLazy
115+
initialValue={s.dotSrc}
116+
onChangeValue={() => []}
117+
onValueError={n => void n}
118+
/>
119+
</Suspense>
102120
</ErrorBoundary>
103121
<ErrorBoundary fallback="Could not load graph preview">
104-
<GraphPane
105-
hasErrors={!!(s.errors && s.errors.length > 0)}
106-
dotSrc={dotSrc}
107-
engine={p.engine}
108-
format={p.format}
109-
/>
122+
<Suspense
123+
fallback={
124+
<div style={loadingStyle}>
125+
<BarLoader />
126+
</div>
127+
}
128+
>
129+
<GraphPaneLazy
130+
hasErrors={!!(s.errors && s.errors.length > 0)}
131+
dotSrc={dotSrc}
132+
engine={p.engine}
133+
format={p.format}
134+
/>
135+
</Suspense>
110136
</ErrorBoundary>
111137
</SplitPane>
112138
);

src/index.tsx

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Component, createRef, lazy, type RefObject, Suspense } from "react";
1+
import { Component, createRef, type RefObject } from "react";
22
import { createRoot } from "react-dom/client";
33

44
import "bootstrap";
55
import "bootstrap/dist/css/bootstrap.min.css";
66

77
import "./index.scss";
88

9-
import { BarLoader } from "react-spinners";
109
import Navigation from "./components/Navigation.js";
10+
import SplitEditor from "./components/SplitEditor.js";
1111
import { getLastState, mergeStates, saveLastEngine } from "./config.js";
1212
import { FileSaver } from "./FileSaver.js";
1313
import { exportAs, type SupportedEngine, saveSource } from "./rendering.js";
@@ -19,8 +19,6 @@ import {
1919
supportedEngines,
2020
} from "./viz";
2121

22-
const LazySplitEditor = lazy(() => import("./components/SplitEditor"));
23-
2422
const defaultEngine = supportedEngines[1];
2523

2624
interface AppState {
@@ -32,12 +30,6 @@ interface AppProps {
3230
}
3331

3432
const defaultSource = tutorial;
35-
const loadingStyle = {
36-
position: "fixed",
37-
top: "50%",
38-
left: "50%",
39-
transform: "translate(-50%, -50%)",
40-
} as const;
4133

4234
class App extends Component<AppProps, AppState> {
4335
#currentSource: string | undefined = undefined;
@@ -113,21 +105,13 @@ class App extends Component<AppProps, AppState> {
113105
loadSample={this.#loadSample}
114106
share={this.#share}
115107
/>
116-
<Suspense
117-
fallback={
118-
<div style={loadingStyle}>
119-
<BarLoader />
120-
</div>
121-
}
122-
>
123-
<LazySplitEditor
124-
ref={this.#editorRef}
125-
initialSource={initialSource}
126-
format="svg"
127-
engine={s.engine}
128-
onSourceChange={this.#sourceChanged}
129-
/>
130-
</Suspense>
108+
<SplitEditor
109+
ref={this.#editorRef}
110+
initialSource={initialSource}
111+
format="svg"
112+
engine={s.engine}
113+
onSourceChange={this.#sourceChanged}
114+
/>
131115
</div>
132116
);
133117
}

0 commit comments

Comments
 (0)