-
-
Notifications
You must be signed in to change notification settings - Fork 631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextEncoder is not defined or TextEncoder is not a constructor (mini_racer) #1457
Comments
I encountered the same issue. @justin808 |
|
Greetings, this was my solution for adding all the required polyfills for react SSR with mini_racer: add this plugin to your webpack config: const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
// need to polyfill certain node libs like `stream` and `util` (used by `react-dom/server`)
config.plugins.push(
new NodePolyfillPlugin({
excludeAliases: ['console', 'Buffer'],
}),
); serverPolyfills.js (import this file at the top of our server-rendering file b/c it needs to run before importing //
// polyfills for mini_racer v8 runtime (which has less features than node.js)
//
// `URL` constructor
import 'core-js/actual/url';
// `URLSearchParams` constructor
import 'core-js/actual/url-search-params';
// polyfill TextEncoder & TextDecoder onto `util` b/c `node-util` polyfill doesn't include them
// https://github.com/browserify/node-util/issues/46
import util from 'util';
import 'fast-text-encoding';
Object.assign(util, { TextDecoder, TextEncoder }); |
@alexeyr Thanks for your reply. |
Thank you @wyattades! I'm assuming we have different version though, because for me URL and URLSearchParams were in core-js-pure instead of core-js. Then I obviously also needed to install the fast-text-encoding package with yarn. So for anyone else having the same issue as me, I needed to make the following changes to @wyattades code:
|
@wyattades I owe you big thanks. I ran into this problem while working on reactjs/react-rails#1290 & your solution is the only logic that didn't result in |
* Upgrade react * Upgrade depenencies * new react source * remove assertions on data-reactroot see facebook/react#10971 * use React v18's root API * add TextEncoder library * use node polyfill plugin & fast-text-encoder see shakacode/react_on_rails#1457 (comment) * entry for #1290 --------- Co-authored-by: Mostafa Ahangarha <[email protected]>
* Upgrade react * Upgrade depenencies * new react source * remove assertions on data-reactroot see facebook/react#10971 * use React v18's root API * add TextEncoder library * use node polyfill plugin & fast-text-encoder see shakacode/react_on_rails#1457 (comment) * entry for #1290 --------- Co-authored-by: Mostafa Ahangarha <[email protected]>
A bug is a crash or incorrect behavior. If you have a debugging or troubleshooting question, please open a discussion.
Environment
Expected behavior
Try to create new app as the steps.
The difference is that I change the argument
prerender
in directoryapp/views/hello_world/index.htm.erb
, like this<%= react_component("HelloWorld", props: @hello_world_props, prerender: true) %>
It should show the
HelloWorld
component when accessing http://localhost:3000/hello_worldActual behavior
But it failed and shows some errors below,
Then I try to switch the JS engine of ruby to
node
, likeexport EXECJS_RUNTIME=Node
.It works.
It seems that the
mini_racer(0.6.2)
does not support theTextEncoder
.Is there something I missed?
Small, reproducible repo
The text was updated successfully, but these errors were encountered: