-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming-and-suspense.js
More file actions
72 lines (29 loc) · 1.43 KB
/
streaming-and-suspense.js
File metadata and controls
72 lines (29 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// File: streaming-and-suspense.js
/* 📘 Topic: Streaming and React Suspense in Next.js App Router
Streaming allows sending parts of the page as they’re ready, improving TTFB and user experience. React Suspense enables showing fallback UI while waiting for data or components. */
/**
🧪 Streaming Example using Server Components with Suspense
File: /app/page.js */
import { Suspense } from 'react'; import UserProfile from './UserProfile';
export default function HomePage() { return ( <div> <h1>Streaming Demo</h1> <Suspense fallback={<p>Loading user profile...</p>}> <UserProfile /> </Suspense> </div> ); }
/**
💡 UserProfile is a server component simulating slow fetch
File: /app/UserProfile.js */
async function fetchUser() { await new Promise((r) => setTimeout(r, 3000)); return { name: 'Jeevan', role: 'Developer' }; }
export default async function UserProfile() { const user = await fetchUser(); return <p>👤 {user.name} - {user.role}</p>; }
/**
✅ Benefits:
React streams content to browser as it’s ready
Faster perceived load time
Works natively in App Router
❗ Suspense works only for Server Components or async Client Components (with use hook or lazy imports). */
/**
🧠 Real-world usage:
Dashboards
Reports with large datasets
E-commerce product details + sidebar */
/**
Summary:
Use <Suspense> to stream async content
Works well with nested layouts
Keep fallbacks light and contextual */