- Next.js๋ ๋ชจ๋ ํ์ด์ง๋ฅผ Pre-renderingํ๋ค
- ์ฆ, Next.js๋ ํด๋ผ์ด์ธํธ์ธก์ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋ก๋๋๊ธฐ ์ ์ ๋ฏธ๋ฆฌ HTML์ ์์ฑํ๋ค
- Pre-rendering์ ๋ ๋์ ์ฑ๋ฅ๊ณผ SEO์ ์ต์ ํ ๋์ด ์๋ค.

Static Generation , Server-side Rendering
Next.js์๋ Static Generation, Server-side Rendering ๋๊ฐ์ง ํํ์ Pre-rendering ๋ฐฉ์์ ์ ๊ณตํ๋ค
๋๊ฐ์ง Rendering ๋ฐฉ์์ ์ฐจ์ด์ ์ ํ์ด์ง์ ๋ํ HTML์ ์ธ์ ์์ฑ ํ๋๋์ ๋ฐ๋ฅธ ์ฐจ์ด์ด๋ค.
Static Generation - HTML์ ๋น๋ ์ ์์ฑ๋๋ฉฐ ์ฌ์ฉ์ ์์ฒญ์ ๋ฐ๋ผ ์ฌ์ฌ์ฉ๋๋ค.
Server-side Rendering - HTML์ ์ฌ์ฉ์ ์์ฒญ๋ง๋ค ์์ฑ๋๋ค
Next.js๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ ํ์ด์ง์์ ์ฌ์ฉ ํ Pre-rendering ๋ฐฉ์์ ์ ํํ ์ ์๋ค.
- ์ฌ์ฉ์์ ์์ฒญ์ ์ํ ๋ฐ์ดํฐ์ ์ด์ฉ ์์ด ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ์ ์ ํ์ด์ง๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ์๋ Static Generation์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค. ์) ํ์ฌ ์๊ฐ ํ์ด์ง, ๋ง์ผํ ํ์ด์ง
- ํ์ด์ง์์ ์์ฃผ ์ ๋ฐ์ดํธ ๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ค์ผํ๊ณ , ์ฌ์ฉ์ ์์ฒญ๋ง๋ค ํ์ด์ง์ ๋ด์ฉ์ด ๋ณ๊ฒฝ๋์ด์ผ ํ๋ ๊ฒฝ์ฐ๋ผ๋ฉด Server-side Rendering๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค
์ฑ๋ฅ์์ ์ด์ ๋ก ๊ฐ๋ฅํ๋ค๋ฉด Static Generation์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
์ ์ ์ผ๋ก ์์ฑ๋ ํ์ด์ง๋ CDN์์ ์บ์ํ ์ ์๋ค.

Static Generation without data
์ผ๋ถ ํ์ด์ง๋ Pre-rendering์ ์ํด ์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ผ ํ๋ค
๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๋ฐฉ์์๋ ๋๊ฐ์ง ๋ฐฉ์์ด ์์ผ๋ฉฐ ํ๋๊ฐ ๋ ์๋ ์๊ณ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ ๋ค ์ ์ฉ ๋ ์ ์๋ค.

ํ์ด์ง ์ฝํ ์ธ ๊ฐ ์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ฅธ๋ค๋ฉด getStaticProps
ํ์ด์ง ๊ฒฝ๋ก๊ฐ ์ธ๋ถ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ฅธ๋ค๋ฉด getStaticPaths
function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li>{post.title}</li>
))}
</ul>
)
}
// ์ด ํจ์๋ ๋น๋ ํ์ ๋ ํธ์ถ๋๋ค
export async function getStaticProps() {
// ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ธฐ ์ํด ์ธ๋ถ API ํธ์ถ
const res = await fetch('https://.../posts')
const posts = await res.json()
// { props: { posts } }๋ฅผ ๋ฆฌํดํ๊ณ Blog ์ปดํฌ๋ํธ๊ฐ posts๋ฅผ props๋ก ๋ฐ๋๋ก ํ๋ค
return {
props: {
posts,
},
}
}
getStaticPaths
Next.js๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ ๊ฒฝ๋ก ๊ฐ ์๋ ํ์ด์ง๋ฅผ ๋ง๋ค ์ ์๋ค
- pages/posts/[id].js
[id].js ํ์ผ ๋น๋ ์ pre-rendering ํ๋ ค๋ ๋ถ๋ถ์ ์ธ๋ถ ๋ฐ์ดํฐ์ ๋ฐ๋ผ ๋ค๋ฅผ ์ ์๋ค.
getStaticPath๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ๊ฒฝ๋ก์ ํ์ด์ง๋ฅผ ๋น๋ ํ์ ๋ pre-rendering ํ๋๋ก ์ค์ ํ ์ ์๋ค
๋์ ๋ผ์ฐํ + getStaticProps๋ฅผ ์ํ ๋ ์ฌ์ฉํ๋ค
function Post({ post }) {
// Render post...
}
// ์ด ํจ์๋ ๋น๋ ์ ํธ์ถ๋๋ค
export async function getStaticPaths() {
const res = await fetch('https://.../posts')
const posts = await res.json()
// pre-renderํ Path๋ฅผ ์ป๋๋ค
const paths = posts.map((post) => ({
params: { id: post.id },
}))
// paths ๋ฐฐ์ด์ ํด๋นํ๋ ๊ฒฝ๋ก์ ํ์ด์ง๋ง ๋น๋ ํ์์ pre-rendering
// { fallback: false } ๋ค๋ฅธ routes๋ค์ 404
return { paths, fallback: false }
}
// ์ด ํจ์๋ ๋น๋ ์ ํธ์ถ๋๋ค
export async function getStaticProps({ params }) {
// params contains the post `id`.
// params๋ post.id๋ฅผ ํฌํจํ๊ณ ์๋ค
// ๊ฒฝ๋ก๊ฐ /posts/1์ด๋ฉด params.id๋ 1์ด๋ค
const res = await fetch(`https://.../posts/${params.id}`)
const post = await res.json()
// ์ธ๋ถ์์ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ props๋ก ์ ๋ฌ
return { props: { post } }
}
Server-side Rendering
getServerSideProps๋ ๋งค ํ์ด์ง ์์ฒญ๋ง๋ค ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก๋ถํฐ ๊ฐ์ ธ์จ๋ค
function Page({ data }) {
// Render data...
}
// ๋ชจ๋ ์์ฒญ๋ง๋ค ํธ์ถ๋๋ค
export async function getServerSideProps() {
// API ํธ์ถ
const res = await fetch(`https://.../data`)
const data = await res.json()
// ๋ฐ์ ๋ฐ์ดํฐ๋ props๋ฅผ ํตํด ์ ๋ฌ๋๋ค
return { props: { data } }
}
export default Page
๋ณด์๋ค์ํผ getServerSideProps์ getStaticProps ์ ์ ์ฌ ํ์ง๋ง getServerSideProps ๋ ๋ชจ๋ ์์ฒญ์ ๋ํด ์คํ๋๋ค๋ ๊ฒ์ด๋ค.
reference
'Next' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Next.js Image ์์๋ณด๊ธฐ (0) | 2022.09.18 |
|---|---|
| Next.js์ _app๊ณผ _document ์์๋ณด๊ธฐ (0) | 2022.09.05 |
| Next.js ์์๋ณด๊ธฐ (1) | 2022.08.31 |