[πŸŽμ½”λ”©μ• ν”Œ κ°•μ˜μš”μ•½]React μ‚¬μ΄νŠΈ build 및 GitHub Pages λ°°ν¬ν•˜κΈ°

🌈 KJΒ·2025λ…„ 5μ›” 17일

codingapple

λͺ©λ‘ 보기
14/23
post-thumbnail

λΉŒλ“œ 및 배포의 ν•„μš”μ„± 🧩

μ™œ λΉŒλ“œκ°€ ν•„μš”ν•œκ°€μš”? πŸ“‹

  • μ›ΉλΈŒλΌμš°μ €λŠ” HTML, CSS, JS만 해석 κ°€λŠ₯ πŸ’»
  • React의 JSX, state 등은 λΈŒλΌμš°μ €κ°€ 직접 이해할 수 μ—†μŒ ❌
  • λΉŒλ“œ 과정을 톡해 λΈŒλΌμš°μ € μΉœν™”μ μΈ 파일둜 λ³€ν™˜ ν•„μš” πŸ”„
  • λ³€ν™˜λœ νŒŒμΌμ„ μ„œλ²„μ— μ˜¬λ €μ•Ό μ‚¬μš©μžκ°€ μ ‘κ·Ό κ°€λŠ₯ 🌐

배포 μ „ 체크사항 βœ…

ν”„λ‘œμ νŠΈ 확인 πŸ“

  • μ½˜μ†”/터미널에 μ—λŸ¬κ°€ μ—†μ–΄μ•Ό 함 (κ²½κ³ λŠ” λ¬΄μ‹œ κ°€λŠ₯) ⚠️
  • ν•˜μœ„ 경둜 배포 μ‹œ μΆ”κ°€ μ„€μ • ν•„μš” πŸ“‚

ν•˜μœ„ 경둜 배포 μ„€μ • πŸ› οΈ

create-react-app ν”„λ‘œμ νŠΈ

// package.json
{
  "homepage": "/blog", // 배포할 경둜 μΆ”κ°€
  // 기타 μ„€μ •...
}
  • React Router μ‚¬μš© μ‹œ basename 속성 μΆ”κ°€ ꢌμž₯ πŸ”„

Vite ν”„λ‘œμ νŠΈ

// vite.config.js
export default defineConfig({
  // 기타 μ„€μ •...
  base: '/blog' // λ˜λŠ” base: './'
})
  • /blog/ κ²½λ‘œλŠ” μ ˆλŒ€ 경둜, ./λŠ” μƒλŒ€ 경둜둜 CSS 파일 μ°Έμ‘° πŸ”—

λΉŒλ“œ μž‘μ—… κ³Όμ • πŸ—οΈ

λΈŒλΌμš°μ € ν˜Έν™˜ 파일 생성 πŸ”„

  1. ν„°λ―Έλ„μ—μ„œ ν”„λ‘œμ νŠΈ ν΄λ”λ‘œ 이동
  2. npm run build λͺ…λ Ήμ–΄ μ‹€ν–‰
  3. dist λ˜λŠ” build 폴더에 λ³€ν™˜λœ 파일 생성
  4. index.html이 메인 νŽ˜μ΄μ§€ πŸ“„

GitHub Pages둜 λ°°ν¬ν•˜κΈ° πŸš€

리포지토리 생성 πŸ“

  1. GitHub 둜그인 ν›„ New Repository 클릭
  2. Repository 이름을 μ‚¬μš©μžλͺ….github.io둜 μ„€μ • (μ€‘μš”!) πŸ“›
  3. README 파일 생성 체크 ν›„ 리포지토리 생성

λΉŒλ“œ 파일 μ—…λ‘œλ“œ πŸ“€

  1. μƒμ„±λœ λ¦¬ν¬μ§€ν† λ¦¬λ‘œ 이동
  2. dist λ˜λŠ” build 폴더 λ‚΄λΆ€ νŒŒμΌμ„ λ“œλž˜κ·Έμ•€λ“œλ‘­μœΌλ‘œ μ—…λ‘œλ“œ (폴더 μžμ²΄κ°€ μ•„λ‹Œ λ‚΄μš©λ¬Όλ§Œ!)
  3. μ΄ˆλ‘μƒ‰ Commit λ²„νŠΌ 클릭
  4. μ•½ 5λΆ„ ν›„ https://μ‚¬μš©μžλͺ….github.io둜 μ ‘μ†ν•˜μ—¬ 배포 확인 🌐

λ‹€λ₯Έ μ΄λ¦„μ˜ 리포지토리 μ‚¬μš©ν•˜κΈ° (선택사항) πŸ”„

  1. λΉŒλ“œ μ „ ν•˜μœ„ 경둜 μ„€μ • (0번 ν•­λͺ© μ°Έκ³ )
  2. μƒˆ 리포지토리 생성 (이름 자유둭게 μ„€μ •)
  3. λΉŒλ“œ 파일 μ—…λ‘œλ“œ
  4. 리포지토리 Settings β†’ Pagesμ—μ„œ Sourceλ₯Ό Noneμ—μ„œ main으둜 λ³€κ²½
  5. μ‚¬μš©μžλͺ….github.io/λ¦¬ν¬μ§€ν† λ¦¬μ΄λ¦„μœΌλ‘œ 접속 κ°€λŠ₯

자주 λ¬»λŠ” 질문 (FAQ) ❓

