react: Cross-origin error passed to componentDidCatch incorrectly
tl;dr React is passing “A cross-origin error was thrown” to componentDidCatch
when there are no cross-origin scripts.
See this discussion thread and this repro case.
I was able to confirm the behavior. A quick look at onError
showed a null event.error
.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 3
- Comments: 59 (25 by maintainers)
Commits related to this issue
- Better messaging for componentDidCatch cross-origin errors The prior message was a bit wordy and didn't cover all cases (like the one we discovered while researching #10441). We can use the new URL t... — committed to bvaughn/react by bvaughn 7 years ago
- Better messaging for componentDidCatch cross-origin errors The prior message was a bit wordy and didn't cover all cases (like the one we discovered while researching #10441). We can use the new URL t... — committed to bvaughn/react by bvaughn 7 years ago
- Better messaging for componentDidCatch cross-origin errors (#10447) The prior message was a bit wordy and didn't cover all cases (like the one we discovered while researching #10441). We can use t... — committed to facebook/react by bvaughn 7 years ago
- Change webpack config to support Error Boundaries Errors are hidden because of a cross-origin issue which is fixed by not evaluating each module and by also enabling cross-origin loading in webpack's... — committed to TrueCar/gluestick by christinebrass 6 years ago
- [BREAKING/MAJOR] Upgrade to React 16 (#1135) * update dependencies * use prop-types package instead of react * update radium * get rid of electrode SSR caching * fxi tests * Fix webpac... — committed to TrueCar/gluestick by zamotany 6 years ago
- Replaces source map to allow cross-origin handling The `eval-source-map` loader causes the React development tools to throw a cross-origin error if anything is thrown, even if the error is caug ht an... — committed to aleph-naught2tog/itg-react-scripts by deleted user 6 years ago
Same issue here with Windows 10, Chrome 62, Webpack 3 & React 16.0-16.1.
Webpack+React always throws
Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.
.Tried
devtool: 'cheap-module-source-map'
(and others without eval)Tried
Tried
Tried crossorigin script tag
Nothing works
FYI @leidegre the solution for this issue is to replace:
with:
in your Webpack config.
For what it’s worth, I ran through the all of the
devtools
settings and here’s what I found:It looks like the default setting (and any of the *eval ones) will cause problems.
We can probably encourage people to stop using it and use
cheap-module-source-map
instead. It’s just as fast (afaik) and doesn’t have some issues.Looks like the repro case (starting with the
tessin_mini
repo) can be reduced to:The console will log:
But the error we pass to
componentDidCatch
will be:Experiencing same problem in Chrome, nothing works as @DominikSerafin mentioned, but there is no such error in Firefox.
Following solution works for me:
webpack.config.js
package.json
And
crossorigin
onscript
tag.I’m still getting this as well no matter my webpack settings.
Same thing for me as for @the-spyke, eval is much faster than other alternatives in my project(s).
React should work correctly with it, and not suggest workarounds (which do not even work).
Hoping it will get fixed…
The team chatted out of band about this briefly and the agreed-upon solution is to change the wording of the error message passed to
componentDidCatch
to be:At the specified URL we’ll have a blurb that explains the technique we’re using in dev-mode and mentions both the
<script>
tagcrossorigin
attribute for CDNs and the Webpackdevtools
setting.Please, make an option for disable this behavior. I want to use
eval
(performance, compatibility). And if switch to acheap-module-source-map
, I just get a duplicate error in the console.Updated dependencies today and started to get this issue in tests. Not sure what is
just as fast
, but in my projecteval
is 2-3 times faster in rebuilds bywebpack-dev-server
watch.No worries. If people are still seeing it, it’s probably okay to re-open it. 😄
@bvaughn @gaearon So I’m just super happy that we could root cause this, thanks for the hard work! Enjoy your weekend!
I had to do these 3 things together (using webpack-dev-server running on port 8080):
Add this to webpack config:
use
cheap-module-source-map
(cheap-module-eval-source-map did not work)Add
crossorigin
to my script tag:@bvaughn
Sure, here is a simple demo project: https://github.com/jjjjw/crossorigin-webpack-demo
Worth noting that the case is when webpack dev server is used on a different port/domain than the web server.
Additionally, here is a first go at a doc addition: https://github.com/reactjs/reactjs.org/pull/187
I ran into this problem when using webpack, code splitting, and webpack dev server (similar to a CDN setup).
In my setup the initial bundle is loaded with a script tag, and other bundles are then loaded via JSONP by webpack. With the
crossorigin
attribute added to the script tag for the initial bundle errors can be handled, but only if they are thrown by code in the initial bundle.To handle errors in the other bundles it’s necessary to configure webpack for crossorigin script loading with this option https://webpack.js.org/configuration/output/#output-crossoriginloading
Like so in the webpack config:
This adds the
crossorigin
attribute to the JSONP script tags used to load the bundles.I’ll put up a PR that adds addition verbiage to the cross-origin (dev-mode) error to mention this.
Creating a small, standalone project with no SSR/Webpack/etc, I am able to reproduce the unexpected behavior only for Chrome when loading content via
file://...
.Here’s what I’m seeing:
file://
http://
tessin_mini
devtessin_mini
prod1: I believe Chrome considers files loaded with
file://
to always be a different domain. If I launch Chrome with the--allow-file-access-from-files
flag, the standalone file works with when loaded via thefile://
protocol as well.2: Also worth adding that the
tessin_mini
example works as expected in production mode (b’c we use a regular try/catch). The unexpected cross-origin error only affects dev-mode.After getting stuck on this for several days, we tried all kinds of solutions but to no avail. In the end, we found out that
127.0.0.1
andlocalhost
are cross-origin, e.g. serving the app via127.0.0.1
but visiting it vialocalhost
will trigger this issue, hope that helps.Want to highlight, similarly to @programmerwy, that it started working for me, even with the source map set to
eval-source-map
, once I addedcrossorigin
to the script tag that loads the webpack bundle (couldn’t get it to work no matter what without that attribute on the script tag).Using Chrome 80.0.3987.100 (Linux) and React 16.12.
Try checking your package.json, remove the -d flag.
That’s what finally worked for me.
I’ve noticed that I have this issue when Chrome DevTools are open,
crossOriginLoading
anddevtool
=cheap-module-source-map
doesn’t change anything. Works fine in Firefox with open developer tools.@bvaughn, my project is buggy. Sorry for flooding this thread. To whom it may concern, here’s a minimal setup of a Chrome extension using React, Redux, react-devtools and error boundaries.
Source: https://github.com/herodrigues/chrome-react-minimal
What makes you say the error is thrown twice?
Are you referring to what Dan describes in facebook/react/issues/10384, by chance?
Same problem here, tried both crossOriginLoading: ‘anonymous’ and devtool set to ‘cheap-module-eval-source-map’
You’re welcome @leidegre!
Just to close the loop here, we’ve added a new docs page with information about this and other cross-origin error causes:
https://fb.me/react-crossorigin-error
By eval I meant the Webpack
devtool: "eval"
setting which is commonly used and turns every module into eval call so that it’s shown separately in DevTools despite no sourcemaps. But seems like we determined that isn’t the issue.