pages/404.js
ํ์ผ ์์ฑ// pages/404.js
export default function Custom404() {
return <h1>404 - Page Not Found</h1>
}
// customizing
import { Icon } from "semantic-ui-react";
export default function Error404() {
return (
<div style={{ padding: "20px 0 ", textAlign: "center", fontSize: 30 }}>
<Icon name="warning circle" color="red" />
404: ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
</div>
);
}
๊ฐ๋ฐ ๋ชจ๋์์๋ ์๋ฒ ์๋ฌ๊ฐ ๋๋ฉด ๋ก๊ทธ๋ฅผ ๋ณด์ฌ์ค
=> ์ด๋์ ์๋ฌ๊ฐ ๋ฐ์ํ๋์ง ์๋ ค์ค
- 500 error(์๋ฒ ์๋ฌ)๋ ํ๋ก๋์ ๋ชจ๋์์ ํ์ธํด์ผ ํ๋ค
=> ํ๋ก๋์ ๋ชจ๋ :build
๋ฅผ ํ ํstart
๋ฅผ ํ๋ค.
=> ๋ด ๊ฒฝ์ฐ,yarn build
,yarn start
=> 500 error ํ์ธ
Error component๋ก client-side error์ server-side error๋ฅผ ๋ชจ๋ ๊ด๋ฆฌํ ์ ์๋ค.
=> _error.js
ํ์ด์ง๋ ์ ์ ์ผ๋ก ์ต์ ํ ๋์ด์์ง ์๋ค.
=> 404๋ static์ผ๋ก ์ฒ๋ฆฌ ํ๋ ๊ฒ์ด ์ข๋ค
pages/_error.js
ํ์ผ ์์ฑ
// _error.js
function Error({ statusCode }) {
return (
<p>
{statusCode
? `An error ${statusCode} occurred on server`
: 'An error occurred on client'}
</p>
)
}
Error.getInitialProps = ({ res, err }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404
return { statusCode }
}
export default Error
ํ๊ฒฝ์ ๋ฐ๋ผ ๋ณํ ์ ์๋ ๊ฐ๋ค์ ๋ถ๊ธฐ ์ฒ๋ฆฌ
=> ๊ฐ๋ฐ ํ๊ฒฝ์์๋ A ๋ฆฌ์คํธ๊ฐ ๋ณด์ฌ์ผ ํ์ง๋ง ํ๋ก๋์
ํ๊ฒฝ์์๋ B ๋ฆฌ์คํธ๊ฐ ๋ณด์ฌ์ ธ์ผ ํ๋ ๊ฒ ๋ฑ
=> ํ
์คํธ๋ก ์ด์ํ๋ ๋ฐ์ดํฐ๊ฐ ์ค์ ์๋น์ค์ ๋ํ๋์ง ์๋๋ก ํ๊ธฐ ์ํด
.env
: ๋ชจ๋ ํ๊ฒฝ.env.development
: ๊ฐ๋ฐ ํ๊ฒฝ.env.production
: ํ๋ก๋์
ํ๊ฒฝ=> ์ต์์ ํด๋์ ํ์ผ ์์ฑ
Environment Variables Examples
// node js
process.env.๋ณ์๋ช
;
// browser
process.env.NEXT_PUBLIC_๋ณ์๋ช
;
//.env.development
// name : ๋
ธ๋ ํ๊ฒฝ์์ ์ฌ์ฉ
// NEXT_PUBLIC_API_URL : ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ ์ฌ์ฉ
name=DEVELOPMENT
NEXT_PUBLIC_API_URL=http://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline
// .env.production
// ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์ ๋ฐ๋ผ API ๊ฒฝ๋ก๋ฅผ ๊ตฌ๋ถํด์ ์ํ ๋ฆฌ์คํธ๋ฅผ ๋ค๋ฅด๊ฒ ๋์ค๊ฒ ํ ์ ์๋ค
// .env.development์ API url๊ณผ ๋ค๋ฅธ url
name=PRODUCTION
NEXT_PUBLIC_API_URL=http://makeup-api.herokuapp.com/api/v1/products.json?brand=dior
NEXT_PUBLIC_API_URL
๋ณ์ ๋ถ๊ธฐ์ฒ๋ฆฌpages/index.js
์ API_URL
์์ NEXT_PUBLIC_API_URL
๋ฅผ ์ฌ์ฉ// const API_URL =
// "http://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline";
const API_URL = process.env.NEXT_PUBLIC_API_URL;
name
๋ณ์๋ฅผ ๋ถ๊ธฐ์ฒ๋ฆฌimport Axios from "axios";
import Head from "next/head";
import Item from "../../src/component/Item";
// getServerSideProps๋ฅผ ํตํด item์ ๋ฐ์์ฌ ์ ์์ผ๋ฏ๋ก
// name๋ ๋ฐ์์จ๋ค
const Post = ({ item, name }) => {
return (
<>
{item && (
<>
<Head>
<title>{item.name}</title>
<meta name="description" content="item.description"></meta>
</Head>
// name์ ํตํด ์ด๋ค ํ๊ฒฝ์ธ์ง ํ์ธ
<p>{name} ํ๊ฒฝ ์
๋๋ค.</p>
<Item item={item} />
</>
)}
</>
);
};
export default Post;
// getServerSideProps๋ก ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ด
export async function getServerSideProps(context) {
const id = context.params.id;
const apiUrl = `http://makeup-api.herokuapp.com/api/v1/products/${id}.json`;
const res = await Axios.get(apiUrl);
const data = res.data;
return {
props: {
item: data,
name: process.env.name, // props๋ก name ์ถ๊ฐ
},
};
}
์ฐ์ใ ใ ใ ใ