1. Request Waterfall

image.png

Request Waterfall데이터 요청이 순차적으로 처리되며 발생하는 지연을 의미합니다.

예시:

async function fetchData() {
  const user = await fetchUser();
  const posts = await fetchPosts(user.id);
  return { user, posts };
}

위 코드에서는 fetchUser가 끝난 뒤에야 fetchPosts가 실행되므로 비효율적입니다.

2. Static Rendering

Static Rendering빌드 시 데이터를 미리 가져와 정적인 HTML 파일로 생성하는 방식입니다.

Next.js의 두 가지 Static Rendering 방식:

  1. Static Site Generation (SSG):

  2. Incremental Static Regeneration (ISR):

3. Parallel Data Fetching

Parallel Data Fetching은 독립적인 데이터 요청을 병렬로 처리하여 로딩 시간을 최적화하는 방법입니다. Promise.all() 또는 Promise.allSettled()를 사용해 동시에 모든 promise를 실행할 수 있습니다.

Next.js에서 병렬 데이터 페칭 구현 예시:

export default async function Page() {
  const [users, posts] = await Promise.all([
    fetch('<https://api.example.com/users>').then((res) => res.json()),
    fetch('<https://api.example.com/posts>').then((res) => res.json()),
  ]);

  return (
    <div>
      <h1>Users</h1>
      <ul>{users.map((user) => <li key={user.id}>{user.name}</li>)}</ul>
      <h1>Posts</h1>
      <ul>{posts.map((post) => <li key={post.id}>{post.title}</li>)}</ul>
    </div>
  );
}