AppDir
next.js์์ layout๊ณผ routing ๊ฒฝํ์ ๊ฐ์ ์ํค๊ณ React ์ต์ ๊ธฐ์ ์ ์ง์ํ๊ธฐ์ํด, next.js 13๋ถํฐ ์ง์ํ๋ ๋๋ ํฐ๋ฆฌ ๋ฐฉ์. ๊ธฐ์กด์ pages ๋๋ ํฐ๋ฆฌ๋ฅผ ๋์ฒดํ๋ค.
23๋ 04์ ๊ธฐ์ค์ผ๋ก ์์ง์ ๋ฒ ํ๋ฒ์ ์ผ๋ก ํ๋ก๋์ ์์ ์ฌ์ฉํ๋ ๊ฑด ์ถ์ฒํ์ง ์๋๋ค๊ณ ํ๋ค. ํ์ง๋ง, ์ข์ ๊ธฐ๋ฅ๋ค์ด ๋ง๊ธฐ์ ํ์ตํด๋ด๋ ๊ด์ฐฎ์ ๊ฑฐ ๊ฐ๋ค.
file conventions
app directory์์ ํ์ผ ์ปจ๋ฒค์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- page.js : ํด๋น path์ UI๋ฅผ ๋ด๋นํ๋ฉฐ, publicํ๊ฒ ์ ๊ทผํ ์ ์๋๋ก ํจ.
- route.js : ์๋ฒ ์ฌ์ด๋ API endpoint
- layout.js : route segment ๊ฐ์ ๊ณต์ ๊ฐ๋ฅํ UI. ์์ layout, page๋ฅผ ๊ฐ์
- template.js : layout ์๋์์ ์์ layout, page๋ฅผ ๊ฐ์.
- loading.js : ํ์ด์ง๋ฅผ ๋ก๋ฉ ์ค์ผ ๋ ๋ณด์ฌ์ฃผ๋ UI. ํ์ด์ง๋ child segment๋ฅผ React Suspense๋ก ๊ฐ์ผ๋ค.
- error.js : page๋ child segment์์ ์๋ฌ๊ฐ ๋ฐ์ํ์ ๊ฒฝ์ฐ ๋ณด์ฌ์ค. React Error Boundary ์ฌ์ฉ.
- global-error.js : root layout์์ ์๋ฌ๋ฅผ ์ก์์ ๋ ๋ณด์ฌ์ฃผ๋ UI
- not-found.js : ํด๋น route segment ์๋์์ ๋งค์น๋๋ route๊ฐ ์์ ๋ ๋ณด์ฌ์ฃผ๋ UI
Layout
next.js 13๋ถํฐ๋ path๊ฐ ๋ฐ๋๋๋ผ๋ ๋ฆฌ๋ ๋๋ง ์์ด ์ด์ ๋ ์ด์์์ ์ ์งํ ์ ์๋ค. ์ด๋ ์ํ๋ ์ ์ง๋๋ค.
์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
- app directory๋ฅผ ์ฌ์ฉํ๋ค.
- ํน์ ํ์ด์ง ์๋์์ ์ด๋ค ํ์ผ์
layout.js๋ก ์์ฑํ๋ฉด, ๊ทธ๊ฒ์ด ๋ ์ด์์ ์ปดํฌ๋ํธ์ด๋ค.
// app/dashboard/layout.ts
export default function DashboardLayout({
children, // will be a page or nested layout
}: {
children: React.ReactNode,
}) {
return (
<section>
{/* Include shared UI here e.g. a header or sidebar */}
<nav></nav>
{children}
</section>
);
}์ฐธ๊ณ
- ๋ชจ๋ route segment๋ ์์ ๋ง์ layout์ ๊ฐ์ง ์ ์๋ค. ์ด layout์ ๊ทธ segment ์๋์ ๋ชจ๋ ํ์ด์ง์ ์ ์ฉ๋๋ค.
- layout์ ๋ํดํธ๋ก nested๋๋ค. ๊ฐ ๋ถ๋ชจ layout์ ์์ layout์ ๊ฐ์ผ๋ค.
- Route Groups๋ฅผ ์ง์ ํ์ฌ ํน์ ํ route segment์๋ง layout์ ๊ณต์ ํ ์๋ ์๋ค.
- Layout์ ๋ํดํธ๋ก Server Component์ด๋ค. ๋ค๋ง Client Component๋ก ์ค์ ํ ์ ์๋ค.
- ๋ถ๋ชจ ์์ layout ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋๊ฑด ๋ถ๊ฐ๋ฅํ๋ค. ํ์ง๋ง, ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ fetch ํ๋ฉด ๋ฆฌ์กํธ์์ ์๋์ผ๋ก ์์ฒญ์ ์ค๋ณต์ ๊ฑฐํ๋ค.
layout.js์page.js๋ฅผ ๊ฐ์ ํด๋์ ์์ฑํ ์ ์๋ค.layout.js๊ฐpage.js๋ฅผ ๊ฐ์ผ๋ค.
Root Layout (Required)
app directory์ ์ต์๋จ์ ๋ฐ๋์ ์์ด์ผํจ. ์ด root layout์ ํตํด ํด๋ผ์ด์ธํธ์์ ์ต์ด์ ๋ฐ๋ HTML์ ์์ ํ ์ ์๋ค. (pages ๋๋ ํฐ๋ฆฌ์์์ _document.js ๊ฐ์ ๋๋)
์ฐธ๊ณ
<html>,<body>๋ฅผ ๋ฐ๋์ ๊ฐ์ง๊ณ ์์ด์ผํ๋ค.<head>๋ฅผ ์์ ํ์ฌ SEO๋ฅผ ์ง์ํ ์ ์์.- Route Group์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ root layout์ ๊ฐ์ง ์ ์์.
- root layout์ ์๋ฒ์ปดํฌ๋ํธ์ด๋ฉฐ, ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ก ์ค์ ํ ์ ์๋ค.
template
layout๊ณผ ๊ฐ์ด child layout, page๋ฅผ ๊ฐ์ธ์ง๋ง, routing์ ๊ฑฐ์ณ ์ํ๊ฐ ์ ์ง๋์ง์๋๋ค. path๊ฐ ๋ณ๊ฒฝ๋๋ฉด ๋ฆฌ๋ ๋๋ง๋๋ค. ์ฌ์ฉํ๋ ค๋ฉด, route segment์ template.js๋ก ์์ฑํ๋ฉด ๋๋ค. ๊ทธ๋ผ ๋ค์๊ณผ ๊ฐ์ด layout ์๋์์ ์์ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ๋๋ก ์ ์ฉ๋๋ค.
<Layout>
{/* Note that the template is given a unique key. */}
<Template key={routeParam}>{children}</Template>
</Layout>Server component
app directory๋ฅผ ์ฌ์ฉํ๋ฉด, ์๋ฒ์ปดํฌ๋ํธ๋ฅผ ๊ฐํธํ๊ฒ ์ฌ์ฉํ ์ ์๋ค. ์๋ฒ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฉด, ํด๋ผ์ด์ธํธ์๊ฒ ์ต์ด๋ก ๋ณด๋ด์ง๋ JS ์ฝ๋๋ฅผ ์ค์ผ ์ ์์ด ๋น ๋ฅธ ํ์ด์ง๋ก๋ฉ์ ๋์์ ์ค๋ค.
app directory ์๋์ ์๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฒ์ปดํฌ๋ํธ์ด๋ค. ๋ฐ๋ผ์ ์๋ฒ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋ณ๋ค๋ฅธ ์์ ์ด ํ์์๋ค.
route๊ฐ next.js์์ ๋ก๋ฉ๋๋ฉด, ์ต์ด์ HTML์ ์๋ฒ์์ ๋ ๋๋ง ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ HTML๋ ์ ์ง์ ์ผ๋ก ๋ธ๋ผ์ฐ์ ์ ์ ๋ฌ๋๋ค. ์ด๋ next.js์ react client-side runtime์ ๋น๋๊ธฐ์ ์ผ๋ก ๋ก๋ฉํ๋ค. ์ด๋ก์จ ํด๋ผ์ด์ธํธ๊ฐ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ค๋ฃจ๊ณ , ์ธํฐ๋์ ํ ์ ์๊ฒ ๋๋ค.
์๋ฒ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฉด ์ต์ด ํ์ด์ง ๋ก๋ฉ์ ๋นจ๋ผ์ง๊ณ , client-side runtime์ ์บ์๋ ์ ์์ผ๋ฉฐ ์ฌ์ด์ฆ๋ฅผ ์์ธกํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ์ต์ด๋ก ๋ด๋ ค์ง๋ JS ๋ฒ๋ค ์ฌ์ด์ฆ๋ ์ดํ๋ฆฌ์ผ์ด์ ํฌ๊ธฐ๊ฐ ์ปค์ง๋๋งํผ ๋ฐ๋ผ ์ปค์ง์ง ์๋๋ค. ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ฅผ ํตํด ์ดํ๋ฆฌ์ผ์ด์ ์ ์ถ๊ฐ๋ client-side interactivity ๋งํผ๋ง ์ฆ๊ฐํ๊ฒ ๋๋ค.
app directory์์ network boundary๋ ์๋ฒ ์ปดํฌ๋ํธ๋, ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋์ ๋ฌ๋ ค์๋ค. pages directory์์๋ getStaticProps, getServerSideProps์ ๋ฌ๋ ค์๋ค.
Client Component
next.js์์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ ์๋ฒ์์ prerender ๋๊ณ ํด๋ผ์ด์ธํธ์์ hydration ๋๋ค. pages/ ๋๋ ํฐ๋ฆฌ์์ ๋์ํ๋ ๋ฐฉ์์ด๋ค. app ๋๋ ํฐ๋ฆฌ์์๋ ๋ค์๊ณผ ๊ฐ์ด ํ์ผ ์๋จ์ "use client";๋ฅผ ์์ฑํ๋ฉด ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๊ฐ ๋๋ค.
'use client';
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}์ด๋ ํด๋ผ์ด์ธํธ ๋ฒ๋ค๋ก ํฌํจ๋๋ ์ปดํฌ๋ํธ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค.
"use client";๊ฐ ์์ฑ๋ ์ปดํฌ๋ํธ"use client";๊ฐ ์์ฑ๋ ์ปดํฌ๋ํธ์ import๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ค- ์์์ปดํฌ๋ํธ
๋ฐ๋ผ์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๋๋ ์ต๋ํ ๋ง๋จ ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ ๊ฒ์ ์ถ์ฒํ๊ณ ์๋ค.
when to use server component vs client component?
next.js ํ์์๋ ์๋ฒ ์ปดํฌ๋ํธ๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํ๋ค๊ฐ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ ํ์ํ ๋๋ง ์ฌ์ฉํ ๊ฒ์ ์ถ์ฒํ๋ค. ๊ทธ๋ผ ์ธ์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๊ฐ ํ์ํ ๊น?
- interactivity๋ event listener(onClick(), onChange() ๋ฑ)๊ฐ ํ์ํ ๋.
- lifecycle effect๋ state๊ฐ ํ์ํ ๋
- browser-only API๋ฅผ ์ฌ์ฉํ ๋
- React Class Component๋ฅผ ์ฌ์ฉํ ๋
ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์๋ฒ์ปดํฌ๋ํธ ์ฌ์ฉํ๊ธฐ
๋ฆฌ์กํธ์์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์๋ฒ์ปดํฌ๋ํธ๋ฅผ importing ํ๋ ๊ฑด ์ ์ฝ์ด ์๋ค. ์๋ฒ ์ปดํฌ๋ํธ๋ server-only code ์ด๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์๋ฒ ์ปดํฌ๋ํธ๋ฅผ importํ ์ ์๋ค.
'use client';
// โ This pattern will not work. You cannot import a Server
// Component into a Client Component
import ServerComponent from './ServerComponent';
export default function ClientComponent() {
return (
<>
<ServerComponent />
</>
);
}ํ์ง๋ง ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์ props๋ก ๋๊ธฐ๋ ๊ฒ์ ๊ฐ๋ฅํ๋ค
'use client';
export default function ClientComponent({children}) {
return (
<>
{children}
</>
);
}// โ
This pattern works. You can pass a Server Component
// as a child or prop of a Client Component.
import ClientComponent from "./ClientComponent";
import ServerComponent from "./ServerComponent";
// Pages are Server Components by default
export default function Page() {
return (
<ClientComponent>
<ServerComponent />
</ClientComponent>
);
}์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋ฆฌ์กํธ๋ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ก child๋ฅผ ๋๊ธฐ๊ธฐ ์ ์ <ServerComponent/>๋ฅผ ์๋ฒ์์ ๋ ๋๋งํด์ผํ๋ค๋ ๊ฒ์ ์๋ค. ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์ ์
์ฅ์์๋ child ๊ฐ ์ด๋ฏธ ๋ ๋๋ง ๋์ด์๋ค. layout์ด ์ด ๋ฐฉ์์ผ๋ก ๊ตฌํ๋์ด์๋ค.
passing props from server to client component
์๋ฒ ์ปดํฌ๋ํธ์์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ก props๋ฅผ ๋๊ธธ ๋ serialization ๋ ํ์๊ฐ ์๋ค. function, Date ๋ฑ์ ์ง์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ก ๋๊ธธ ์ ์๋ค๋ ๊ฒ์ด๋ค.
server-only code๊ฐ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์คํ๋๋ ๊ฒ์ ๋ง๋ ๋ฐฉ๋ฒ
server-only ํจํค์ง๋ฅผ ์ฌ์ฉํ์ฌ, ์๋ฒ์์๋ง ์คํ๋์ด์ผํ๋ ์ฝ๋๊ฐ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์ฌ์ฉ๋๋ฉด build-time error๋ฅผ ๋ด๋๋ก ํ ์ ์๋ค.
import "server-only";
export async function getData() {
let resp = await fetch("https://external-service.com/data", {
headers: {
authorization: process.env.API_KEY,
},
});
return resp.json();
}๋ง์ฐฌ๊ฐ์ง๋ก client-only ํจํค์ง๋ ์ ๊ณตํ๋ค. ์ด ํจํค์ง๋ window ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ํจ์์ ๊ฐ์ด ํด๋ผ์ด์ธํธ์์๋ง ์ฌ์ฉ๋์ด์ผํ๋ ํจ์์์ ์ฌ์ฉํ ์ ์๋ค.
third party packages
ํ์ฌ ๋ง์ ์๋ํํฐ ํจํค์ง๋ค์ useState ๋ฑ์ ์ฌ์ฉํจ์๋ use client๋ฅผ ๋ถ์ด์ง ์์ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค. ์ด๋ฌํ ์๋ํํฐ ํจํค์ง๋ฅผ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ๋ ๊ฒ์ ํฐ ๋ฌธ์ ๊ฐ ์๋ค. ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์ ์ด๋ฏธ use client๊ฐ ๋ถ์ด์๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ง๋ง ์๋ฒ ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ ๋ ์ค๋ฅ๊ฐ ๋ฐ ๊ฒ์ด๋ค. ๋ฐ๋ผ์ ์๋ฒ ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์๋ํํฐ ํจํค์ง๋ฅผ ๊ฐ์ธ๊ณ ์ฌ์ฉํ๋ฉด ๋๋ค.
'use client';
import { AcmeCarousel } from 'acme-carousel';
export default AcmeCarousel;import Carousel from './carousel';
export default function Page() {
return (
<div>
<p>View pictures</p>
{/* ๐ข Works, since Carousel is a Client Component */}
<Carousel />
</div>
);
}data fetching
ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ data feching์ ํ ์ ์์ง๋ง, ๊ฐ๋ฅํ ์๋ฒ ์ปดํฌ๋ํธ์์ ํ๋ ๊ฒ์ด ์ฑ๋ฅ์ ์ข๋ค.
context
ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ context๋ provider๋ฅผ ์ฌ์ฉํ์ฌ context๋ฅผ ๊ณต์ ํ ์ ์๋ค. ํ์ง๋ง, ์๋ฒ ์ปดํฌ๋ํธ์์๋ react state๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค.
๋ฐ๋ผ์ next.js ํ์ ์๋ฒ ์ปดํฌ๋ํธ ๊ฐ์ ๋ฐ์ดํฐ ๊ตํ์ ์ํด native javascript ํจํด์ ์ ์ํ๋ค. ์๋ฅผ๋ค์ด ๋ง์ฝ ์ฌ๋ฌ ์๋ฒ ์ปดํฌ๋ํธ ์ฌ์ด์์ DB connection์ ๊ณต์ ํด์ผํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
// utils/database.js
export const db = new DatabaseConnection(...);// app/users/layout.js
import { db } from "@utils/database";
export async function UsersLayout() {
let users = await db.query(...);
// ...
}// app/users/[id]/pages.js
import { db } from "@utils/database";
export async function DashboardPage() {
let user = await db.query(...);
// ...
}์๋ฒ ์ปดํฌ๋ํธ์์๋ ๊ฐ์ context์์ fetch ์ค๋ณต๋ ์์ฒญ์ ์๋์ผ๋ก ์ ๊ฑฐ๋๋ค. ๋ฐ๋ผ์ fetch request result๋ฅผ ์ด๋ป๊ฒ ๊ณต์ ํ ์ง๋ ๊ฑฑ์ ํ์ง ์์๋ ๋๋ค. revalidate ๋ฑ์ ์ด๋ป๊ฒ ํ๋์ง๋ ๋ฐ์ data fetching ์น์ ์์ ๋ค๋ฃฌ๋ค.
Support for data fetching
fetch Web API๋ React์ next.js์์ ํ์ฅ๋์๋ค.
- React์์๋ automatic request deduping์ ์ํด fetch๋ฅผ ํ์ฅํ์๋ค.
- next.js์์๋ options๋ฅผ ํ์ฅํ์ฌ caching/revalidating ๊ท์น์ ์ค์ ํ์๋ค.
์ด๋ฅผ ํตํด next.js์์๋ ํ๋์ API๋ก SSG, SSR, ISR์ ์ฌ์ฉ๊ฐ๋ฅํ๋ค. (fetch์ ๋ํด์)
// This request should be cached until manually invalidated.
// Similar to `getStaticProps`.
// `force-cache` is the default and can be omitted.
fetch(URL, { cache: 'force-cache' });
// This request should be refetched on every request.
// Similar to `getServerSideProps`.
fetch(URL, { cache: 'no-store' });
// This request should be cached with a lifetime of 10 seconds.
// Similar to `getStaticProps` with the `revalidate` option.
fetch(URL, { next: { revalidate: 10 } });next.js์์๋ ์ฌ๋งํ๋ฉด ์๋ฒ ์ปดํฌ๋ํธ์์ data fetching ์ํ์ ๊ถ์ฅํ๋ค. ๋ค์๊ณผ ๊ฐ์ ์ด์ ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
- ๋ฐ์ดํฐ ๋ฆฌ์์ค๋ก์ ์ง์ ์ ๊ทผ
- API key์ ๊ฐ์ ๋ฏผ๊ฐํ ์ ๋ณด์ ๋ ธ์ถ ์๋ฐฉ
- data fetching๊ณผ render๊ฐ ๊ฐ์ ํ๊ฒฝ์์ ์ด๋ฃจ์ด์ง. ๋ฐ๋ผ์ ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฌ์ด์ ํต์ ์ด ์ค์ด๋ ๋ค.
- multiple data fetching์ ํ๋์ round-trip์ผ๋ก ์คํํ๋ค. ํด๋ผ์ด์ธํธ์์ ๊ฐ๋ณ์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ ๊ฒ๊ณผ ๋ค๋ฅด๋ค.
- ํด๋ผ์ด์ธํธ-์๋ฒ waterfall์ ์ค์
- ์๋ฒ๊ฐ ๋ฐ์ดํฐ ๋ฆฌ์์ค์ ๊ฐ๊น์ด ์๋ค๋ฉด ์ฑ๋ฅ ํฅ์์ ํจ๊ณผ๊ฐ ์์.
ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ data fetching์ ๊ฐ๋ฅํ์ง๋ง, ๊ทธ๋๋ SWR๋ React Query ์ฌ์ฉ์ ์ถ์ฒํ๊ณ ์๋ค. ํฅํ React์ use() hook์ ์ฌ์ฉํ ์๋ ์๋ค๊ณ ํ๋ค.
component-level data fetching
์ปดํฌ๋ํธ์์ data fetching์ ์ํํ ๋ ๋๊ฐ์ง ๋ชจ๋ธ์ด ์๋ค.
- parallel : route ๋ด request๊ฐ ๋์์ ์ด๋ฃจ์ด์ง๋ค. ์ด๋ client-server waterfall์ ์ค์ด๋ฉฐ ๋ฐ์ดํฐ ๋ก๋ฉ์ ๊ฑธ๋ฆฌ๋ ์ด ์๊ฐ์ ์ค์ธ๋ค.
- sequential : route ๋ด request๊ฐ ์์ฐจ์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ค. ํ๋์ ์์ฒญ์ด ๋ค๋ฅธ ์์ฒญ์ ์์กด์ ์ผ๋ ์ฌ์ฉํ ์ ์๋ค.

