我正在尝试使用webpacker&和rails进行服务器端渲染反应护栏。我对客户端呈现没有任何问题,但我在服务器端呈现时遇到以下异常。
ExecJS::ProgramError in Static#index
identifier 'Set' undefined
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>ReactExample</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application' %>
</head>
<body>
<%= yield %>
</body>
</html>
index.html.erb
<%= react_component('hello', {}, {prerender: true}) %>
的application.js
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb
// Support component names relative to this directory:
var componentRequireContext = require.context("components", true)
var ReactRailsUJS = require("react_ujs")
ReactRailsUJS.useContext(componentRequireContext)
server_rendering.js
// By default, this pack is loaded for server-side rendering.
// It must expose react_ujs as `ReactRailsUJS` and prepare a require context.
var componentRequireContext = require.context("components", true)
var ReactRailsUJS = require("react_ujs")
ReactRailsUJS.useContext(componentRequireContext)
答案 0 :(得分:0)
从TheRubyRacer升级到现代的JavaScript执行引擎。
TheRubyRacer依赖LibV8(为node.js供电的位),但它是locked to version 3,LibV8本身已经在version 7。
使用现代的JS宝石,例如Mini_Racer,这个问题就消失了。