๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
React

React ํ”„๋กœ์ ํŠธ githubPages๋กœ ๋ฐฐํฌํ•˜๊ธฐ

by vividmin 2022. 6. 20.
320x100

 

ํ”„๋กœ์ ํŠธ๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋„ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐํฌํ•˜๋Š” ์‚ฌ์ดํŠธ(๊นƒํ—™ ํŽ˜์ด์ง€, netlify, aws...)๋Š” ์—ฌ๋Ÿฌ ๊ณณ์ด ์žˆ์ง€๋งŒ, ๊ทธ์ค‘์—์„œ๋„ githubPages๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” githubPages์˜ ์žฅ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • github์„ ์ด์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๋ณ„๋„๋กœ ์‚ฌ์ดํŠธ ๊ฐ€์ž…ํ•„์š” ์—†์Œ๐Ÿ™…๐Ÿป‍โ™€๏ธ
  • ๋„๋ฉ”์ธ์„ ๊ตฌ์ž…ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ๋ฌด๋ฃŒ!๐Ÿ˜ƒ

Github์—์„œ ๋ธ”๋กœ๊ทธ๋ฅผ ์šด์˜ํ•  ๋•Œ๋„ githubPages๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๊ฐœ์ธ์ ์œผ๋กœ ๋ธ”๋กœ๊ทธ๋Š” ๊ทธ ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ํ”Œ๋žซํผ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

githubPages๋Š” ๋ธ”๋กœ๊ทธ ์™ธ์—๋„ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์˜ repository๋ฅผ ์›นํŽ˜์ด์ง€๋กœ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ค๋Š˜์€ ๊ทธ ์ค‘์—์„œ๋„ React ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์ŠคํŒ…ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

โšก ํ”„๋กœ์ ํŠธ์™€ github์ด ์ด๋ฏธ ์—ฐ๋™๋˜์–ด ์žˆ๋‹ค๋ฉด 1, 2 ๊ณผ์ • ์ƒ๋žต ๊ฐ€๋Šฅ

 

1. github์— ์ƒˆ๋กœ์šด repository ๋งŒ๋“ค๊ธฐ

 

 

2. ํ”„๋กœ์ ํŠธ์™€ Github ์—ฐ๋™ํ•˜๊ธฐ

echo "# react-test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ ์ฃผ์†Œ
git push -u origin master

  • commit, push ์™„๋ฃŒ๋œ ๊ฒƒ ํ™•์ธ

 

3. ๋ณธ๊ฒฉ githubPages ๋ฐฐํฌ ์ค€๋น„ํ•˜๊ธฐ
  • githubpage ํŒจํ‚ค์ง€ ์„ค์น˜
npm install gh-pages --save-dev
  • package.json ํŒŒ์ผ์— ํ™ˆํŽ˜์ด์ง€ ์ฃผ์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ

"homepage": "<http://์‚ฌ์šฉ์ž์ด๋ฆ„.github.io/๋ ˆํŒŒ์ง€ํ† ๋ฆฌ์ด๋ฆ„>"
  • script ๋ถ€๋ถ„์— deploy, predeply ์ถ”๊ฐ€ํ•˜๊ธฐ

"scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  },

 

๐Ÿ“ ์ถ”๊ฐ€ ์‚ฌํ•ญ

  • githubPages๋กœ ๊ทธ๋ƒฅ ๋ฐฐํฌ ์‹œ ์ƒˆ๋กœ ๊ณ ์นจ ํ•˜๋ฉด 404 ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
  • 404 ํŽ˜์ด์ง€๊ฐ€ ๋œจ์ง€ ์•Š๋„๋ก 404ํŽ˜์ด์ง€๊ฐ€ index.html ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์ค€๋‹ค.
  • postbuild๋ฅผ ์ด์šฉํ•˜์—ฌ build ๋˜๊ธฐ ์ „ ์ž๋™์œผ๋กœ index.htmlํŽ˜์ด์ง€๊ฐ€ 404.htmlํŽ˜์ด์ง€๋กœ ๋ณต์‚ฌ๋˜๋„๋ก ์„ค์ •ํ•ด์ค€๋‹ค.
"scripts": {
  "postbuild": "cp build/index.html build/404.html",
}

 

 

4. ๋ฐฐํฌํ•˜๊ธฐ
npm run deploy
  • ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž๋™์œผ๋กœ build → 404.html ๋ณต์‚ฌ → gh-page์— build ํ›„ ๋ฐฐํฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

 

 

5. ๋ฐฐํฌ ํ™•์ธ
  • repository์— github-pages active ํ‘œ์‹œ ํ™•์ธ

  • ํ™ˆํŽ˜์ด์ง€ ์ฃผ์†Œ๋กœ ์ด๋™ํ•ด ๋ณด๊ธฐ
    • ์ฃผ์†Œ๋Š” ์•„๋ž˜ ํŽ˜์ด์ง€์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ

  • ์ฃผ์†Œ๋กœ ์ด๋™ ์‹œ ์ •์ƒ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ ํ™•์ธ

  • ์ƒˆ๋กœ ๊ณ ์นจ ์‹œ์—๋„ ์ •์ƒ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ ํ™•์ธ
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€