Skip to content

Commit 2610a34

Browse files
committed
Fix 1: Lazy load page sections
Using `Suspense` and `React.lazy()`, we isolated entire sections of the page and lazy load them. Note that Suspense is wrapped around each of the lazy components. That ensures separate async threads. Suspense is not wrapping the entire app because that would cause Menu and Hero to wait for the other components to load before rendering. Instead, we focus on rendering Hero and Menu first.
1 parent 31cdfa6 commit 2610a34

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/App.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
// @ts-nocheck
2-
import React from 'react';
3-
import Menu from './components/Menu';
2+
import React, { Suspense, lazy } from 'react';
43
import Hero from './components/Hero';
5-
import Sidebar from './components/Sidebar';
6-
import Products from './components/Products';
7-
import BoringContent from './components/BoringContent';
8-
import Footer from './components/Footer';
4+
import Menu from './components/Menu';
95
import './App.css';
106

7+
const Sidebar = lazy(() => import('./components/Sidebar'));
8+
const Products = lazy(() => import('./components/Products'));
9+
const BoringContent = lazy(() => import('./components/BoringContent'));
10+
const Footer = lazy(() => import('./components/Footer'));
11+
1112
const App = () => (
1213
<>
1314
<Menu />
14-
<Sidebar />
15+
<Suspense fallback={<div />}>
16+
<Sidebar />
17+
</Suspense>
1518
<div id="main">
1619
<Hero />
1720
<main>
18-
<Products />
19-
<BoringContent />
21+
<Suspense fallback={<div />}>
22+
<Products />
23+
</Suspense>
24+
<Suspense fallback={<div />}>
25+
<BoringContent />
26+
</Suspense>
2027
</main>
2128
</div>
22-
<Footer />
29+
<Suspense fallback={<div />}>
30+
<Footer />
31+
</Suspense>
2332
</>
2433
);
2534

0 commit comments

Comments
 (0)