Repository navigation
perf(router): implement route-level code splitting and improve color … #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Development mode (npm run dev - port 5173) | ||
| # Requests are routed directly to the Spring Cloud API Gateway on port 8070 | ||
| VITE_API_URL=http://localhost:8070 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Production mode (npm run build) | ||
| # VITE_API_URL is empty so relative URLs (/v1/api/...) are used, routed via reverse proxy/ingress | ||
| VITE_API_URL= |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,179 @@ | ||
| import { Component, lazy, Suspense, type ErrorInfo, type ReactNode } from 'react' | ||
| import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom' | ||
| import { GuestRoute, ProtectedRoute } from './guards' | ||
| import { ROUTES } from './routes' | ||
| import { AppLayout } from '../layouts/AppLayout' | ||
| import { AuthLayout } from '../layouts/AuthLayout' | ||
| import { DashboardPage } from '../pages/DashboardPage' | ||
| import { ForgotPasswordPage } from '../pages/ForgotPasswordPage' | ||
| import { LoginPage } from '../pages/LoginPage' | ||
| import { MfaVerifyPage } from '../pages/MfaVerifyPage' | ||
| import { ResetPasswordPage } from '../pages/ResetPasswordPage' | ||
| import { VerifyEmailPage } from '../pages/VerifyEmailPage' | ||
| import { Button } from '../components/ui' | ||
|
|
||
| const LoginPage = lazy(() => | ||
| import('../pages/LoginPage').then((m) => ({ default: m.LoginPage })) | ||
| ) | ||
| const MfaVerifyPage = lazy(() => | ||
| import('../pages/MfaVerifyPage').then((m) => ({ default: m.MfaVerifyPage })) | ||
| ) | ||
| const ForgotPasswordPage = lazy(() => | ||
| import('../pages/ForgotPasswordPage').then((m) => ({ default: m.ForgotPasswordPage })) | ||
| ) | ||
| const ResetPasswordPage = lazy(() => | ||
| import('../pages/ResetPasswordPage').then((m) => ({ default: m.ResetPasswordPage })) | ||
| ) | ||
| const VerifyEmailPage = lazy(() => | ||
| import('../pages/VerifyEmailPage').then((m) => ({ default: m.VerifyEmailPage })) | ||
| ) | ||
| const DashboardPage = lazy(() => | ||
| import('../pages/DashboardPage').then((m) => ({ default: m.DashboardPage })) | ||
| ) | ||
|
|
||
| function PageLoading() { | ||
| return ( | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| minHeight: '240px', | ||
| width: '100%', | ||
| }} | ||
| role="status" | ||
| aria-label="Loading page" | ||
| > | ||
| <div | ||
| style={{ | ||
| width: '28px', | ||
| height: '28px', | ||
| border: '3px solid var(--color-border, #e2e8f0)', | ||
| borderTopColor: 'var(--color-primary, #0f172a)', | ||
| borderRadius: '50%', | ||
| animation: 'spin 600ms linear infinite', | ||
| }} | ||
| /> | ||
| <style>{`@keyframes spin { to { transform: rotate(360deg); } }`}</style> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| interface RouteErrorBoundaryProps { | ||
| children: ReactNode | ||
| } | ||
|
|
||
| interface RouteErrorBoundaryState { | ||
| hasError: boolean | ||
| error: Error | null | ||
| } | ||
|
|
||
| export class RouteErrorBoundary extends Component<RouteErrorBoundaryProps, RouteErrorBoundaryState> { | ||
| override state: RouteErrorBoundaryState = { | ||
| hasError: false, | ||
| error: null, | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error: Error): RouteErrorBoundaryState { | ||
| return { hasError: true, error } | ||
| } | ||
|
|
||
| override componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
| console.error('Route chunk load failure:', error, errorInfo) | ||
| } | ||
|
|
||
| handleReload = () => { | ||
| window.location.reload() | ||
| } | ||
|
|
||
| handleRetry = () => { | ||
| this.setState({ hasError: false, error: null }) | ||
| } | ||
|
|
||
| override render() { | ||
| if (this.state.hasError) { | ||
| return ( | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| minHeight: '60vh', | ||
| padding: '2rem', | ||
| textAlign: 'center', | ||
| }} | ||
| role="alert" | ||
| > | ||
| <div | ||
| style={{ | ||
| maxWidth: '420px', | ||
| width: '100%', | ||
| padding: '2rem', | ||
| borderRadius: '0.75rem', | ||
| border: '1px solid var(--color-border, #e2e8f0)', | ||
| backgroundColor: 'var(--color-surface, #ffffff)', | ||
| boxShadow: '0 4px 6px -1px rgb(0 0 0 / 0.1)', | ||
| }} | ||
| > | ||
| <h2 | ||
| style={{ | ||
| fontSize: '1.25rem', | ||
| fontWeight: 600, | ||
| color: 'var(--color-text-primary, #0f172a)', | ||
| marginBottom: '0.75rem', | ||
| }} | ||
| > | ||
| Failed to load page | ||
| </h2> | ||
| <p | ||
| style={{ | ||
| fontSize: '0.875rem', | ||
| color: 'var(--color-text-secondary, #64748b)', | ||
| marginBottom: '1.5rem', | ||
| lineHeight: 1.5, | ||
| }} | ||
| > | ||
| A network error occurred while loading this page, or a new version of the app is available. | ||
| </p> | ||
| <div style={{ display: 'flex', gap: '0.75rem', justifyContent: 'center' }}> | ||
| <Button variant="outline" size="sm" onClick={this.handleRetry}> | ||
| Try again | ||
| </Button> | ||
| <Button variant="primary" size="sm" onClick={this.handleReload}> | ||
| Reload application | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| return this.props.children | ||
| } | ||
| } | ||
|
|
||
| export function AppRouter() { | ||
| return ( | ||
| <BrowserRouter> | ||
| <Routes> | ||
| <Route element={<GuestRoute />}> | ||
| <Route element={<AuthLayout />}> | ||
| <Route path={ROUTES.LOGIN} element={<LoginPage />} /> | ||
| <Route path={ROUTES.MFA_VERIFY} element={<MfaVerifyPage />} /> | ||
| <Route path={ROUTES.FORGOT_PASSWORD} element={<ForgotPasswordPage />} /> | ||
| <Route path={ROUTES.RESET_PASSWORD} element={<ResetPasswordPage />} /> | ||
| <Route path={ROUTES.VERIFY_EMAIL} element={<VerifyEmailPage />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| <Route element={<ProtectedRoute />}> | ||
| <Route element={<AppLayout />}> | ||
| <Route path={ROUTES.DASHBOARD} element={<DashboardPage />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| <Route path="*" element={<Navigate to={ROUTES.DASHBOARD} replace />} /> | ||
| </Routes> | ||
| <RouteErrorBoundary> | ||
| <Suspense fallback={<PageLoading />}> | ||
| <Routes> | ||
| <Route element={<GuestRoute />}> | ||
| <Route element={<AuthLayout />}> | ||
| <Route path={ROUTES.LOGIN} element={<LoginPage />} /> | ||
| <Route path={ROUTES.MFA_VERIFY} element={<MfaVerifyPage />} /> | ||
| <Route path={ROUTES.FORGOT_PASSWORD} element={<ForgotPasswordPage />} /> | ||
| <Route path={ROUTES.RESET_PASSWORD} element={<ResetPasswordPage />} /> | ||
| <Route path={ROUTES.VERIFY_EMAIL} element={<VerifyEmailPage />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| <Route element={<ProtectedRoute />}> | ||
| <Route element={<AppLayout />}> | ||
| <Route path={ROUTES.DASHBOARD} element={<DashboardPage />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| <Route path="*" element={<Navigate to={ROUTES.DASHBOARD} replace />} /> | ||
| </Routes> | ||
| </Suspense> | ||
| </RouteErrorBoundary> | ||
| </BrowserRouter> | ||
| ) | ||
| } | ||
|
|
||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '1,190p' src/app/router.tsx cat package.jsonRepository: Priyansusahoo/modelX
Length of output: 6227
🏁 Script executed:
Repository: Priyansusahoo/modelX
Length of output: 13891
🌐 Web query:
site:react.dev/reference/react/lazy React lazy promise reject cached promise load called more than once💡 Result:
Remove the non-retrying “Try again” action.
handleRetryonly clears the boundary state. The route re-renders the same module-scopeReact.lazycomponent, whose rejected loader promise React caches and throws again. KeepReload applicationas the recovery action.Suggested fix
🤖 Prompt for AI Agents