Next.js 15 Fullstack (App Router + Server Actions) · บทเรียน

การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด

ปรับปรุงประสิทธิภาพที่ผู้ใช้รับรู้ด้วยการสตรีมส่วนติดต่อผู้ใช้ที่เรนเดอร์บนเซิร์ฟเวอร์ด้วย React Suspense ไฟล์ loading.tsx และโครงกระดูก เพื่อให้ผู้ใช้เห็นเนื้อหาเร็วขึ้น

บทเรียน 4 จาก 413 ขั้นตอน

การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด เป็นบทเรียน Next.js 15 Fullstack (App Router + Server Actions) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack (App Router + Server Actions) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack (App Router + Server Actions) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Waterfall Problem

If a page waits for every data fetch before rendering, users stare at a blank screen. Streaming lets the server send ready parts of the page immediately and fill in slow parts as they finish.

What is Streaming SSR?

Next.js with the App Router can stream HTML in chunks. The shell and fast components arrive first; slow data-dependent sections stream in when ready, improving Time to First Byte and perceived speed.

React Suspense Basics

Suspense wraps a component that may not be ready and shows a fallback until it is. The rest of the page renders without waiting.

import { Suspense } from 'react';
<Suspense fallback={<p>Loading...</p>}>
  <SlowComponent />
</Suspense>

Async Server Components

In the App Router, a Server Component can be async and await data. Wrapping it in Suspense lets the page stream while that fetch resolves.

async function Reviews() {
  const data = await getReviews();
  return <ReviewList items={data} />;
}

Streaming a Slow Section

Render the page shell instantly and stream the slow part. Users interact with the rest while reviews load.

export default function Page() {
  return (
    <main>
      <Header />
      <Suspense fallback={<ReviewsSkeleton />}>
        <Reviews />
      </Suspense>
    </main>
  );
}

The loading.tsx Convention

A loading.tsx file beside a route automatically wraps that route's page in Suspense. Its export is shown instantly while the page's data loads.

// app/dashboard/loading.tsx
export default function Loading() {
  return <DashboardSkeleton />;
}

Building a Skeleton

A skeleton mimics the final layout with gray placeholders, reducing layout shift and signaling that content is coming.

function ReviewsSkeleton() {
  return (
    <div className="animate-pulse space-y-2">
      <div className="h-4 bg-gray-200 rounded" />
      <div className="h-4 bg-gray-200 rounded w-3/4" />
    </div>
  );
}

Parallel Data Fetching

Avoid sequential awaits that create waterfalls. Multiple Suspense boundaries let independent sections fetch in parallel and stream as each completes.

<Suspense fallback={<A />}><Sales /></Suspense>
<Suspense fallback={<B />}><Traffic /></Suspense>

Granular Boundaries

Place Suspense around the smallest slow unit, not the whole page. Finer boundaries mean more of the UI is interactive sooner.

Streaming vs Static

Streaming shines for personalized or slow data. For content that rarely changes, static generation or caching is still faster. Combine both: cache what you can, stream the rest.

Best Practices

Stream effectively:

  • Wrap slow async Server Components in Suspense
  • Use loading.tsx for route-level fallbacks
  • Show skeletons to cut layout shift
  • Use parallel boundaries to avoid waterfalls

Quick Check

Test your streaming knowledge.

Recap

You learned to stream UI:

  • Streaming SSR sends ready HTML first and fills slow parts later
  • Wrap async components in Suspense with a fallback
  • Use loading.tsx for automatic route fallbacks
  • Show skeletons and use parallel boundaries

Your pages now feel fast even with slow data.

เริ่มต้นได้ฟรี

เรียนรู้ TypeScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
22
บทเรียน
88

คำถามที่พบบ่อย

บทเรียน “การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack (App Router + Server Actions) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack (App Router + Server Actions) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด”

ปรับปรุงประสิทธิภาพที่ผู้ใช้รับรู้ด้วยการสตรีมส่วนติดต่อผู้ใช้ที่เรนเดอร์บนเซิร์ฟเวอร์ด้วย React Suspense ไฟล์ loading.tsx และโครงกระดูก เพื่อให้ผู้ใช้เห็นเนื้อหาเร็วขึ้น คุณปฏิบัติ Next.js 15 Fullstack (App Router + Server Actions) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack (App Router + Server Actions) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack (App Router + Server Actions) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack (App Router + Server Actions) นี้ได้ไหม

ได้ บทเรียน Next.js 15 Fullstack (App Router + Server Actions) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การปรับแต่งรูปภาพและแบบอักษร
  2. การวิเคราะห์ขนาดชุดรวม
  3. กลยุทธ์การแคชชั้นสูง
  4. การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด
← กลับไปที่ Next.js 15 Fullstack (App Router + Server Actions)