在我的React应用程序已构建并部署到Heroku上之后,我在控制台中收到这些错误。
Refused to apply style from 'https://radiant-tor-66940.herokuapp.com/index.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Failed to load resource: the server responded with a status of 404 (Not Found) main.3174e036.chunk.js:1
Failed to load resource: the server responded with a status of 404 (Not Found) 1.b1e0c624.chunk.js:1
Failed to load resource: the server responded with a status of 404 (Not Found) main.3174e036.chunk.js:1
Failed to load resource: the server responded with a status of 404 (Not Found) manifest.json:1
Manifest: Line: 1, column: 1, Unexpected token. manifest.json:1
index.js
文件位于我的Heroku服务器上的/client/build/
中。这是我的Express服务器发送的文件。服务器正在发送此index.html
文件,但是文件本身找不到所需的资源。
这导致加载空白应用程序的问题。 F由于某些原因index.html
在chunk.js
内找不到/client/build/static/js
文件。它们肯定在那里,我可以通过heroku run bash
进行确认并检查目录。
当我在浏览器中检查index.html
文档时,我可以在底部看到脚本标签在chunk.js
中调用/static/js
文件的地方:
应用的根package.json
如下所示:
"scripts": {
"test": "jest",
"start": "node server/server.js",
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
},
"engines": {
"node": "~9.10.1",
"npm": "~5.6.0"
}
package.json
中的React应用的/client
如下所示:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"lodash": "^4.17.11",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.3",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"styled-components": "^4.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"proxy": "http://localhost:8000/"
}
以下是Heroku构建后的文件结构:
/client
目录/client/build
目录/client/build/static/js
目录答案 0 :(得分:1)
问题实际上出在我的server.js
文件中,我没有在本文中提及。
最初是express.static(path_join(__dirname, '/client/build'))
它必须为:express.static(path_join(__dirname, '../client/build'))
之所以如此,是因为我的server.js
文件位于/server
中,并且它试图在/client/build
内而不是Heroku的根应用程序目录中找到/server
。