React 项目在本地主机上运行正常,但在 gh-pages 上为空白。

huangapple go评论89阅读模式
英文:

React projects works fine in local host, but is blank in gh-pages

问题

我正在处理一个React项目,它之前能够工作,但无法从正确的页面启动。为了解决这个问题,我决定将一个React组件重命名为与项目的GitHub仓库同名。这个组件之前叫做Notebook(现在叫做Note)。最初,gh-pages会从Notebook(现在叫Note)组件启动,而不是登录组件。然而,在从Notebook更名为Note之后,gh-pages现在从一个空白页面启动(什么都不加载),而当我在我的电脑上启动应用程序时,它却表现得非常出色。我已经在网上阅读了很多关于更改package.json文件的评论,但都没有成功。希望能够提供一些帮助!另外,请随时与我联系如果有任何问题。

package.json

  1. {
  2. "name": "notebook",
  3. "version": "0.1.0",
  4. "private": true,
  5. "homepage": "https://thewizard91.github.io/notebook/",
  6. "dependencies": {
  7. "@googlemaps/react-wrapper": "^1.1.18",
  8. "@reduxjs/toolkit": "^1.7.1",
  9. "@testing-library/jest-dom": "^5.16.1",
  10. "@testing-library/react": "^12.1.2",
  11. "@testing-library/user-event": "^13.5.0",
  12. "babel-preset-react": "^6.24.1",
  13. "bootstrap": "^5.2.1",
  14. "firebase": "^9.9.4",
  15. "gulp": "^3.9.1",
  16. "react": "^17.0.2",
  17. "react-bootstrap": "^2.5.0",
  18. "react-dom": "^17.0.2",
  19. "react-redux": "^7.2.6",
  20. "react-router-dom": "^6.2.1",
  21. "react-scripts": "^2.1.8",
  22. "redux": "^4.1.2",
  23. "redux-thunk": "^2.4.1",
  24. "semantic-ui-css": "^2.4.1",
  25. "semantic-ui-react": "^2.0.4",
  26. "serve": "^14.2.0",
  27. "styled-components": "^5.3.3",
  28. "web-vitals": "^2.1.2"
  29. },
  30. "scripts": {
  31. "predeploy": "npm run build",
  32. "deploy": "gh-pages -d build -b gh-pages",
  33. "start": "react-scripts start",
  34. "build": "react-scripts build",
  35. "test": "react-scripts test",
  36. "eject": "react-scripts eject",
  37. "heroku-postbuild": "npm run build"
  38. },
  39. "heroku-run-build-script": true,
  40. "eslintConfig": {
  41. "extends": [
  42. "react-app",
  43. "react-app/jest"
  44. ]
  45. },
  46. "browserslist": {
  47. "production": [
  48. ">0.2%",
  49. "not dead",
  50. "not op_mini all"
  51. ],
  52. "development": [
  53. "last 1 chrome version",
  54. "last 1 firefox version",
  55. "last 1 safari version"
  56. ]
  57. },
  58. "devDependencies": {
  59. "gh-pages": "^3.2.3"
  60. },
  61. "engines": {
  62. "node": "16.18.1",
  63. "npm": "8.19.2"
  64. }
  65. }
英文:

I am working on a react project that worked but would not start from the right page. To fix that, I decided to rename a react component with the same name as the gh repo of the project. That was the Notebook(now Note). Initially, gh-pages would start from the Notebook(now Note) component, not the Login component. However, after the name change from NOtebook to Note, now gh-pages starts at a blank page (it loads nothing), while it works wonders on my computer when I launch the app. I have read a lot of comments online about changes in the package.json file and did them, but unsuccessfully; any help would be appreciated! Also, please feel free to reach out to me with any questions.

package.json

  1. {
  2. "name": "notebook",
  3. "version": "0.1.0",
  4. "private": true,
  5. "homepage" : "https://thewizard91.github.io/notebook/",
  6. "dependencies": {
  7. "@googlemaps/react-wrapper": "^1.1.18",
  8. "@reduxjs/toolkit": "^1.7.1",
  9. "@testing-library/jest-dom": "^5.16.1",
  10. "@testing-library/react": "^12.1.2",
  11. "@testing-library/user-event": "^13.5.0",
  12. "babel-preset-react": "^6.24.1",
  13. "bootstrap": "^5.2.1",
  14. "firebase": "^9.9.4",
  15. "gulp": "^3.9.1",
  16. "react": "^17.0.2",
  17. "react-bootstrap": "^2.5.0",
  18. "react-dom": "^17.0.2",
  19. "react-redux": "^7.2.6",
  20. "react-router-dom": "^6.2.1",
  21. "react-scripts": "^2.1.8",
  22. "redux": "^4.1.2",
  23. "redux-thunk": "^2.4.1",
  24. "semantic-ui-css": "^2.4.1",
  25. "semantic-ui-react": "^2.0.4",
  26. "serve": "^14.2.0",
  27. "styled-components": "^5.3.3",
  28. "web-vitals": "^2.1.2"
  29. },
  30. "scripts": {
  31. "predeploy": "npm run build",
  32. "deploy": "gh-pages -d build -b gh-pages",
  33. "start": "react-scripts start",
  34. "build": "react-scripts build",
  35. "test": "react-scripts test",
  36. "eject": "react-scripts eject",
  37. "heroku-postbuild": "npm run build"
  38. },
  39. "heroku-run-build-script": true,
  40. "eslintConfig": {
  41. "extends": [
  42. "react-app",
  43. "react-app/jest"
  44. ]
  45. },
  46. "browserslist": {
  47. "production": [
  48. ">0.2%",
  49. "not dead",
  50. "not op_mini all"
  51. ],
  52. "development": [
  53. "last 1 chrome version",
  54. "last 1 firefox version",
  55. "last 1 safari version"
  56. ]
  57. },
  58. "devDependencies": {
  59. "gh-pages": "^3.2.3"
  60. },
  61. "engines": {
  62. "node": "16.18.1",
  63. "npm": "8.19.2"
  64. }
  65. }

