Heroku - Развертывание приложения React / Express и получение «Не удалось загрузить ресурс» в консоли при запросе файлов chunk.js - PullRequest
1 голос
/ 03 июля 2019

Я получаю эти ошибки в консоли после того, как мое приложение 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 находится в /client/build/ на моем сервере Heroku.Это файл, который отправляет мой сервер Express.Сервер отправляет этот index.html файл, но сам файл не находит необходимые ему ресурсы.

Это вызывает проблему загрузки пустого приложения.По какой-то причине index.html не находит файлы chunk.js внутри /client/build/static/js.Они определенно там, и я могу подтвердить с помощью heroku run bash и осмотреть каталоги.

Когда я проверяю документ index.html в браузере, внизу я вижу, где теги скрипта вызывают файлы chunk.js в /static/js:

enter image description here

Вот как выглядит корень 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:

  • 1-е изображение: /client каталог
  • 2-е изображение:/client/build каталог
  • 3-е изображение: /client/build/static/js каталог

enter image description here enter image description here enter image description here

1 Ответ

1 голос
/ 03 июля 2019

Проблема была на самом деле в моем server.js файле, который я не включил в это сообщение.

Первоначально это было express.static(path_join(__dirname, '/client/build'))

, оно должно было быть: express.static(path_join(__dirname, '../client/build'))

Это тот случай, когда мой server.js файл находится в /server и он пытался найти /client/build внутри /server вместо корневого каталога приложения на Heroku.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...