next.js์์๋ ์ด๋ค ํ์ด์ง๊ฐ getServerSideProps ๋๋ getInitialProps๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค๋ฉด, static ํ์ด์ง๋ก ๊ฒฐ์ ํ๋ค. ์ด๋ ๊ฒ static ํ์ด์ง๋ก ๊ฒฐ์ ๋๋ฉด, ๋น๋์ ๊ทธ ํ์ด์ง๋ static ํ์ด์ง๋ก ๋น๋๋๋ค.
static ํ์ด์ง๋ ์๋ฒ์ฌ์ด๋์์ ๋ ๋๋์ง ์๊ณ ๋ฐ๋ก ์ ์ ์๊ฒ ์ ๋ฌ๋๋ค. end-user์ ์๋ฒ ์ฌ์ด์ CDN์ด ์๋ค๋ฉด CDN์ ์ ์ฉ ๋ํ ๋ฐ๋๋ค.
query ๊ฐ์ฒด ์ฒ๋ฆฌ
getServerSideProps ๋๋ getInitialProps๊ฐ ์๋ ํ์ด์ง๋ ๋น๋์ prerender ๋์ด static ํ์ด์ง๊ฐ ์์ฑ๋๋ค.
prerender ๋์์ router์ query๋ ๋น์ด์๋ค. ํ์ง๋ง hydration์ด ์๋ฃ๋ ์ดํ next.js๋ ํ์ด์ง๋ฅผ ์
๋ฐ์ดํธํ๋ฉฐ query๊ฐ์ฒด๋ฅผ ๋ฐ์ํ๋ค. ์ด ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ ๊ณผ์ ์ผ ๋ ๋ฐ์ํ๋ค.
- ํ์ด์ง๊ฐ dynamic-route์ผ ๊ฒฝ์ฐ
- URL์ query ๊ฐ์ด ์์ ๊ฒฝ์ฐ
- Rewrites๊ฐ
next.config.js์ ์ค์ ๋์ด์์ ๋.
query๊ฐ ์์ ํ ๋ฐ์๋์์์ ํ์ธํ๊ธฐ ์ํด์ next/router์ isReady ํ๋๋ฅผ ํ์ธํ๋ฉด ๋๋ค.
๋น๋ ๊ฒฐ๊ณผ๋ฌผ
next build๋ก ๋น๋๋ฅผ ๋ง์น๋ฉด static ํ์ด์ง๋ .next/server/pages/about.html๋ก ๊ฒฐ๊ณผ๋ฌผ์ด ๋์จ๋ค. ํ์ง๋ง getServerSideProps ์ ์ฌ์ฉํ ํ์ด์ง๋ .next/server/pages/about.js๋ก ๊ฒฐ๊ณผ๋ฌผ์ด ๋์ค๊ฒ ๋๋ค.
์ฃผ์์ฌํญ
getInitialProps๋ฅผ ์ฌ์ฉํ๋ Custom App์ด ์๋ค๋ฉด,getStaticProps๋ฑ์ ํ์ฉํ Static Generation๋ฅผ ์ ์ฉํ์ง ์๋ํ automatic static ์ต์ ํ๊ฐ ๋์ํ์ง ์๋๋ค.getInitialProps๋ฅผ ์ฌ์ฉํ๋ Custom Document๊ฐ ์๋ค๋ฉด,ctx.req๊ฐ ๋น๋์ ์ ์๋์ด์์ด์ผํ๋ค. prerender ๋จ๊ณ์์ctx.req๋ undefined์ด๊ธฐ ๋๋ฌธ์ด๋ค.next/router์isReadyํ๋๊ฐtrue๊ฐ ๋๊ธฐ ์ ๊น์งasPath๊ฐ์ ์ฌ์ฉํ์ง๋ง๋ผ.
์ถ์ฒ
- [automatic-static-optimization][https://nextjs.org/docs/advanced-features/automatic-static-optimization]