π Next.js 16: Server β Client Communication & Serialization
Once you understand Server Components (SC) and Client Components (CC), the next natural step is passing data between them. However, in Next.js, you cannot simply pass "anything" across the boundary.
Errors like:
- "Functions cannot be passed"
- "Cannot serialize"
Ye sab isi concept se related hain.
π§ 1. The Basic Flow: Server β Client
Jab ek Server Component, Client Component ko render karta hai, wo props ke through data pass karta hai.
How it Works
- Server HTML render karta hai
- Props ko "package" karke browser ko bhejta hai
β οΈ Constraint
Ye data network se travel karta hai β isliye Serializable hona zaroori hai
β Valid Example
// Server Component (app/page.js)
import Counter from "./Counter";
export default async function Page() {
const data = { title: "Next.js Anatomy", version: 16 };
return <Counter data={data} />;
}π₯ 2. What is Serialization? (Core Concept)
Serialization = Data ko ek aise format mein convert karna jo:
- Store ho sake
- Network par bheja ja sake
Usually β JSON format
π§© Parcel Analogy
- Solid items (string, number, array) β safely box mein ja sakte hain
- Liquid/gas (functions) β directly ship nahi ho sakte
JSON = safe delivery box π¦
π« 3. Why Functions Cannot Be Passed
β Error Example
// Server Component
export default function Page() {
const handleClick = () => console.log("Server Logic");
return <Counter onClick={handleClick} />; // β ERROR
}π§ Why?
- Functions = executable code
- JSON = sirf data
Function ko stringify nahi kiya ja sakta (scope, closures lost)
π§© Brain Analogy
- Data = instructions list
- Function = actual brain
Aap instructions bhej sakte ho, brain nahi π§ β
β οΈ 4. NonβSerializable Data Types
A. Date Objects
Problem:
- Serialize hone par string ban jata hai
- Methods lost (e.g.,
.getFullYear())
β Solution
- Server:
date.toISOString() - Client:
new Date(dateString)
B. Map & Set
Problem:
- JSON mein convert nahi hote
- Client par
{}ya broken data milta hai
β Solution
- Map β
Object.fromEntries(map) - Set β
Array.from(set)
π 5. Allowed vs Not Allowed Data
| Data Type | Status | Notes |
|---|---|---|
| String, Number, Boolean | β Allowed | Safe primitives |
| Objects & Arrays | β Allowed | Nested data bhi serializable hona chahiye |
| Functions | β Forbidden | Cannot serialize |
| Class Instances | β Forbidden | Prototype lost ho jata hai |
| Date, Map, Set | β οΈ Risky | Manual conversion required |
π 6. Decoding Real Errors
"Functions cannot be passed"
π Aapne function pass kiya hai Server β Client
"Only plain objects can be passed"
π Complex object (DB instance, class) pass kar diya
"Cannot serialize"
π Unsupported data type pass kiya
π‘ 7. Best Practice: The "Data Only" Rule
Rule:
π Server = Data π Client = Logic
Pattern
// Server Component
export default async function Page() {
const user = await db.user.findUnique({ where: { id: 1 } });
return <ProfileCard user={user} />;
}
// Client Component
"use client";
export default function ProfileCard({ user }) {
const handleFollow = () => {
// Client-side logic
};
return <button onClick={handleFollow}>Follow {user.name}</button>;
}π― Final Mental Model
- Server β Data create karta hai
- Network Boundary β JSON transport layer
- Client β Data receive karke interaction add karta hai
Golden Rule
Agar data
JSON.stringify()nahi ho sakta, toh use props mein pass nahi karna chahiye.
π Next Topic
Next.js fetch Deep Dive β Request Deduplication, Caching, aur Data Revalidation π