๐Ÿ’ป์›น(Web)/React

[React]Netlify๋กœ React ๋ฐฐํฌํ•˜๊ธฐ: ์ƒˆ๋กœ๊ณ ์นจ, API ์—ฐ๊ฒฐ ์—๋Ÿฌ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

stonesy 2023. 9. 1. 11:44
728x90

์กธ์—… ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ์—ˆ์„ ๋•Œ๋Š” ์‹œ๊ฐ„์  ์ œ์•ฝ์œผ๋กœ ์ธํ•ด์„œ ๋ฐฐํฌ๊นŒ์ง€ ๋งˆ์น˜์ง€ ๋ชปํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋๋ƒˆ์—ˆ๋‹ค. ๋กœ์ปฌ์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ž˜ ๋™์ž‘ํ•˜์ง€๋งŒ ๋ฐฐํฌํ•˜๋ฉด ๋™์ž‘ํ•˜์ง€ ์•Š์„ ๋•Œ๊ฐ€ ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค. ์ด๋ฒˆ ์ฐฝ์—… ๋™์•„๋ฆฌ ํ™œ๋™์„ ํ•˜๋ฉด์„œ๋„ ๋กœ์ปฌ์—์„œ๋Š” ์ž˜ ๋™์ž‘ํ–ˆ์—ˆ๋Š”๋ฐ, ๋ฐฐํฌ๋ฅผ ํ•˜๋ฉด์„œ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„์™€ API ์—ฐ๊ฒฐ์ด ์ œ๋Œ€๋กœ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์—ˆ๋‹ค. ๋ฐฐํฌ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋„ ํ•ด๋ณผ๊ฒธ ๊ฒธ์‚ฌ๊ฒธ์‚ฌ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค๐Ÿ™Œ๐Ÿป

 

๐Ÿ”ŽNetlify

์ด๋ฒˆ ์ฐฝ์—… ๋™์•„๋ฆฌ ์ค‘๊ฐ„ ํ”„๋กœ์ ํŠธ๋Š” Netlify๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐํฌํ–ˆ๋‹ค. Netlify๋Š” ์ •์  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐ ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(SPA) ๋ฐฐํฌ์— ํŠนํ™”๋˜์–ด ์žˆ๋Š” ์„œ๋น„์Šค์ด๋‹ค.

 

๐Ÿ”ŽNetlify๋ฅผ ํ†ตํ•œ React ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ ๋ฐฉ๋ฒ•

Netfliy๋ฅผ ํ†ตํ•œ React ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. React ํ”„๋กœ์ ํŠธ build

React ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  React ํ”„๋กœ์ ํŠธ๋ฅผ build ํ•ด์•ผ ํ•œ๋‹ค. build(๋นŒ๋“œ)๋ž€ ์†Œ์Šค ์ฝ”๋“œ ํŒŒ์ผ์„ ์ปดํ“จํ„ฐ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋…๋ฆฝ์ ์ธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •๊ณผ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋งํ•œ๋‹ค. ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” HTML, CSS, JavaScript๋งŒ ํ•ด์„ํ•˜๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” React์˜ JSX ๋ฌธ๋ฒ•์„ ์ดํ•ดํ•  ์ˆ˜ ์—†๋Š”๋ฐ, React์—์„œ build(๋นŒ๋“œ)๋ž€ JSX ํŒŒ์ผ์„ HTML ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. React ํ”„๋กœ์ ํŠธ๋Š” build ๊ณผ์ •์„ ํ†ตํ•ด ๊ฐœ๋ฐœ ์ค‘์ธ ์ฝ”๋“œ์™€ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ตœ์ ํ™”๋œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜๋˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์„ฑ๋Šฅ ์ตœ์ ํ™”์™€ ๋ฐฐํฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

React ํ”„๋กœ์ ํŠธ๋ฅผ build(๋นŒ๋“œ)ํ•˜๊ธฐ ์œ„ํ•ด์„œ npm run build ๋˜๋Š” yarn build ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋‚˜๋ฉด ํด๋”์— build ํด๋”๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. React ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด build ํด๋”์˜ ๋‚ด์šฉ๋ฌผ์ด ํ•„์š”ํ•˜๋‹ค.

npm run build
yarn build

 

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

Deploy manually์—์„œ ์œ„์—์„œ ์œ„์˜ ๋‹จ๊ณ„์—์„œ ์–ป์€ build ํด๋”๋ฅผ ์˜ฌ๋ฆฌ๋ฉด ๋œ๋‹ค.

 

3. ์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๊ธฐ

