0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · درس

جلب البيانات من جانب الخادم

نفّذ جلبًا متينًا للبيانات من جانب الخادم باستخدام `fetch` وطرائق أخرى داخل Server Components لتحقيق أفضل أداء

جلب البيانات من جانب الخادم درس مجاني في Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit. هذا هو الدرس 1 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Next.js 15 Fullstack (App Router + Server Actions)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Next.js 15 Fullstack (App Router + Server Actions) 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Fetch Data on the Server?

Next.js 15 excels at rendering your application on the server. This is super powerful for performance and SEO!

When you fetch data directly on the server, your users see content faster because the HTML is already built with data. It also helps search engines index your content better.

  • Performance: Faster initial page loads.
  • SEO: Content is available for crawlers.
  • Security: Keep sensitive logic on the server.

Enhanced `fetch` for Server Components

In Next.js 15 Server Components, the standard fetch Web API is automatically enhanced. This means it comes with built-in caching and revalidation features.

You can use fetch directly inside your async Server Components to get data from external APIs or your own backend services.

Fetching Data in a Component

Let's see how simple it is to fetch data. Here, we'll get a single "Todo" item from a public API. Notice the async keyword on the component function.

Next.js handles the waiting for data before rendering the component.

Run This Basic Fetch Example

This JavaScript code demonstrates fetching data from a public API. While a Next.js Server Component uses JSX, the underlying fetch logic is the same. Run it to see the data!

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

fetchTodo();

Automatic Caching with `fetch`

By default, Next.js automatically caches the results of fetch requests in Server Components. If the same fetch request is made again (e.g., across multiple components or re-renders), Next.js will use the cached data.

This is fantastic for performance as it avoids redundant network requests.

Opting Out of Caching

Sometimes, you need the freshest data, bypassing the cache. You can do this by passing an option to fetch: { cache: 'no-store' }. This tells Next.js to always refetch the data.

Use 'no-store' for highly dynamic data that changes frequently and needs to be up-to-date on every request.

async function fetchFreshData() {
  const url = 'https://jsonplaceholder.typicode.com/posts/1';
  // 'no-store' ensures data is always fresh
  const response = await fetch(url, { cache: 'no-store' });
  const data = await response.json();
  console.log("Fresh Post Title:", data.title);
}

fetchFreshData();

Revalidating Data on Demand

What if you want cached data, but also want to specify how often it should be considered "fresh"? You can use the next.revalidate option with fetch.

This sets a time-to-live (TTL) for the data. After this time, the next request will refetch the data and update the cache.

async function fetchRevalidatedData() {
  const url = 'https://jsonplaceholder.typicode.com/users/1';
  // Revalidate data every 60 seconds
  const response = await fetch(url, { next: { revalidate: 60 } });
  const data = await response.json();
  console.log("User Name (revalidated):", data.name);
}

fetchRevalidatedData();

`revalidate` vs. `no-store`

Choosing between cache: 'no-store' and next: { revalidate: N } depends on your data's needs:

  • no-store: Use for data that must be absolutely fresh on every request (e.g., real-time stock prices, user-specific shopping cart).
  • revalidate: N: Use for data that can be slightly stale but should update periodically (e.g., blog posts, product listings that change hourly).
  • Default (cached): Use for static or infrequently changing data (e.g., navigation links, static page content).

Beyond `fetch`: Direct Database Access

While fetch is powerful for external APIs, Server Components can also directly interact with databases or ORMs like Prisma. Since Server Components run on the server, they can safely contain database credentials.

This means you can write code like await prisma.user.findMany() directly in your components, keeping your client bundle small and secure.

Data Fetching Options

Which of the following statements about server-side data fetching in Next.js 15 are TRUE?

Recap: Server-Side Fetching

You've learned how to harness server-side data fetching in Next.js 15!

  • fetch in Server Components is automatically optimized with caching.
  • Use cache: 'no-store' for always-fresh data.
  • Use next: { revalidate: N } for time-based data freshness.
  • Server Components can also directly access databases securely.

Mastering these techniques is key to building fast, efficient, and secure Next.js applications.

الأسئلة الشائعة

هل درس «جلب البيانات من جانب الخادم» مجاني؟

نعم — نص درس «جلب البيانات من جانب الخادم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Next.js 15 Fullstack (App Router + Server Actions)، انتقل إلى CoddyKit PRO. تتضمن دورة Next.js 15 Fullstack (App Router + Server Actions) 3 دروس في المجموع.

ماذا ستتعلم في «جلب البيانات من جانب الخادم»؟

نفّذ جلبًا متينًا للبيانات من جانب الخادم باستخدام `fetch` وطرائق أخرى داخل Server Components لتحقيق أفضل أداء تتمرن على Next.js 15 Fullstack (App Router + Server Actions) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Next.js 15 Fullstack (App Router + Server Actions)؟

لا تُشترط خبرة سابقة. Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 3.

كم من الوقت يستغرق درس «جلب البيانات من جانب الخادم»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Next.js 15 Fullstack (App Router + Server Actions) هذا؟

نعم. كل درس في Next.js 15 Fullstack (App Router + Server Actions) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. جلب البيانات من جانب الخادم
  2. التخزين المؤقت وإعادة التحقق
  3. جلب البيانات بالتوازي والتتابع والبث
← العودة إلى Next.js 15 Fullstack (App Router + Server Actions)