| 1 | --- |
| 2 | title: Preload Based on User Intent |
| 3 | impact: MEDIUM |
| 4 | impactDescription: reduces perceived latency |
| 5 | tags: bundle, preload, user-intent, hover |
| 6 | --- |
| 7 | |
| 8 | ## Preload Based on User Intent |
| 9 | |
| 10 | Preload heavy bundles before they're needed to reduce perceived latency. |
| 11 | |
| 12 | **Example (preload on hover/focus):** |
| 13 | |
| 14 | ```tsx |
| 15 | function EditorButton({ onClick |
| 16 | const preload = |
| 17 | if (typeof window |
| 18 | void import("./monaco-editor"); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | return ( |
| 23 | <button onMouseEnter={ |
| 24 | Open Editor |
| 25 | </button> |
| 26 | ); |
| 27 | } |
| 28 | ``` |
| 29 | |
| 30 | **Example (preload when feature flag is enabled):** |
| 31 | |
| 32 | ```tsx |
| 33 | function FlagsProvider({ children |
| 34 | useEffect(() => |
| 35 | if (flags.editorEnabled |
| 36 | void import("./monaco-editor"). |
| 37 | } |
| 38 | }, [flags.editorEnabled |
| 39 | |
| 40 | return <FlagsContext.Provider |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | The `typeof window !== 'undefined'` check prevents bundling preloaded modules for SSR, optimizing server bundle size and build speed. |