-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
Cross-origin error passed to componentDidCatch incorrectly #10441
Comments
Could be something funky caused by e.g. eval? |
Not sure I understand. Are you asking if the user-code is maybe using The repro is just accessing a prop on a null value. |
Looks like the repro case (starting with the import React, { Component } from "react";
export default class App extends Component {
state = {
doError: false,
};
render() {
return (
<ErrorBoundary>
{this.state.doError
? <ComponentThatFails/>
: <button onClick={() => this.setState({doError: true})}>Click me</button>}
</ErrorBoundary>
)
}
}
class ErrorBoundary extends Component {
state = {
error: null
};
componentDidCatch(error) {
this.setState({error})
}
render() {
if (this.state.error) {
return this.state.error.message;
}
return this.props.children
}
}
class ComponentThatFails extends Component {
render() {
const data = null;
return <p>{data.foo}</p>;
}
} The console will log:
But the error we pass to
|
I think this has something to do with the bundling code in the Maybe it has to do with the SSR logic? |
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 Here's what I'm seeing:
1: I believe Chrome considers files loaded with 2: Also worth adding that the |
By eval I meant the Webpack |
😭 Thanks for clarifying! You're absolutely right. That was the cause. Removing that property from the Webpack config clears the issue up in the |
We can probably encourage people to stop using it and use |
It's interesting that I use the I'm not super familiar with Webpack. 😄 Edit This was working for the RV project b'c of |
For what it's worth, I ran through the all of the
It looks like the default setting (and any of the *eval ones) will cause problems. |
I'll put up a PR that adds addition verbiage to the cross-origin (dev-mode) error to mention this. |
FYI @leidegre the solution for this issue is to replace: devtool: "eval" with: devtool: "cheap-module-source-map" in your Webpack config. |
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
At the specified URL we'll have a blurb that explains the technique we're using in dev-mode and mentions both the |
The prior message was a bit wordy and didn't cover all cases (like the one we discovered while researching facebook#10441). We can use the new URL to explain background info on the DEV-mode technique we're using in addition to common fixes (eg crossorigin attribute for CDN scripts and Webpack devtools settings). Putting this information behind a link will allow us to more easily edit it in the future as common causes for this issue change. Resolves facebook#10441
The prior message was a bit wordy and didn't cover all cases (like the one we discovered while researching facebook#10441). We can use the new URL to explain background info on the DEV-mode technique we're using in addition to common fixes (eg crossorigin attribute for CDN scripts and Webpack devtools settings). Putting this information behind a link will allow us to more easily edit it in the future as common causes for this issue change. Resolves facebook#10441
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 to explain background info on the DEV-mode technique we're using in addition to common fixes (eg crossorigin attribute for CDN scripts and Webpack devtools settings). Putting this information behind a link will allow us to more easily edit it in the future as common causes for this issue change. Resolves #10441
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: |
LGTM! |
The problem No one of these helped:
|
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 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 |
Hey @jjjjw. Thanks for the additional info! Any chance you could point me to a minimal repro/setup of the setup you're describing? I'd like to better understand it so that I can update the cross-origin-errors page in the React docs. |
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: reactjs/react.dev#187 |
Same issue here with Windows 10, Chrome 62, Webpack 3 & React 16.0-16.1. Webpack+React always throws Tried Tried
Tried
Tried crossorigin script tag Nothing works |
Experiencing same problem in Chrome, nothing works as @DominikSerafin mentioned, but there is no such error in Firefox. |
Getting same behavior as two previous comments in chrome. |
Updated dependencies today and started to get this issue in tests. Not sure what is |
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... |
I’ve noticed that I have this issue when Chrome DevTools are open, |
I'm still getting this as well no matter my webpack settings. |
@herodrigues were you able to get valid errors in your chrome extension in dev? I have a chrome extension as well and am seeing the same cross-origin errors. |
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 output config for dev. See also: facebook/react#10441
Try checking your package.json, remove the -d flag. That's what finally worked for me. |
Do you mean the npm loglevel flag? https://docs.npmjs.com/misc/config#shorthands-and-other-cli-niceties If yes, then it isn't a fix, but just merely "silencing" the problem, so I wouldn't recommend doing this because you might silence other useful errors this way. |
@IanVS there was something wrong in my project. I developed a "clean" extension using React and everything is ok. See my comment #10441 (comment) |
@DominikSerafin no I mean for webpack cli, https://webpack.js.org/api/cli/#shortcuts |
Following solution works for me:
And |
* update dependencies * use prop-types package instead of react * update radium * get rid of electrode SSR caching * fxi tests * Fix webpack aliases in production for server * Fix issue with latest yarn * Upgrade image to use Node 8 * Upgrade minor version of React * Update peer dependencies as well * 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 output config for dev. See also: facebook/react#10441 * Lint * Unify docker images on Node 8
Please, make an option for disable this behavior. I want to use |
When i use DllReferencePlugin , the problem will happen. And have tried the ways above, none of them works...... |
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 and handled (or would have been). The resulting cross-origin error is thrown in place of whatever else was thrown: as a result, the error b eing thrown is opaque because of how the browser handles cross-origin requests, and thus removes any ability to parse or handle differing erro rs or other throwables such as `Promise`s, etc. This is because of the `eval` nature of the loader, and the fix is to use a different loader, such as `cheap-source-map-loader`, as suggested by React [here](https://reactjs.org/docs/cross-origin-errors.html#source-maps) and as referenc ed in this [issue](facebook/react#10441).
i try the table above ,bug i get the inverse result 🤣, im confused.. |
Want to highlight, similarly to @programmerwy, that it started working for me, even with the source map set to Using Chrome 80.0.3987.100 (Linux) and React 16.12. |
This error is still thrown in React 17.0.2 on Google Chrome (version 92.0.4515.107 64-bit) when calling JSON.parse() twice on a state property, I fail to see how is it related with the actual error. |
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 |
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 nullevent.error
.The text was updated successfully, but these errors were encountered: