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

在 RSC 中获取数据

学习如何直接在服务器组件中高效获取数据,减少客户端 JavaScript 包的体积

第 2 / 4 课10 个步骤

在 RSC 中获取数据 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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!

免费开始

用 AI 导师学习 TypeScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
22
课程
88

常见问题解答

「在 RSC 中获取数据」课时是免费的吗?

是的 — 「在 RSC 中获取数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

「在 RSC 中获取数据」这节课中我会学到什么?

学习如何直接在服务器组件中高效获取数据,减少客户端 JavaScript 包的体积 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack (App Router + Server Actions),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack (App Router + Server Actions) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack (App Router + Server Actions) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「在 RSC 中获取数据」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 理解 RSC 与 RCC
  2. 在 RSC 中获取数据
  3. 使用 RCC 实现交互
  4. 服务器组件与客户端组件的组合模式
← 返回 Next.js 15 Fullstack (App Router + Server Actions)