我有一个默认的“create-react-app”,可以在我的计算机上本地运行良好。我使用 Git 将其推送到 Gitlab 存储库,该存储库连接到 URL 上的 Gitlab Pages:https://reactwebsitetobetested-alexandreasen09-9e495b734d3272d04dc8ee99.gitlab.io/
我的 .yml 文件是:
# The Docker image that will be used to build your app
image: node:lts
# Functions that should be executed before the build script is run
before_script:
- npm ci
variables:
GITLAB_PAGES_URL: "https://reactwebsitetobetested-alexandreasen09-9e495b734d3272d04dc8ee99.gitlab.io/"
PUBLIC_URL: "$GITLAB_PAGES_URL" # Optional, define if needed
pages:
image: node:latest
script:
- npm install
- npm run build --publicPath="$PUBLIC_URL" # Prepend PUBLIC_URL for asset paths
- mv build public
artifacts:
paths:
- public
only:
- main
但是如果我去:
https://reactwebsitetobetested-alexandreasen09-9e495b734d3272d04dc8ee99.gitlab.io/
它显示一个空白页面,控制台显示:
/%PUBLIC_URL%/favicon.ico:1
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
/%PUBLIC_URL%/manifest.json:1
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
/%PUBLIC_URL%/favicon.ico:1
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
/%PUBLIC_URL%/manifest.json:1
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
在我看来,它很难找到网站所需的文件,就像出于某种原因它一直使用“%PUBLIC_URL%”而不是我在 .yml 文件中分配为变量的 URL。
请注意:该 URL 是私有的,但我有访问它的正确权限。
我的文件夹结构如下所示:
reactwebsitetobetested (this is the main folder for the project)
-README (markdown file)
-node_modules (folder)
-public (folder)
-- faveicon.ico
-- index.html
-- logo192.png
-- logo512.png
-- manifest.json
-- robots.txt
-src (folder)
-.gitignore
-package.json
-package-lock.json
-.gitlab-ci
你知道解决这个问题的方法吗?
谢谢!
我找到了解决方案。
发生错误是因为我忘记更改index.html文件中的URL。他们引用的是 %PUBLIC_URL%,例如 %PUBLIC_URL%/favicon.ico,但应该只是 favicon.ico。
更改 index.html 中的 URL 修复了错误。
现在我收到了 CORS 错误,但这当然是一个不同的问题,我将继续进一步研究。