简体   繁体   English

在Heroku部署的React应用上未显示图像,但响应为200

[英]Images not showing on heroku-deployed React app, but responding with 200

I have a simple react app deployed on Heroku, (using the static-build pack https://github.com/heroku/heroku-buildpack-static ) however the images are not loading. 我在Heroku上部署了一个简单的react应用程序(使用static-build pack https://github.com/heroku/heroku-buildpack-static ),但是图像未加载。 I know this must have to do something with the path, as it works for me locally, however nothing I have tried works. 我知道这一定与路径有关,因为它在本地对我有效,但是我没有尝试过。

Here is what the directory looks like 这是目录的样子

build
  index.html
  bundle.js
node_modules
public
  images
    logo.png
    index.html
src
    components
        LeftSidebar
            index.js
static.json
webpack.config.js

Here is my static.json file: 这是我的static.json文件:

{
  "root": "build",
  "routes": {
    "/**": "index.html"
  },
  "https_only": true
}

I have set the image src (jsx in the LeftSidebar component) to have a relative path from the build to the public directory. 我已经将图像src (LeftSidebar组件中的jsx)设置为具有从构建到公共目录的相对路径。 The relative path from the build folder to the public folder like so: 从构建文件夹到公用文件夹的相对路径,如下所示:

"../public/images/logo.png" “../public/images/logo.png”

The reason I did the above, is because I figured that since the only thing that is actually being ran in the browser is the index.html file which loads the bundle.js, which is invoking the <img /> this relative path would work. 进行上述操作的原因是因为我认为,由于浏览器中实际运行的唯一内容是加载bundle.js的index.html文件,该文件正在调用<img />因此此相对路径可以工作。 (please correct me if I'm wrong here) (如果我在这里错了,请纠正我)

I have also tried setting the path such that it is relative from my specific component LeftSidebar -- index.js to the image in the public folder: 我还尝试设置路径,使其相对于我的特定组件LeftSidebar -- index.js相对于公用文件夹中的图像:

../../../public/images/logo.png ../../../public/images/logo.png

I have also tried 我也尝试过

public/images/logo.png 公共/图像/ logo.png

and a few other variations, all with no luck. 和其他一些变体,都没有运气。

I think that I must be missing something more important about the way a static web server (Nginx in this case) will server static assets. 我认为对于静态Web服务器(在本例中为Nginx)处理静态资产的方式,我必须缺少一些更重要的内容。 Or int he way Webpack ultimately creates the bundle.js file. 或者用Webpack最终创建bundle.js文件的方式。

It is also quite odd that there is no 404 status code returning, but the image won't load and the response in the network tab is nothing but a white screen. 还很奇怪,没有返回404状态代码,但是图像不会加载,并且“网络”标签中的响应只是白屏。

When importing images into React, we usually rely on Webpack to copy the appropriate files over, rather than having to reference a public directory, eg 当将图像导入React时,我们通常依靠Webpack来复制适当的文件,而不必引用公共目录,例如

src
    components
        MyComponent
            index.jsx
            image.png

In my index.jsx file, I'd simply import the image like so: 在我的index.jsx文件中,我只是像这样导入图像:

import myImage from './image.png';

Finally, and most importantly, you need to update your Webpack config to include a loader for your images if doesn't already include one, such as file-loader . 最后,最重要的是,您需要更新Webpack配置,使其包含映像的加载器(如果尚未包含),例如file-loader

See this question/answer for more details on the process: How to import image (.svg, .png ) in a React Component 有关此过程的更多详细信息,请参见此问题/答案: 如何在React组件中导入图像(.svg,.png)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM