[NextJs 14] Summarize data fetching architecture

418 阅读1分钟

Introduction

In this article, I am gonna summarize the modes about data fetching in NextJs for better understanding of NextJs.

If you have not used NextJs 14, please read 《NextJs 14 从入门到精通》

API Router Mode

Tranditionally,we use fetch or axios tool to request data by HTTP-Request.

This mode, meanwhile, is supposed to offer api to outside world in public.

image.png

However, when you use fetch-api in Server Component,that obviously is inapproriate. Because why fecthing api in the same place. In Server Component, we can directly call the function service from server.

image.png

Server Component Call Service Mode

In Server Component, we can call the service function from server, at the same time,remain the api ability in public.

image.png

Client Component Call Service Mode

In Client Component, we can't call the service function from server directly. because, the service does not exist in Client Side. But, we can continue to use api router mode.

image.png

If we want to do this, we should mark service file as "use server"

image.png