feat: add route-level code splitting with React.lazy and Suspense (Closes #469) - #587
Open
waterWang wants to merge 1 commit into
Open
feat: add route-level code splitting with React.lazy and Suspense (Closes #469)#587waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
…oses Protocol-Guild#469) Replace 20 eager page imports with React.lazy() + custom lazyWithRetry wrapper that clears the chunk-load promise on failure so an ErrorBoundary reset can retry the load. Each route gets a Suspense fallback (RouteLoader) wrapped in an ErrorBoundary whose fallback shows a Try Again button that resets the boundary state. - lazyWithRetry utility: clears cached promise on import() rejection - RouteLoader component: centered spinner with i18n loading text - ErrorBoundary: supports function-form fallback for onReset injection - App.tsx: 18 lazy page imports, LazyRoute helper, reorganized layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #469 — Add route-level code splitting with
React.lazy()andSuspense.All 20 page components were eagerly imported in
App.tsx, so the initial bundle included every route. This change code-splits each page into its own chunk loaded on demand.What changed
src/utils/lazyWithRetry.ts(new): aReact.lazywrapper that caches theimport()promise and clears it on rejection, so a momentary chunk-load failure can be retried on the next render (via the ErrorBoundary reset) instead of permanently poisoning the lazy component.src/components/RouteLoader.tsx(new): centered spinner + i18n "Loading…" text used as the Suspense fallback for every lazy route.src/components/ErrorBoundary.tsx: now supports a function-form fallback (({ onReset }) => …) and an internalresetErrorBoundarythat re-renders children — enabling a working "Try again" button after a chunk-load failure.src/App.tsx: all 18 unique page components are now lazy-loaded vialazyWithRetry; each route element is wrapped in<Suspense fallback={<RouteLoader />}>inside an<ErrorBoundary>. TheEmployerLayoutshell stays eagerly imported so nested-route Suspense never bubbles to the root without a fallback. Route structure is unchanged.Verification
npx tsc --noEmit— passes.vite build— succeeds, and confirms per-route chunks:Each page now loads its own chunk on navigation; the initial
indexbundle no longer includes all 20 pages.Acceptance criteria
RouteLoaderfallback)lazyWithRetryclears the promise; ErrorBoundary function fallback exposes workingonReset→ "Try again")