NextJS ๊ณต์๋ฌธ์ ๐
Next.js์์ ์ปค์คํ
๊ณ ๊ธ(?) ์ค์ ์ ํ๊ธฐ ์ํด์ ํ๋ก์ ํธ ๋๋ ํฐ๋ฆฌ ๋ฃจํธ์ next.config.js ํ์ผ์ ๋ง๋ค ์ ์๋ค.next.config.js๋ JSON ํ์ผ์ด ์๋ ์ผ๋ฐ Node.js ๋ชจ๋์ด๋ค.Next.js ์๋ฒ ๋ฐ ๋น๋ ๋จ๊ณ์์ ์ฌ์ฉ๋๋ฉฐ ๋ธ๋ผ์ฐ์ ๋น๋์๋ ํฌํจ๋์ง ์๋๋ค.
next.config.js์์redirects์rewrites์ค์ ์ ํ ์ ์๋ค.
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
async redirects() {
return [
{
source: "/contact",
destination: "/form",
permanent: false,
},
];
},
};
module.exports = nextConfig;
redirects ์ source, destination, permanent ์์ฑ์ด ์๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ฐํํ๋ ๋น๋๊ธฐ ํจ์์ด๋ค.source : request ๊ฒฝ๋กdestination : redirectํ ๊ฒฝ๋กpermanent : true or false308 status code๋ฅผ ์ฌ์ฉ307 status code๋ฅผ ์ฌ์ฉ๐
next.config.js์์redirectsํค๋ฅผ ์ฌ์ฉํ์ฌsource๊ฒฝ๋ก๋ฅผdestination๊ฒฝ๋ก๋กredirectํ๋๋ก ์ค์ ํ ์ ์๋ค.
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
async redirects() {
return [
{
source: "/old-blog/:path",
destination: "/new-blog/:path",
permanent: false,
},
];
},
};
module.exports = nextConfig;
// ex) localhost:3000/old-blog/11122 (request) -> localhost:3000/new-blog/11122 (redirect)
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
async redirects() {
return [
{
source: "/old-blog/:path*",
destination: "/new-blog/:path*",
permanent: false,
},
];
},
};
module.exports = nextConfig;
// ex) localhost:3000/old-blog/11122/comments/11 (request)
// -> localhost:3000/new-blog/11122/comments/11 (redirect)
/** @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}`,
},
{
source: "/api/movies/:id",
destination: `https://api.themoviedb.org/3/movie/:id?api_key=${API_KEY}`,
},
];
},
};
module.exports = nextConfig;
rewrites๋ source, destination ์์ฑ์ ๊ฐ์ง๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ฐํํ๋ ๋น๋๊ธฐ ํจ์์ด๋ค.source: request ๊ฒฝ๋กdestination : routingํ ๊ฒฝ๋ก๐
next.config.js์์rewritesํค๋ฅผ ์ฌ์ฉํ์ฌsource๊ฒฝ๋ก๋ฅผdestination๊ฒฝ๋ก์ ๋งคํํ๋๋ก ์ค์ ํ ์ ์๋ค.
โจ Example
1. rewrites ์ค์ X
// next.config.js
/** @type {import('next').NextConfig} */
const API_KEY = process.env.API_KEY;
const nextConfig = {
reactStrictMode: true,
};
module.exports = nextConfig;
rewrites ์ค์ ์ ํ์ง ์์ ์ํ์์ https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}๋ก GET ์์ฒญ์ ๋ณด๋ด๋ฉด ์๋ ์ฌ์ง์ ๋ณด์ด๋ฏ์ด API KEY๊ฐ ๊ทธ๋๋ก ๋
ธ์ถ์ด ๋๋ค.

/** @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;
http://locahost:3000/api/movies ํน์ api/movies๋ก GET ์์ฒญ์ ๋ณด๋ด๋ฉด rewrites ์ค์ ์ ๋ฐ๋ผ destination ๊ฒฝ๋ก๋ก ๋งคํ์ด ๋์ด https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}๋ก GET ์์ฒญ์ ๋ณด๋ธ๋ค. ๋ํ, API_KEY์ ๋
ธ์ถ์ ๋ง์ ์ ์๋ค.๐

๐ Rewrites vs Redirect
Rewrites์ URL ํ๋ก์ ์ญํ ์ ํ๊ณ destination ๊ฒฝ๋ก๋ฅผ source ๊ฒฝ๋ก๋กmaskํ๋ค.Redirects์ source๊ฒฝ๋ก๋ฅผ destination ๊ฒฝ๋ก๋ก redirect์ํค๊ณ , URL๋ฅผ ๋ณ๊ฒฝํ๋ค.