๐ก TIL 20230816 - redirect์ rewrite
1. redirect, rewrite์ ๊ณตํต์ /์ฐจ์ด์
redirect์ rewrite
์ ์ ๊ฐ ์ด๋ค path๋ก ์ ๊ทผํ๋ ๊ฒฝ์ฐ, ๋ค๋ฅธ path๋ ๊ฒฝ๋ก๋ก ์๋์ผ๋ก ์ด๋์์ผ์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
๊ณตํต์
- next.js์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด๋ค.
- ๋ ๊ฐ์ง ๋ชจ๋ ์ ์ ๊ฐ ํน์ path๋ก ์ด๋ ์ ์๋ํ path๋ก ์ด๋์ํค ์ํด ์ฌ์ฉํ๋ค.
์ฐจ์ด์
- redirect๋ ์ ํด์ง path๋ก url์ด ๋ณ๊ฒฝ๋์ด ์ ์ ๊ฐ ํ์ธํ ์ ์๋ค.
- ๋ฐ๋ฉด rewrite๋ redirect ํด์ฃผ๋ ๊ฒ์ ๋๊ฐ์ง๋ง, ์ ์ ๊ฐ ์ฒ์ ์ ๋ ฅํ url ๊ทธ๋๋ก ์ ์ ์๊ฒ ๋ณด์ฌ์ง๋ค.
2. redirect, rewrite ์ค์ ๋ฐฉ๋ฒ
1) next.config.js
// next.config.js
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: false,
},
{
source: '/mypage',
destination: '/myprofile',
permanent: true,
},
]
},
async rewrites() {
return [
{
source: '/about',
destination: '/',
},
]
}
}
๊ธฐ๋ณธ url์ด `localhost:3000`์ด๋ผ๊ณ ๊ฐ์ ํ์ ๊ฒฝ์ฐ, ์์ redirects ์ค์ ์ ์ฌ์ฉ์๊ฐ `localhost:3000/about`์ผ๋ก ์ ๊ทผ ์ ๋ค์ ๊ธฐ๋ณธ `/` (=`localhost:3000`)์ผ๋ก redirect ์์ผ์ค๋ค๋ ์๋ฏธ์ด๋ค.
- source : ์ฌ์ฉ์๊ฐ ์์ฒญํ๋ path์ ํจํด์ ๋งํ๋ค.
- destination : ๊ฐ๋ฐ์๊ฐ ์๋ํด์ ๋ฆฌ๋ค์ด๋ ํธ ์ํค๊ธฐ ์ํ ๋ชฉ์ ์ง path๋ฅผ ์๋ฏธํ๋ค.
- // redirect
- permanent : true or false - ์ด ๋ฆฌ๋๋ ์ ์ด ์๊ตฌํ์ง ์๋์ง์ ๋ฐ๋ผ ๋ธ๋ผ์ฐ์ ๋ ๊ฒ์์์ง์ด ์ด ์ ๋ณด๋ฅผ ๊ธฐ์ตํ๋์ง ์ฌ๋ถ๊ฐ ๊ฒฐ์ ๋๋ค.
3. source, destination ์ค์
1) ์ธ๋ถ ์น์ฌ์ดํธ๋ก ๋ฆฌ๋๋ ์
destination์ path๋ ํ์ฌ ์นํ์ด์ง ๋ด์ path๋ก๋ง ์ค์ ํ ์ ์๋ ๊ฒ์ด ์๋๊ณ , ์ธ๋ถ ์น์ฌ์ดํธ๋ก๋ ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
// next.config.js
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: 'https://google.com',
permanent: true,
},
]
},
}
์์ฒ๋ผ ์ค์ ํด์ฃผ๋ฉด, ์ ์ ๊ฐ `localhost:3000/about`์ผ๋ก ์ ๊ทผ ์ google.com์ผ๋ก ์ด๋ํ๋ค.
2) pattern matching
// next.config.js
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:id',
destination: '/new-blog/:id',
permanent: true,
},
]
},
}
์์ฒ๋ผ ์ค์ ํด์ฃผ๋ฉด, ์ ์ ๊ฐ `localhost:3000/old-blog/124211`๋ก ์ ๊ทผ ์, `localhost:3000/new-blog/124211`๋ก ์ด๋ํ๋ค.
124211 ์๋ฆฌ์ ๋ค๋ฅธ id๊ฐ ์ค๋๋ผ๋ ๋๊ฐ์ด ์๋ํ๋ค.
3) catch
// next.config.js
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:id*',
destination: '/new-blog/:id*',
permanent: true,
},
]
},
}
์์ฒ๋ผ ์ค์ ํด์ฃผ๋ฉด, ์ ์ ๊ฐ `localhost:3000/old-blog/124211/comments/49285`๋ก ์ ๊ทผ ์, `localhost:3000/new-blog/124211/comments/49285`๋ฅผ ๋ชฉ์ ์ง๋ก ๋ฆฌ๋๋ ์ ํด์ค๋ค.
3. rewrite๋ฅผ ์ด์ฉํ api key ์จ๊ธฐ๊ธฐ
// next.config.js
/** @type {import('next').NextConfig} */
const API_KEY = process.env.API_KEY;
const nextConfig = {
reactStrictMode: true,
async rewrites() {
return [
{
source: "/api/movies",
destination: `https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}`
},
]
}
}
module.exports = nextConfig
.env
API_KEY=๋ณธ์ธ์ APIํค
- ์์ ๊ฐ์ด ๊ธฐ๋ณธ next.config ํ์ผ์์์ rewrite ์ค์ ์ ํตํด API_KEY๋ฅผ ์์ฝ๊ฒ ์จ๊ธธ ์ ์๋ค.
์ฐธ๊ณ ์ฌ์ดํธ
Nomad Coder - NextJS ์์ํ๊ธฐ
Next.js ๊ณต์ ๋ฌธ์ - rewrites
next.js์ rewrite์ redirect
'โ๏ธ What I Learned > TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] Next.js 13 - getStaticProps, getStaticPaths (feat. 12๋ฒ์ ์์์ getServerSideProps) (0) | 2023.08.18 |
---|---|
[TIL] Next.js - SSR vs SSG vs ISR (0) | 2023.08.17 |
[TIL] React.js์ Next.js์ ์ฐจ์ด์ - Library vs Framework, CSR vs SSR (0) | 2023.08.15 |
[TIL] npm vs npx vs yarn (0) | 2023.08.15 |
[TIL] ๊ฐ๋จํ Recoil ์ฒซ ์ฌ์ฉ๊ธฐ (0) | 2023.08.14 |