反应服务器端渲染和热重新加载

时间:2016-12-30 08:06:12

标签: reactjs webpack serverside-rendering webpack-hmr

我的网站包装如下:

从'webpack'导入webpack; 从'path'导入路径;

export default {
  debug: true,
  devtool: 'inline-source-map',
  noInfo: false,
  entry: [
    'eventsource-polyfill', // necessary for hot reloading with IE
    'webpack-hot-middleware/client?path=http://localhost:3000/__webpack_hmr&reload=true', //note that it reloads the page if hot module reloading fails.
    path.resolve(__dirname, 'src/index')    //starting point set to index js in src dir
  ],
  target: 'web',
  output: {
    path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
    publicPath: '/',
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: path.resolve(__dirname, 'src')
  },
  plugins: [
   new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [
      {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
      {test: /(\.css)$/, loaders: ['style', 'css']},
      {test: /\.scss$/, loaders: ['style', 'css', 'postcss', 'sass']},
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
      {test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'},
      {test: /\.(jpe?g|png|gif|svg)$/i, loaders: ['file?hash=sha512&digest=hex&name=[hash].[ext]', 'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false']}    ]
  }
};

babelrc:

{
  "presets": ["react", "es2015"],
  "env": {
    "development": {
      "presets": ["react-hmre"]
    }
  }
}

我想导入我的routes.js文件,该文件只是一个包含路由描述的小文件,并使用react-router

const routes= (
  <Route path="/sbl_next" component={App}>
    <IndexRoute  component={HomePageContent}/>
    <Route path = "orderTracking" component = {OrderTracking} />
  </Route>
);


export default routes;

当我尝试这样做时,它一直在说:

/home/simran/sbl_frontend/node_modules/react-transform-hmr/lib/index.js:51
    throw new Error('locals[0] does not appear to be a `module` object with Hot Module ' + 'replacement API enabled. You should disable react-transform-hmr in ' + 'production by using `env` section in Babel configuration. See the ' + 'example in README: https://github.com/gaearon/react-transform-hmr');
    ^

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你需要使用historyApiFallback作为ahack取决于路由器版本

  devServer: {
    contentBase: path.resolve(__dirname, 'src'),
    historyApiFallback: true   },

并将您的babel文件更改为以下内容。

"presets": [
    "es2015", // will run third
    "react", // will run second
    "stage-0" // will run first
  ],