答案1

得分: 0

当访问您的网页时,我看到许多网络请求返回404,特别是babel和firebase资源。

您可以检查一下这些资源是否已提交到gh-pages分支。

英文:

When visiting your web page, I see a lot of network requests return 404, particularly for babel and firebase resources.

Could you check that those resources were committed to the gh-pages branch.

答案2

得分: 0

我找到了一个便宜的解决方法。我注意到当我将项目部署到 gh-pages 时,应用的主体未加载。这意味着在启动应用程序时未加载<Routes></Routes>中的所有组件(仅在本地主机上有效)。因此,我在<Routes></Routes>段的顶部添加了一个导航栏,而在<Navigation />内,我有指向主页(登录页面)的链接(我打算使用该组件启动应用程序;因此,单击链接后我得到我想要的效果。以下是代码。请随时评论以获得进一步的澄清,也许我可以更清晰:)!

  1. class App extends Component {
  2. render() {
  3. return (
  4. <Container
  5. id="AppId"
  6. className="d-flex align-items-center justify-content-center">
  7. <div className="w-100"> {/*className = "w-100"*/}
  8. <Router>
  9. <Navigation/>
  10. <AuthProvider>
  11. <Routes>
  12. <Route exact path="/dashboard" element={<Dashboard />}/>
  13. {/* <Route exact path='/' element={<PrivateRoute/>} >
  14. <Route exact={true} path='/login' element={<LogIn/>}/>
  15. </Route> */}
  16. <Route path='/update-profile' element={<UpdateProfile/>}/>
  17. <Route path="/signup" element={<SignUp/>}/>
  18. <Route path="/login" exact={true} element={<LogIn/>}/>
  19. <Route path="/forgot-password" element={<ForgotPassword/>}/>
  20. <Route path="/note" element={<Note/>}/>
  21. <Route path="/mainpage" element={<MainPage/>}/>
  22. <Route path="/new" element={<New/>}/>
  23. </Routes>
  24. </AuthProvider>
  25. </Router>
  26. </div>
  27. </Container>
  28. );
  29. }
  30. }
  31. export default App;
英文:

I found a cheap fix. I noticed that when I deploy the project on gh-pages, the app's body is not loaded. Meaning all the components within the &lt;Routes&gt;&lt;/Routes&gt; were not loaded when you launched the app (only in the local host it works). So what I did was add a navbar on top of the &lt;Routes&gt;&lt;/Routes&gt; segment, and inside of the &lt;Navigation /&gt;, I have the link to the homepage (login page) (the component that I intended to start the app with; so upon the click to the link I get what I want. Here is the code. Please feel free to comment for further clarification, maybe I can be clearer:)!

  1. class App extends Component {
  2. render() {
  3. return (
  4. &lt;Container
  5. id = &quot;AppId&quot;
  6. className = &quot;d-flex align-items-center justify-content-center&quot;&gt;
  7. &lt;div className = &quot;w-100&quot;&gt; {/*className = &quot;w-100&quot;*/}
  8. &lt;Router&gt;
  9. &lt;Navigation/&gt;
  10. &lt;AuthProvider&gt;
  11. &lt;Routes&gt;
  12. &lt;Route exact path=&quot;/dashboard&quot; element={&lt;Dashboard /&gt;}/&gt;
  13. {/* &lt;Route exact path=&#39;/&#39; element={&lt;PrivateRoute/&gt;} &gt;
  14. &lt;Route exact={true} path=&#39;/login&#39; element={&lt;LogIn/&gt;}/&gt;
  15. &lt;/Route&gt; */}
  16. &lt;Route path=&#39;/update-profile&#39; element={&lt;UpdateProfile/&gt;}/&gt;
  17. &lt;Route path=&quot;/signup&quot; element={&lt;SignUp/&gt;}/&gt;
  18. &lt;Route path=&quot;/login&quot; exact={true} element={&lt;LogIn/&gt;}/&gt;
  19. &lt;Route path=&quot;/forgot-password&quot; element={&lt;ForgotPassword/&gt;}/&gt;
  20. &lt;Route path=&quot;/note&quot; element={&lt;Note/&gt;}/&gt;
  21. &lt;Route path = &quot;/mainpage&quot; element={&lt;MainPage/&gt;}/&gt;
  22. &lt;Route path = &quot;/new&quot; element={&lt;New/&gt;}/&gt;
  23. &lt;/Routes&gt;
  24. &lt;/AuthProvider&gt;
  25. &lt;/Router&gt;
  26. &lt;/div&gt;
  27. &lt;/Container&gt;
  28. );
  29. }
  30. }
  31. export default App;

huangapple
  • 本文由 发表于 2023年3月4日 07:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定