๋ํ ์์ ์ธ๊ธํ๋ฏ์ด fetch()์ ์๋์ผ๋ก deduping ๋๋ค. ๋ง์ฝ ํธ๋ฆฌ๋ด ์ฌ๋ฌ ์ปดํฌ๋ํธ์์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ํ๋ค๋ฉด, next.js๋ ์๋์ผ๋ก fetch request(GET)๋ฅผ ์บ์ฑํ๊ณ ๊ฐ์ ์์ฒญ์ด ๋ฐ์ํ๋ฉด ์ ์ฅํ๋ ๊ฒ์ ๋๋ ค์ค๋ค.
- ์๋ฒ์์๋ ๋ ๋๋ง ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ์บ์๊ฐ ์ ์ง๋๋ค.
- ์ด๋ Layout, Page, Server Component, generateMetadata, generateStaticParams์์ ์์ฑ๋
fetch์์ฒญ์ ์ ์ฉ๋๋ค. - static generation ๋์์๋ ์ ์ฉ๋๋ค.
- ์ด๋ Layout, Page, Server Component, generateMetadata, generateStaticParams์์ ์์ฑ๋
- ํด๋ผ์ด์ธํธ์์๋ ์ธ์ ๋์ ์บ์๊ฐ ์ ์ง๋๋ค.
๋ง์ฝ fetch() ๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ํฉ์ด๋ผ๋ฉด cache function์ ์ฌ์ฉํด๋ ๋๋ค.
static and dynamic data fetches
๋๊ฐ์ง ํ์ ์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ค.
- static data : ์์ฃผ ๋ณ๊ฒฝ๋์ง ์๋ ๋ฐ์ดํฐ
- dynamic data : ์์ฃผ ๋ณ๊ฒฝ๋๊ฑฐ๋ ์ ์ ๋ณ๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ
๊ธฐ๋ณธ์ ์ผ๋ก next.js์์๋ static fetch๋ฅผ ํ๋ค. ์ด๋ ๋น๋ํ์์ fetch๋์ด ์บ์๋๊ณ ๊ฐ ์์ฒญ๋ง๋ค ์ฌ์ฉํ๋ค๋ ๋ง์ด๋ค. ํ์ง๋ง dynamic fetch๋ฅผ ํด์ผํ ๋๋ ์๋๋ฐ, next.js๋ ์บ์๋ฅผ ๋ฌดํจํํ๊ฑฐ๋ revalidating ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
- ์บ์ ๋ฌดํจํ
fetch(URL, { cache: 'no-store' });- revalidating
- background : time interval์ ์ฃผ์ด ์ฃผ๊ธฐ์ ์ผ๋ก ๋ค์ fetch
fetch(URL, { next: { revalidate: 10 } });- on-demand : update๊ฐ ์์ ๋๋ง๋ค refetch ([์์ ](https://beta.nextjs.org/docs/data-fetching/revalidating#on-demand-revalidation))
next.js์์๋ Streaming๊ณผ Suspense์ ๋ํ ์ง์๋ ์ ๋์ด์๋ค. Layout ์น์ ์์ ๋ณด์๋ฏ์ด ํ๋ฉด์ ์ผ๋ถ๋ ์ฆ์ ๋ด๋ ค๋ณด๋ด๊ณ , ์ผ๋ถ๋ ๋น๋๊ธฐ์ ์ผ๋ก loading ํ๋ฉด์ ๋ณด์ฌ์ฃผ์๋ค๊ฐ data fetching์ด ์๋ฃ๋๋ฉด ๋ด๋ ค๋ณด๋ธ๋ค. ์ด๋ฅผ ํตํด ์ ์ ๋ ํ์ด์ง์ ๋ชจ๋ ๋ถ๋ถ์ ๋ก๋ํ ํ์๊ฐ ์๋ค.
caching data
The Next.js Cache is a persistent HTTP cache that can be globally distributed. This means the cache can scale automatically and be shared across multiple regions depending on your platform (e.g. Vercel).
next.js๋ fetch()์ options ๊ฐ์ฒด๋ฅผ ํ์ฅํ์ฌ ์๋ฒ์์ ์ด๋ฃจ์ด์ง๋ ๊ฐ ์์ฒญ์ด persistent caching ๋์์ด ์ด๋ฃจ์ด์ง๋๋ก ์ค์ ํ ์ ์๋ค. ์ด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ ์ดํ๋ฆฌ์ผ์ด์
์ฝ๋์์ ์บ์ฑ ๋์์ ์ค์ ํ ์ ์๋ค.