404 였λ₯˜κ°€ λ°œμƒν•  λ•Œ 🚫

  • 배포 ν›„ 5λΆ„ 이상 기닀렀보기 ⏱️
  • Ctrl + Shift + R둜 κ°•λ ₯ μƒˆλ‘œκ³ μΉ¨ μ‹œλ„ πŸ”„
  • 리포지토리 이름이 μ‚¬μš©μžλͺ….github.io ν˜•μ‹μΈμ§€ 확인 πŸ”

μ—…λ°μ΄νŠΈ 방법 πŸ”„

  • μ½”λ“œ μˆ˜μ • ν›„ λ‹€μ‹œ npm run build μ‹€ν–‰
  • μƒμ„±λœ νŒŒμΌμ„ λ‹€μ‹œ μ—…λ‘œλ“œν•˜λ©΄ 됨
  • λΉŒλ“œν•  λ•Œλ§ˆλ‹€ CSS, JS 파일λͺ…이 μžλ™ λ³€κ²½λ˜μ–΄ μΊμ‹œ 문제 ν•΄κ²° 🧹

파일 이름 μœ μ§€ν•˜κΈ° πŸ“‚

  • public 폴더에 넣은 νŒŒμΌμ€ λΉŒλ“œ 후에도 이름과 경둜 μœ μ§€
  • 개발 μ‹œ /경둜둜 import κ°€λŠ₯

일뢀 파일이 λ‘œλ“œλ˜μ§€ μ•Šμ„ λ•Œ πŸ”

  • λΉŒλ“œ μ—λŸ¬ μ—¬λΆ€ 확인
  • ν•˜μœ„ 폴더 배포 μ„€μ • 확인
  • 개발자 λ„κ΅¬μ—μ„œ 파일 경둜 확인

λΌμš°νŒ… κ΄€λ ¨ 404 였λ₯˜ πŸ”€

  • GitHub PagesλŠ” SPA λΌμš°νŒ…μ— μ œν•œμ΄ 있음
  • ν•΄κ²°μ±…:
    • μ„œλ²„ μ„€μ • (GitHub Pagesμ—μ„œλŠ” λΆˆκ°€λŠ₯) ❌
    • HashRouter μ‚¬μš© (# κΈ°ν˜Έκ°€ URL에 좔가됨) βœ…

μΆ”κ°€ 정보 및 팁 πŸ’‘

μ‚¬μš©μž μ •μ˜ 도메인 μ—°κ²°ν•˜κΈ° πŸ”—

  1. 도메인 μ œκ³΅μ—…μ²΄μ—μ„œ 도메인 ꡬ맀
  2. GitHub 리포지토리 β†’ Settings β†’ Pagesμ—μ„œ Custom domain μ„€μ •
  3. DNS μ œκ³΅μ—…μ²΄μ—μ„œ λ‹€μŒ μ„€μ • μΆ”κ°€:
    • A λ ˆμ½”λ“œ: @ β†’ 185.199.108.153 (GitHub IP)
    • CNAME λ ˆμ½”λ“œ: www β†’ μ‚¬μš©μžλͺ….github.io
  4. 리포지토리에 CNAME 파일 μΆ”κ°€ (λ‚΄μš©μ€ 도메인λͺ…λ§Œ)
  5. μ„€μ • μ™„λ£Œ ν›„ HTTPS 적용 체크

SPA λΌμš°νŒ… 문제 ν•΄κ²°ν•˜κΈ° ⚑

<!-- 404.html μ˜ˆμ‹œ -->
<!DOCTYPE html>
<html>
<head>
  <script>
    sessionStorage.redirect = location.href;
  </script>
  <meta http-equiv="refresh" content="0;URL='/'">
</head>
</html>

<!-- index.html에 μΆ”κ°€ -->
<script>
  (function(){
    let redirect = sessionStorage.redirect;
    delete sessionStorage.redirect;
    if (redirect && redirect != location.href) {
      history.replaceState(null, null, redirect);
    }
  })();
</script>
  • 이 λ°©μ‹μœΌλ‘œ SPA λΌμš°νŒ… 문제λ₯Ό ν•΄κ²°ν•  수 있음 (HashRouter λŒ€μ•ˆ) πŸ”€

ν™˜κ²½λ³€μˆ˜ ν™œμš©ν•˜κΈ° 🌿

// .env 파일
REACT_APP_API_URL=https://api.example.com

// .env.production 파일
REACT_APP_API_URL=https://production-api.example.com

// μ½”λ“œμ—μ„œ μ‚¬μš©
const apiUrl = process.env.REACT_APP_API_URL;
  • 개발/배포 ν™˜κ²½μ— 따라 λ‹€λ₯Έ μ„€μ • μ‚¬μš© κ°€λŠ₯ πŸ”„
  • API μ£Όμ†Œ, κΈ°λŠ₯ ν”Œλž˜κ·Έ 등을 ν™˜κ²½λ³„λ‘œ 관리 πŸ› οΈ

CI/CD μžλ™ν™” μ„€μ •ν•˜κΈ° πŸ€–

# .github/workflows/deploy.yml
name: Deploy to GitHub Pages

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm ci
      - run: npm run build
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: build # λ˜λŠ” dist
  • GitHub Actionsλ₯Ό ν™œμš©ν•œ μžλ™ 배포 μ„€μ •
  • main λΈŒλžœμΉ˜μ— ν‘Έμ‹œν•˜λ©΄ μžλ™μœΌλ‘œ λΉŒλ“œ 및 배포 πŸš€

0개의 λŒ“κΈ€