Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用服务器端渲染在Rails上作出反应_Ruby On Rails_Webpack_React On Rails - Fatal编程技术网

Ruby on rails 使用服务器端渲染在Rails上作出反应

Ruby on rails 使用服务器端渲染在Rails上作出反应,ruby-on-rails,webpack,react-on-rails,Ruby On Rails,Webpack,React On Rails,我使用React on Rails创建了一个站点。它工作得非常好,但我没有注意到服务器端渲染被关闭,我需要它来进行SEO,所以我决定打开它。问题是,现在我得到了这样一个错误: ERROR in SERVER PRERENDERING Encountered error: "ReferenceError: window is not defined" /* eslint comma-dangle: ["error", {"functions": "never", "arrays": "onl

我使用React on Rails创建了一个站点。它工作得非常好,但我没有注意到服务器端渲染被关闭,我需要它来进行SEO,所以我决定打开它。问题是,现在我得到了这样一个错误:

ERROR in SERVER PRERENDERING
Encountered error: "ReferenceError: window is not defined"
/* eslint comma-dangle: ["error",
  {"functions": "never", "arrays": "only-multiline", "objects":
"only-multiline"} ] */

const webpack = require('webpack');
const path = require('path');

const devBuild = process.env.NODE_ENV !== 'production';
const nodeEnv = devBuild ? 'development' : 'production';

const bundles = [
    './app/bundles/Home/startup/registration'
]

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    ...bundles
  ],

  output: {
    filename: 'webpack-bundle.js',
    path: '../app/assets/webpack',
  },

  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
      react: path.resolve('./node_modules/react'),
      'react-dom': path.resolve('./node_modules/react-dom'),
    },
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(nodeEnv),
      },
    }),
  ],
  module: {
    loaders: [
      {
        test: require.resolve('react'),
        loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
};

module.exports = config;

if (devBuild) {
  console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
  module.exports.devtool = 'eval-source-map';
} else {
  config.plugins.push(
    new webpack.optimize.DedupePlugin()
  );
  console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}
我的webpack.config.js如下所示:

ERROR in SERVER PRERENDERING
Encountered error: "ReferenceError: window is not defined"
/* eslint comma-dangle: ["error",
  {"functions": "never", "arrays": "only-multiline", "objects":
"only-multiline"} ] */

const webpack = require('webpack');
const path = require('path');

const devBuild = process.env.NODE_ENV !== 'production';
const nodeEnv = devBuild ? 'development' : 'production';

const bundles = [
    './app/bundles/Home/startup/registration'
]

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    ...bundles
  ],

  output: {
    filename: 'webpack-bundle.js',
    path: '../app/assets/webpack',
  },

  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
      react: path.resolve('./node_modules/react'),
      'react-dom': path.resolve('./node_modules/react-dom'),
    },
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(nodeEnv),
      },
    }),
  ],
  module: {
    loaders: [
      {
        test: require.resolve('react'),
        loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
};

module.exports = config;

if (devBuild) {
  console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
  module.exports.devtool = 'eval-source-map';
} else {
  config.plugins.push(
    new webpack.optimize.DedupePlugin()
  );
  console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}

我该怎么做才能修好它?我阅读了《rails上的反应》(react on rails)github)上的一个相关问题,但我仍然不知道。

window
是大多数web浏览器定义的全局变量,但不是web服务器定义的。如果在客户端代码中引用
窗口
,而没有适当的保护措施,例如在试图修改它之前确保它已定义,则会出现此错误。