-
Notifications
You must be signed in to change notification settings - Fork 5
NextJs
๋ฆฌ์กํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ SPA๋ฅผ ๋น ๋ฅด๊ฒ ๋น๋ํ ์ ์๋๋ก ๋์์ฃผ๋ ํ๋ ์์ํฌ.
- ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฒ์ฌ์ด๋ ๋ ๋๋ง์ ์ ๊ณตํจ
- ์๋ฒ์ฌ์ด๋ ๋ ๋๋ง์ ์ฅ์
- ๊ฒ์์์ง ์ต์ ํ
- ์ด๊ธฐ ๋ก๋ฉ ์ฑ๋ฅ ๊ฐ์
- ๊ทธ๋ฅ ๊ฒ์์์ง ์ต์ ํ SEO (Search Engine Optimization)
- ๋์ฑ ๋น ๋ฅธ ํ์ด์ง ๋ก๋๋ฅผ ์ํ ์ฝ๋ ์คํ๋ฆฌํ ์๋ํ
- HMR์ ์ง์ํ๋ ์นํฉ ๊ธฐ๋ฐ ํ๊ฒฝ
- Express๋ Node.js์ ๊ฐ์ http ์๋ฒ์ ํจ๊ป ๊ตฌํ ๊ฐ๋ฅ
- Babel, Webpack ์ค์ ์ปค์คํฐ๋ง์ด์ง ๊ฐ๋ฅ
- isomorphic-unfetch
๊ณตํต์ : page ์ปดํฌ๋ํธ์์ ์ฌ์ฉํด์ผ ํจ.
-
getStaticProps
- SSG
- ๋ค๋ฅธ ํจ์๋ค ๋ณด๋ค ์ด ํจ์๋ฅผ ์ฌ์ฉํ๊ธธ ๊ถ์ฅ
- ๋ฐ์ดํฐ๋ฅผ ์บ์ฑํ๊ณ pre-rendering์ ์งํ, ๊ทธ๋ฌ๊ธฐ์ ์ข ๋ ์ข์ ์ฑ๋ฅ
- request์ ์์ ํ์ด์ง๋ฅผ ๋ ๋๋งํ ๋ ํ์ํ ๋ฐ์ดํฐ๊ฐ ์์ ๋
- ๋ฐ์ดํฐ๊ฐ ๊ณต์์ ์ผ ๋ (ํน์ ์ ์ ๋ ๊ด๊ณ๋๋ฉด ์๋)
- ๋ฐ์ดํฐ๊ฐ headless CMS๋ก ๋ถํฐ ์ฌ ๋,
** headless CMS?
- ๋จธ๋ฆฌ๋ ์ปจํ ์ธ ๋ฅผ ๋ณด์ฌ์ค ์๋จ, ๋ชธํต์ ์ปจํ ์ธ ๋ฅผ ์๋ฏธ
- ๋จธ๋ฆฌ(ํํ ์๋จ)๋ฅผ ์ธ์ ๋ ๋ฐ๊ฟ ๋ผ์ธ ์ ์๋ค๋ ๋ป์ด๋ค.
-
Headless CMS๋ ์ด๋ป๊ฒ ์๋ํ๋๊ฐ?
- ์ปจํ ์ธ ํธ์ง์์ ๊ฐ๋ฐ์๊ฐ ์๋ฒฝํ ๋ถ๋ฆฌ๋ ์ ์๋ค. ์ปจํ ์ธ ํธ์ง์๋ ์ต์ํ GUI๋ก ์ปจํ ์ธ ๋ฅผ ์ถ๊ฐ/์์ /์ญ์ ํ ์ ์๋ค.
-
getStaticPaths
- getStaticProps๋ฅผ ์ธ ๋๋ง ์ฌ์ฉ ๊ฐ๋ฅ
- route๊ฐ ๋์ ์ผ ๋ ์ฌ์ฉํ๋ค. /post/[pId]
- ๋จ, ๊ทธ๋ผ post id๊ฐ 20000๊ฐ ๊ฐ๋ ์์๋๋ ์ด๊ฑธ pre renderํ ๊ฒ์ด๋..? โ ์๋ฐฉ๋ฐฉ๋ฒ์ด ์๋คํจ.
-
getServerSideProps
- SSR
- ์์ฒญ๋ง๋ค ๋ฐํ ๋ฐ์ดํฐ๊ฐ์ด ๋ณํ๊ฐ ๋ง์ ๊ฒฝ์ฐ ์ด ํจ์๋ก ํธ์ถํ๋ฉด ์ข๋ค.
- ๋น๋ ์ดํ์๋ Data ๋ณ๊ฒฝ๊ฐ๋ฅ? ๊ทธ๋ผ SSR์ฌ์ฉ
pre-rendering : ๋ฏธ๋ฆฌ html์ ๋ง๋๋ ๊ฒ.
** ์ฌ๊ธฐ์ ๋น๋ํ์์ด๋? next-build
Static Generation
HTML์ ๋น๋ํ์์ ๋ง๋ค์ด๊ฐ์ง๊ณ , ๊ฐ๊ฐ request ๋ณด๋ผ ๋ ์ฌ ์ฌ์ฉํจ. getStaticProps๋ฅผ exportํด์ ์ฌ์ฉํ๋ค.
bundle.js์๋ ํฌํจ ์๋.(?)
Server-Side Rendering
HTML์ด ๊ฐ๊ฐ request๋ฅผ ๋ณด๋ผ ๋ ๋ง๋ค์ด์ง. getServerSideProps๋ฅผ exportํ์ฌ ์ฌ์ฉํ๋ค.
https://medium.com/@selvaganesh93/setup-next-js-with-typescript-integration-dece94e43cf5
yarn init -y
npm init --yes
yarn add react react-dom next
npm install react react-dom next
Package.json
ย ํ์ผ์ ๋ค์ด๊ฐ์ ๋ค์ ์คํฌ๋ฆฝํธ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
}
yarn add @zeit/next-typescript @types/next @types/react
Create aย next.config.js
ย in your project
// next.config.js
const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript()
Create aย .babelrc
ย in your project
{
"presets": ["next/babel", "@zeit/next-typescript/babel"]
}
Create aย tsconfig.json
ย in your project
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"removeComments": false,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "esnext"
}
}
Optionally you can add your custom Next.js configuration as parameter
// next.config.js
const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript({
webpack(config, options) {
return config
}
})
The next step in this set up is to add aย scripts
ย property to theย package.json
file that was generated in the installation above. The property will
include a script to run a local serverย for development of your project:
{
...
"scripts": {
"dev": "next"
}
}
์ค๊ฐ์ ์ค๋ฅ ๋ฐ์์ ํ์ํ ๋ชจ๋๋ค์ ์ค์นํด์ฃผ์!!
npm install --save-dev typescript @types/node @babel/core
๋จผ์ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ global๋ก ์ค์นํฉ์๋ค.
$ yarn global add typescript
$ npm install -g typescript
๊ทธ๋ฐ ๋ค์ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์
๋ ฅํ๋ฉดย tsconfig.json
ย ํ์ผ์ด ์๋ ์์ฑ๋ฉ๋๋ค.
$ tsc --init
tsconfig.json ํ์ผ์์๋ ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์ปดํ์ผ ๋ ๋ ํ์ํ ์ต์ ์ ์ ํฉ๋๋ค. ๋ช ๋ น์ด๋ฅผ ์์๋ด ์๋ค.
-
targetย : ์ปดํ์ผ๋ ์ฝ๋๊ฐ ์ด๋ค ํ๊ฒฝ์์ ์คํ๋ ์ง ์ ์ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ย
target
์ยes5
๋ก ์ง์ ํ๋ค๋ฉด ์ด๋ฅผ ์ผ๋ฐ functionํค์๋๋ฅผ ํ์ฉํ ํจ์๋ก ์ปดํ์ผ ํด์ค๋๋ค. ๊ทธ๋ฌ๋, ์ด๋ฅผยes6
๋ก ์ค์ ํ๋ค๋ฉด ํ์ดํ ํจ์ ๊ทธ๋๋ก ์ ์งํฉ๋๋ค. -
moduleย : ์ปดํ์ผ๋ ์ฝ๋๊ฐ ์ด๋ค ๋ชจ๋์์คํ
์ ์ฌ์ฉํ ์ง ์ ์ํฉ๋๋ค. ์ด ๊ฐ์ ๋ง์ฝย
common
์ผ๋ก ํ๋ฉดยexport default Sample
ย ์ฝ๋๋ยexports.default = Sample
๋ก ๋ณํ๋์ง๋ง ๊ฐ์ยes2015
๋ก ํ๋ฉดยexport default Sample
์ ๊ทธ๋๋ก ์ ์งํฉ๋๋ค. - strictย : ๋ชจ๋ ํ์ ์ฒดํน ์ต์ ์ ํ์ฑํํฉ๋๋ค.
- esModuleInterop :ย commonjs ๋ชจ๋ ํํ๋ก ์ด๋ค์ง ํ์ผ์ es2015 ๋ชจ๋ ํํ๋ก ๋ถ๋ฌ์ฌ ์ ์๊ฒ ํด์ค๋๋ค.
- outDirย : ์ปดํ์ผ๋ ํ์ผ์ด ์ด๋์ ์ ์ฅ๋ ์ง ๊ฒฝ๋ก๋ฅผ ์ ํฉ๋๋ค.
- includeย : ์ด๋ค ํ์ผ์ ์ปดํ์ผํ ๊ฒ์ธ์ง ์ ํฉ๋๋ค.
- excludeย : ์ด๋ค ํ์ผ์ ์ปดํ์ผ์์ ์ ์ธํ ์ง ์ ํฉ๋๋ค.
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
์ฌ๊ธฐ์ ์ฐ๋ฆฌ๋ ๊ธ๋ก๋ฒ๋ก ์ค์นํ ํ์
์คํฌ๋ฆฝํธ CLI๋ฅผ ํตํด ์ฝ๋๋ฅผ ์ปดํ์ผ ํ๋๋ฐ
๋ง์ฝ ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ๋ด์์ ์ค์นํ typescript ํจํค์ง๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํ์ผํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํฉ์๋ค.
(์ฌ์ค ์ผ๋ฐ์ ์ผ๋ก๋ ๋ก์ปฌ๋ก ์ค์นํ ํ์
์คํฌ๋ฆฝํธ ํจํค์ง๋ฅผ ์ฌ์ฉํด์ ์ปดํ์ผํฉ๋๋ค.)
$ yarn add typescript // npm install --save typescript
package.json
{
"name": "ts-practice",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"typescript": "^3.9.6"
},
"scripts": {
"build": "tsc"
}
}
์ด๋ ๊ฒ ์ค์ ํ๋ฉดย build
ย ์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค์ด์ ํธ๋ฆฌํ๊ฒ ์ปดํ์ผํ ์ ์์ต๋๋ค.
โ 2020. Project01-C-User-Event-Collector [J028_๊น๋๊ท , J089_๋ฐ์งํ, J139_์ด์๊ฒฝ, S059_์ต๊ดํ, S060_์ต๋๊ท] all rights reserved.