์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- fly.io
- naver api
- hooks
- touch event
- ๋ฐ์ํ
- node.js
- gh-pages
- php
- npm-check-updates
- bucket
- ํด๋ฆฐ์ฝ๋
- multer-s3
- ๋ฐฐํฌ
- Next.js
- s3
- dependencies
- AWS
- react
- qoddi
- createRoot
- CSS
- aws-sdk
- heroku
- package.json
- react18
- dependabot
- .eslintrc
- Web Storage
- GitHub
- Github Pages
- Today
- Total
Ann's log
[React] Github Pages๋ก ๋ฐฐํฌํ๊ณ ์๋ก๊ณ ์นจ์ 404 ์๋ฌ ํด๊ฒฐ ๋ณธ๋ฌธ
[React] Github Pages๋ก ๋ฐฐํฌํ๊ณ ์๋ก๊ณ ์นจ์ 404 ์๋ฌ ํด๊ฒฐ
-Ann- 2022. 5. 30. 17:57
๐ฑ ๋ฌธ์
GitHub Pages๋ก ๋ฐฐํฌํ React ํ์ด์ง์์ ์๋ก๊ณ ์นจ์ ํ๋ฉด 404 ์๋ฌ๊ฐ ๋ฌ๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ค.
Homepage์์๋ ์๋ก๊ณ ์นจํด๋ ์๋ฌด๋ฐ ๋ฌธ์ ๊ฐ ์๋๋ฐ, ์๋ธ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๊ณ ์๋ก๊ณ ์นจ์ ํ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค..
์ด์ ๋ฅผ ์ฐพ์๋ณด๋ ๊นํ๋ธ ํ์ด์ง๋ SPA(Single Page Applications)๋ฅผ ์ธ์ํ์ง ๋ชปํ๋ ๋ฌธ์ ๊ฐ ์๋ค๊ณ ํ๋ค!
SPA๋ ์ผ๋ฐ์ ์ผ๋ก ์น ๋ธ๋ผ์ฐ์ ์์ ์ก์ธ์ค ํ ์ ์๋ ํ๋์ index.html ํ์ผ๋ง ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, Github Pages๋ก ๋ฐฐํฌ๋ฅผ ํ๊ฒ ๋๋ฉด homepage url ํ๋๋ง ์ธ์ํ๊ฒ ๋๋ค.
๊ทธ๋์ hompage url ๋ค์ ๊ฒฝ๋ก๊ฐ ๋ ๋ถ์ผ๋ฉด ๊ทธ ๊ฒฝ๋ก์์ ํด๋น ํ์ด์ง์ ํ์ผ์ ์ฐพ์ผ๋ ค ํ๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
๊ตฌ๊ธ์ ๊ฒ์ํด ๋ณด๋๊น ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์๋ ๊ฒ ๊ฐ๋ค.
Create React App ๋ฌธ์์์๋ 2๊ฐ์ง ๋ฐฉ๋ฒ์ ๋ํด ์๋ ค์ฃผ๊ณ ์๋๋ฐ, ๋๋ 404.html์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์จ๋ณด๋ ค ํ๋ค.
404.html์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
CRA ๋ฌธ์์์๋ spa-github-pages ๊ฐ์ด๋๋ฅผ ์ฐธ๊ณ ํ๋ผ๊ณ ๋์์๋ค.
(์ด ๊ธ์ ํ๊ตญ์ด๋ก ๋ฒ์ญํด ์ฃผ์ ๊นํ๋ธ ์ ์ฅ์๊ฐ ์์ด์ ์ด ๊ธ์ ์ฐธ๊ณ ํ์๋ค.)
์ด ๋ฐฉ๋ฒ์ ์ ์ ์๋ ๊ฒฝ๋ก์์๋ 404.html ํ์ด์ง๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ ์ด์ฉํ์ฌ 404 ํ์ด์ง๋ฅผ ์ฐํํ๋ ๋ฐฉ๋ฒ์ด๋ค.
404.html ํ์ผ์ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํด์ query์ hash๋ก ๊ตฌ์ฑ๋ ์๋ก์ด URL์ ๋ฐํํ์ฌ redirectionํ๊ฒ ๋๋ค.
๋ง์ฝ์ URL์ด example.tld/one/two?a=b&c=d#qwe์ธ ๊ฒฝ์ฐ, example.tld/?p=/one/two&q=a=b~and~c=d#qwe๋ก ๋ณ๊ฒฝ๋๋ค๊ณ ํ๋ค.
public/404.html
1. React ํ๋ก์ ํธ ํด๋์ public ํด๋ ์์ 404.html ํ์ผ์ ์์ฑํ๋ค.
2. ๋ค์ ๋ด์ฉ์ ๊ทธ๋๋ก ๋ณต์ฌํด์ ๋ถ์ฌ ๋ฃ๋๋ค.
(์ถ์ฒ: https://github.com/rafgraph/spa-github-pages/blob/gh-pages/404.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Single Page Apps for GitHub Pages</title>
<script type="text/javascript">
// Single Page Apps for GitHub Pages
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script takes the current url and converts the path and query
// string into just a query string, and then redirects the browser
// to the new url with only a query string and hash fragment,
// e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
// Note: this 404.html file must be at least 512 bytes for it to work
// with Internet Explorer (it is currently > 512 bytes)
// If you're creating a Project Pages site and NOT using a custom domain,
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
// This way the code will only replace the route part of the path, and not
// the real directory in which the app resides, for example:
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
// Otherwise, leave pathSegmentsToKeep as 0.
var pathSegmentsToKeep = 0;
var l = window.location;
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
l.hash
);
</script>
</head>
<body>
</body>
</html>
์ด ํ์ผ์ 404.html์ด ๋ฐํ๋ ๋ ํ์ฌ URL์ ์๋ก์ด URL๋ก ๋ณ๊ฒฝ์ํค๊ณ ๋ธ๋ผ์ฐ์ ๋ฅผ redirect ํด์ฃผ๋ script๋ฅผ ํฌํจํ๊ณ ์๋ค.
๋ง์ฝ ์ปค์คํ ๋๋ฉ์ธ์ ์ฌ์ฉํ์ง ์๊ณ username.github.io/repo-name ํ์์ ์ฃผ์๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ, pathSegmentsToKeep ๋ณ์์ ๊ฐ์ 0์ด ์๋ 1๋ก ๋ฐ๊ฟ์ค์ผ ํ๋ค!
public/index.html
1. public ํด๋ ์์ index.html ํ์ผ์ </head> ํ๊ทธ ๋ฐ๋ก ์ ์ ๋ค์ ๋ด์ฉ์ ์ถ๊ฐํ๋ค.
(์ถ์ฒ: https://github.com/rafgraph/spa-github-pages/blob/gh-pages/index.html#L58)
<!-- Start Single Page Apps for GitHub Pages -->
<script type="text/javascript">
// Single Page Apps for GitHub Pages
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script checks to see if a redirect is present in the query string,
// converts it back into the correct url and adds it to the
// browser's history using window.history.replaceState(...),
// which won't cause the browser to attempt to load the new url.
// When the single page app is loaded further down in this file,
// the correct url will be waiting in the browser's history for
// the single page app to route accordingly.
(function(l) {
if (l.search[1] === '/' ) {
var decoded = l.search.slice(1).split('&').map(function(s) {
return s.replace(/~and~/g, '&')
}).join('?');
window.history.replaceState(null, null,
l.pathname.slice(0, -1) + decoded + l.hash
);
}
}(window.location))
</script>
<!-- End Single Page Apps for GitHub Pages -->
์ด script๋ ์ฟผ๋ฆฌ ์คํธ๋ง์ ๋ฆฌ๋ค์ด๋ ํธ๊ฐ ์กด์ฌํ๋์ง ํ์ธํด์ ๋ค์ ์ฌ๋ฐ๋ฅธ URL๋ก ๋ณํ์ํค๋ ์ญํ ์ ํ๋ค.
๊ทธ๋ฆฌ๊ณ window.history.replaceState()๋ฅผ ์ฌ์ฉํ์ฌ ๋ธ๋ผ์ฐ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ฒ ๋๋๋ฐ, ๋ธ๋ผ์ฐ์ ๋ ์๋ก์ด URL์ ๋ก๋ํ์ง๋ ์๋๋ค๊ณ ํ๋ค.
MDN ๋ฌธ์๋ฅผ ์ฐพ์๋ณด๋, replaceState()๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด URL ํ์์ค์๋ ์๋ก์ด URL์ด ํ์ ๋์ง๋ง, ๋ธ๋ผ์ฐ์ ๊ฐ ๊ทธ ํ์ผ์ด ์๋์ง ํ์ธํ์ง๋ ์๋๋ค๊ณ ํ๋ค.
2. ํ๋ก์ ํธ๋ฅผ Github Pages๋ก ์ฌ๋ฐฐํฌ ํ๋ค.
ํ ์คํธ ํด๋ณด๋๊น ์ด์ ๋ ์๋ก๊ณ ์นจ ํด๋ 404 ํ์ด์ง๊ฐ ๋ณด์ด์ง ์๊ณ ์ ์์ ์ผ๋ก ์๋ํ๊ฒ ๋์๋ค!! ๐
๋น๋ก ๋ด๊ฐ ์ง์ ์ฝ๋๋ฅผ ์์ฑํด์ ํด๊ฒฐํ๊ฑด ์๋์ง๋ง, ์ด๋ฒ์ ์ฌ์ฉํ ์ฝ๋์ ๋ํด ๊ณต๋ถํด์ ๋์ค์ ํ์ฉํ ์ ์๋๋ก ํด๋ด์ผ๊ฒ ๋ค.
์ฐธ๊ณ :
https://chinsun9.github.io/2021/07/30/serve-react-app-simply/
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] sessionStorage ์ฌ์ฉํ๊ธฐ (0) | 2022.06.20 |
---|---|
[React] npm-check-updates ์ฌ์ฉํ๊ธฐ (0) | 2022.06.07 |
[React] Naver ์ง์ญ API ์ฌ์ฉํ๊ธฐ (0) | 2022.04.30 |
[React] GitHub Pages๋ก ๋ฐฐํฌํ๊ธฐ (0) | 2022.04.15 |
[React] security vulnerabilities ๋ณด์ํ๊ธฐ + GitHub Dependabot alerts ์ ๊ฑฐํ๊ธฐ (0) | 2022.04.13 |