Framework/Next.js

[Next.js] url ๋กœ ํŠน์ • page์— ์ง์ ‘ ์ ‘๊ทผ ๋ง‰๋Š” ๋ฐฉ๋ฒ•

yuri lee 2022. 10. 3. 10:11
๋ฐ˜์‘ํ˜•

Intro

Next.js๋กœ ๊ตฌํ˜„๋œ ํ”„๋กœ์ ํŠธ์—์„œ ํŠน์ • ํŽ˜์ด์ง€๋“ค์„ ์ˆจ๊ธฐ๋Š” ์š”์ฒญ์‚ฌํ•ญ์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต Next.js๋Š” pages๋ผ๋Š” ํด๋” ํ•˜์œ„์— ํŒŒ์ผ ํ˜น์€ ํด๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ํŒŒ์ผ/ํด๋”๋ฅผ ์ž๋™์œผ๋กœ ๋ผ์šฐํŒ… ํ•ด์ค๋‹ˆ๋‹ค. 

 

์ฒ˜์Œ ๋‹จ์ˆœํ•˜๊ฒŒ NavBar ๋„ค๋น„๋ฐ”์— ์žˆ๋Š” ํŠน์ • link tag ๋ฅผ ์ฃผ์„์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ, url๋กœ ํ•ด๋‹น ํŽ˜์ด์ง€ ์ง์ ‘ ์ ‘๊ทผ์‹œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

How to do 

Next.js์˜ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, Next.js๋Š” Redirects๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ธฐ๋Šฅ์€ Next.js ํ”„๋กœ์ ํŠธ ํด๋” root์— ์žˆ๋Š” next.config.js ํŒŒ์ผ์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

Redirects

Redirects allow you to redirect an incoming request path to a different destination path. 

module.exports = {
  async redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}

Redirects ๋Š” source, destination, permanent ์†์„ฑ์ด ํฌํ•จ๋œ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” array์ด ๋ฐ˜ํ™˜๋˜๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. 

  • source : ์ ‘๊ทผ์„ ๋ง‰์„ ํŽ˜์ด์ง€
  • destination : redirect ํ•  ํŽ˜์ด์ง€ 
  • permante : ์˜๊ตฌ์ ์œผ๋กœ redirect ํ• ์ง€์˜ ์—ฌ๋ถ€ (true/false)

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Next.js ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 


https://nextjs.org/docs/api-reference/next.config.js/redirects

 

๋ฐ˜์‘ํ˜•