简体   繁体   English

无法使用Webpack加载图像并在SSR中做出反应

[英]Not load images with webpack and react in SSR

When was compile react in SSR output this error : 在编译时在SSR输出中发生此错误:

Warning: Prop src did not match. 警告:属性src不匹配。 Server: "6eccc1deda4ab0e5bd5f0ffc12b182d9.png" Client: "/app/b2c50f601d2ca40b58c37c526b62b120.png" 服务器:“ 6eccc1deda4ab0e5bd5f0ffc12b182d9.png”客户端:“ / app / b2c50f601d2ca40b58c37c526b62b120.png”

My code in react for add image is like this: 我在添加图像的代码中是这样的:

import logo from './image/logo.png';

And my webpack have this rule with 'image-webpack-loader': 我的webpack的“ image-webpack-loader”有以下规则:

{
  test: /\.(jpe?g|png|gif)$/i,
  use: [
    'file-loader',
    {
      loader: 'image-webpack-loader',
      options: {
        mozjpeg: {
          enabled: false,
          progressive: true,
          quality: 65
        },
        // optipng.enabled: false will disable optipng
        optipng: {
          enabled: false
        },
        pngquant: {
          quality: '65-90',
          speed: 4
        },
        gifsicle: {
          interlaced: false
        },
        // the webp option will enable WEBP
        webp: {
          enabled: false,
          quality: 75
        }
      }
    }
  ]
}

How can I solve it? 我该如何解决?

I already solved this, I did it in the following way: 我已经解决了这个问题,我通过以下方式做到了:

{
  test: /\.(png|jpe?g|gif)(\?.*)?$/,
  use: [
    {
      loader: 'file-loader',
      options: {
        name: '[name].[ext]',
        publicPath: 'app/'
      }
    },
    {
      loader: 'image-webpack-loader',
      options: {
        mozjpeg: {
          progressive: false,
          quality: 10
        },
        // optipng.enabled: false will disable optipng
        optipng: {
          enabled: false
        },
        pngquant: {
          quality: '65-90',
          speed: 4
        },
        gifsicle: {
          interlaced: false
        },
        // the webp option will enable WEBP
        webp: {
          quality: 75
        }
      }
    }
  ]
}

To resolve the problem you have to add the client file-loader config (name and publicPath) to the server file-loader config. 要解决此问题,您必须将客户端文件加载器配置(名称和publicPath)添加到服务器文件加载器配置中。

Your server file-loader config should be: 您的服务器文件加载器配置应为:

{
  test: /\.(jpe?g|png|gif)$/i,
  use: [
    {
      loader: 'file-loader',
       options: {
         name: '[name].[ext]',
         publicPath: 'app/'
      }
    },
    {
      loader: 'image-webpack-loader',
      options: {
        mozjpeg: {
          enabled: false,
          progressive: true,
          quality: 65
        },
        // optipng.enabled: false will disable optipng
        optipng: {
          enabled: false
        },
        pngquant: {
          quality: '65-90',
          speed: 4
        },
        gifsicle: {
          interlaced: false
        },
        // the webp option will enable WEBP
        webp: {
          enabled: false,
          quality: 75
        }
      }
    }
  ]
}

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

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