Next.js 15 Fullstack (App Router + Server Actions) · Lekcja

Pobieranie danych w RSC

Dowiedz się, jak wydajnie pobierać dane bezpośrednio w komponentach serwerowych, aby zmniejszyć pakiety JavaScript po stronie klienta

Lekcja 2 z 410 kroki

Pobieranie danych w RSC to bezpłatna lekcja Next.js 15 Fullstack (App Router + Server Actions) na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Next.js 15 Fullstack (App Router + Server Actions), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Next.js 15 Fullstack (App Router + Server Actions) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Why Fetch Data on the Server?

Welcome to data fetching in Next.js Server Components! Traditionally, data fetching often happened on the client-side, leading to heavier JavaScript bundles.

Server Components (RSCs) change this by allowing you to fetch data directly on the server, before any JavaScript is sent to the browser.

How Server Components Fetch Data

In a Server Component, you can use standard JavaScript async/await syntax with the native fetch API to get data.

  • No need for client-side hooks like useEffect.
  • Data fetching runs entirely on the server.
  • The fetched data is rendered into HTML.

A Basic Data Fetch Scenario

Imagine fetching a list of products or user profiles. In a Next.js Server Component, your component function can be marked as async, allowing you to await the result of a fetch call.

This means your component receives the data before it's even sent to the browser for rendering.

Runnable Fetch Example

Here's a simplified Node.js example demonstrating how fetch works with async/await, similar to how it would operate within a Next.js Server Component.

Run it to see how data is retrieved from an external API.

async function fetchData() {
  try {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    console.log("Fetched data:", data);
  } catch (error) {
    console.error("Error fetching data:", error);
  }
}

// In a Next.js Server Component, this would be called
// directly inside your async component function.
fetchData();

The Role of `await` in RSCs

When you await a fetch call in an RSC, the server pauses rendering that part of the component until the data arrives. Once the data is ready, the component continues rendering.

This ensures the initial HTML sent to the client already contains all the necessary data, improving perceived load times.

Keeping Server-Side Secrets

One major advantage of fetching data in Server Components is that server-only logic, like database queries or API keys, never leaves the server.

This significantly enhances security, as sensitive information is not exposed to the client's browser.

Leaner Client Bundles

By fetching data on the server, the client doesn't need to download extra JavaScript bundles for data fetching libraries or complex data hydration logic.

This results in:

  • Faster initial page loads.
  • Less JavaScript for the browser to parse and execute.
  • Improved performance, especially on slower networks.

Common Data Sources for RSCs

Server Components can fetch data from various sources:

  • External APIs: Using fetch for REST or GraphQL endpoints.
  • Databases: Directly querying a database (e.g., with Prisma client).
  • File System: Reading local files (e.g., Markdown content).

The key is that these operations happen on the server.

Check Your Understanding

Which of the following is a primary benefit of fetching data directly within Next.js Server Components?

Data Fetching in RSCs Recap

In this lesson, we explored how to fetch data efficiently using Next.js Server Components.

  • RSCs use async/await and fetch to get data on the server.
  • This keeps sensitive logic server-side and reduces client-side JavaScript.
  • The result is faster initial page loads and improved security.

Next, we'll look at interactivity with Client Components!

Bezpłatny start

Ucz się TypeScript dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
22
Lekcje
88

Często zadawane pytania

Czy lekcja „Pobieranie danych w RSC” jest bezpłatna?

Tak — pełny tekst „Pobieranie danych w RSC” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Next.js 15 Fullstack (App Router + Server Actions), przejdź na CoddyKit PRO. Kurs Next.js 15 Fullstack (App Router + Server Actions) zawiera 4 lekcji w sumie.

Co nauczysz się w „Pobieranie danych w RSC”?

Dowiedz się, jak wydajnie pobierać dane bezpośrednio w komponentach serwerowych, aby zmniejszyć pakiety JavaScript po stronie klienta Ćwiczysz Next.js 15 Fullstack (App Router + Server Actions) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Next.js 15 Fullstack (App Router + Server Actions)?

Nie wymagamy żadnego doświadczenia. Next.js 15 Fullstack (App Router + Server Actions) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Pobieranie danych w RSC”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Next.js 15 Fullstack (App Router + Server Actions)?

Tak. Każda lekcja Next.js 15 Fullstack (App Router + Server Actions) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. RSC i RCC — wyjaśnienie
  2. Pobieranie danych w RSC
  3. Interaktywność z RCC
  4. Wzorce kompozycji komponentów Server i Client
← Powrót do Next.js 15 Fullstack (App Router + Server Actions)