๐Ÿ› ๏ธNetlify์—์„œ ์ƒˆ๋กœ๊ณ ์นจ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ: Page Not Found ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ

์šฐ๋ฆฌ๋Š” create-react-app ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด React ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด ํ”„๋กœ์ ํŠธ๋Š” SPA(Single Page Application)์ด๋‹ค. SPA๋Š” ํ•˜๋‚˜์˜ index.html ์œ„์— ์—ฌ๋Ÿฌ ํ™”๋ฉด์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ํ˜•ํƒœ๋ฅผ ๋งํ•œ๋‹ค. ์ฆ‰, SPA๋Š” ์ตœ์ดˆ์— ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ index.html๊ณผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋งŒ ์‚ฌ์šฉํ•ด ํ™”๋ฉด์„ ์ „ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์„œ๋ฒ„์— ์ถ”๊ฐ€์ ์ธ ์›น ๋ฌธ์„œ๋ฅผ ์š”์ฒญํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”์—†๋‹ค. ์ด๋Ÿฌํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์œผ๋กœ ์ธํ•ด React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ฃผ์†Œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํ™”๋ฉด์„ ์ „ํ™˜ํ•  ๋•Œ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์š”์ฒญํ•˜๋Š” ๊ณผ์ •์ด ์—†์–ด์ง€๋ฏ€๋กœ ํ™”๋ฉด ์ „ํ™˜ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋กœ ์ธํ•ด Netlify๋กœ ๋ฐฐํฌ๋œ React ํ”„๋กœ์ ํŠธ์—์„œ ์ƒˆ๋กœ๊ณ ์นจ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. React ํ”„๋กœ์ ํŠธ์— ์ฒ˜์Œ ์ ‘์†ํ•  ๋•Œ์˜ url์€ /์ด๊ณ , ์ด๋•Œ index.html์„ ์ฝ๋Š”๋‹ค. ํ•˜์ง€๋งŒ ํ™”๋ฉด์„ ๋„˜๊ธฐ๋ฉด์„œ ๋ผ์šฐํŒ…๋œ url์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐ๊ฐ์˜ url์— ๋งž๋Š” ์ž์›์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋กœ ์ธํ•ด ๋ผ์šฐํŒ…๋œ url์—์„œ ์ƒˆ๋กœ๊ณ ์นจํ•œ ๊ฒฝ์šฐ /์™€ ๋‹ฌ๋ฆฌ ๊ณง๋ฐ”๋กœ ๋‚ด๋ ค์ค„ ์ž์›์ด ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. public/_redirects

public ํด๋” ๋‚ด๋ถ€์— _redirects ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค. ์ด ์ฝ”๋“œ์˜ ์˜๋ฏธ๋Š” ์–ด๋– ํ•œ ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์™€๋„ index.html์„ ๋ Œ๋”๋ง ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

/* /index.html 200

2. public/netlify.toml

์œ„ ๋ฐฉ๋ฒ•์ด ์•ˆ๋œ๋‹ค๋ฉด public ํด๋” ๋‚ด๋ถ€์— netlify.toml ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•ˆ์˜ ๋‚ด์šฉ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

[[redirects]]
    from = "/*"
    to = "/index.html"
    status = 200

 

๐Ÿ› ๏ธNetlify์—์„œ ํ”„๋ก์‹œ ์—๋Ÿฌ ํ•ด๊ฒฐํ•˜๊ธฐ: API ํ˜ธ์ถœ ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ

๊ฐœ๋ฐœ์ค‘์ผ ๋•Œ๋„ cors ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์—ˆ๋Š”๋ฐ, Netlify ๋ฐฐํฌ ํ›„์—๋„ ์š”์ฒญ url์ด ๋ฐ”๋€Œ๋ฉด์„œ ๋™์ผํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค. Netlify์—์„œ cors ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๋กœ์ปฌ ์„œ๋ฒ„์ผ ๊ฒฝ์šฐ ์„œ๋ฒ„ ์ฃผ์†Œ๋กœ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” api ์ฃผ์†Œ ์•ž์— api๊ฐ€ ๋ถ™๊ฒŒ ํ•œ๋‹ค. ์ดํ›„ axios ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ๋Š” ์„œ๋ฒ„ ์ฃผ์†Œ๋Š” ์ƒ๋žตํ•˜๊ณ  endpoint๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

const host =
  window.location.hostname === "localhost"
    ? `${์„œ๋ฒ„์ฃผ์†Œ}/`
    : "api";

export const apiClient = axios.create({
  baseURL: host,
});

/*์š”์ฒญ*/
const response = await apiClient.get(
	"content/platform/all
);
/*public/netlify.toml*/

[[redirects]]
  from = "/api/*"
  to = "<https://url์ฃผ์†Œ/:splat>"
  status = 200
  force = true
  • from: redirection ํ•˜๊ธฐ ์ „ ๊ฒฝ๋กœ
  • to: redirection ํ•˜๋ ค๋Š” URL ๋˜๋Š” ๊ฒฝ๋กœ
  • status: ์‚ฌ์šฉํ•  HTTP ์ƒํƒœ ์ฝ”๋“œ
  • force: ๊ฒฝ๋กœ์˜ ๊ธฐ์กด ๋‚ด์šฉ์„ ์žฌ์ •์˜ํ• ์ง€ ์—ฌ๋ถ€(default ๋Š” false)

 

์ด๋•Œ, ์ƒˆ๋กœ๊ณ ์นจ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด public/redirects_ ํŒŒ์ผ์„ ์„ค์ •ํ•˜๊ณ , API ์—ฐ๊ฒฐ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด public/netlify.toml ํŒŒ์ผ์„ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ๋‹ค์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” /public/_redirects ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ  /public/netlify.toml ํŒŒ์ผ์— ์„ค์ •์„ ํ•ฉ์ณ์คŒ์œผ๋กœ์จ ํ•ด๊ฒฐํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ”ŽNetlify๋ฅผ ํ†ตํ•œ React ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ ๋ฐฉ๋ฒ•2: github ์—ฐ๋™

์œ„์˜ 2๋ฒˆ ๋‹จ๊ณ„์—์„œ ์ง์ ‘ build ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ github repository์™€ ์—ฐ๋™ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Add new site>Import an existing project>Deploy with GitHub๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด Basic build setting์„ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

-Branch to deploy: ํ”„๋กœ์ ํŠธ Repository์˜ ๋ฐฐํฌํ•  ๋ธŒ๋žœ์น˜

-Base directory: reposiory์•ˆ์— frontend, backend, … ๋“ฑ ํ”„๋ก ํŠธ์™€ ๋ฐฑ์„ ๋‘˜๋‹ค ๊ด€๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐฐํฌํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋”๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค. ๋‚˜์˜ ๊ฒฝ์šฐ repository ์•ˆ์— ํ”„๋ก ํŠธ์—”๋“œ ์ฝ”๋“œ๋งŒ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋นˆ์นธ์œผ๋กœ ๋น„์›Œ๋‘์—ˆ๋‹ค.

-Build command: ๋นŒ๋“œ ๋ช…๋ น์–ด, ์œ„์˜ 1๋ฒˆ ๋‹จ๊ณ„์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ npm run build๋‚˜ yarn build ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๊ณ  ํ•˜์˜€๋Š”๋ฐ, ์ด์™€ ๊ฐ™์€ ๋นŒ๋“œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

-Publish directory: ๋นŒ๋“œ๊ฐ€ ์™„๋ฃŒ๋œ ํ›„ ๋นŒ๋“œ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ํด๋” ์ด๋ฆ„

 

(๋นŒ๋“œ ๊ฒฐ๊ณผ)

 

๊ทผ๋ฐ ์œ„์™€ ๊ฐ™์ด deploy ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋”๋‹ˆ build ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค. 2020๋…„๋ถ€ํ„ฐ๋Š” ๊ธฐ์กด์˜ npm run build ์ปค๋งจ๋“œ ์•ž์— “CI= “๋ฅผ ๋ถ™์–ด์•ผ ์ž‘๋™ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. build command ๋ถ€๋ถ„์„ CI= npm run build๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค. ์ด๋•Œ ๋ฐ˜๋“œ์‹œ “CI= “์™€ ๊ฐ™์ด CI= ๋’ค์— ๊ณต๋ฐฑ์„ ๋ถ™์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

https://answers.netlify.com/t/5-35-pm-failed-during-stage-building-site-build-script-returned-non-zero-exit-code-1/17925

 

5:35 PM: Failed during stage 'building site': Build script returned non-zero exit code: 1

I need advice to debug deploy Netlify App below is my deploy Log Kindly help me i have looked for solution everywhere 6:07:08 PM: Build ready to start 6:07:10 PM: build-image version: 9d79ad851d6eff3969322d6e5b1df3d597650c41 6:07:10 PM: build-image tag: v3

answers.netlify.com

 

(๋นŒ๋“œ ๊ฒฐ๊ณผ) ์„ฑ๊ณต!!

 

728x90