Skip to content
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

Open
bvaughn opened this issue Aug 11, 2017 · 59 comments · Fixed by #10447 or #10457
Open

Cross-origin error passed to componentDidCatch incorrectly #10441

bvaughn opened this issue Aug 11, 2017 · 59 comments · Fixed by #10447 or #10457
Assignees
Labels
Component: DOM React Core Team Opened by a member of the React Core Team Type: Needs Investigation

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Aug 11, 2017

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.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

cc @acdlite, @gaearon, @leidegre

@gaearon
Copy link
Collaborator

gaearon commented Aug 11, 2017

Could be something funky caused by e.g. eval?

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

Not sure I understand. Are you asking if the user-code is maybe using eval?

The repro is just accessing a prop on a null value.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

Looks like the repro case (starting with the tessin_mini repo) can be reduced to:

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:

Uncaught TypeError: Cannot read property 'foo' of null

But the error we pass to componentDidCatch will be:

A cross-origin error was thrown...

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

I think this has something to do with the bundling code in the tessin_mini repo. The same code in a CRA-created app works as expected.

Maybe it has to do with the SSR logic?

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

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:

Browser standalone file:// standalone http:// tessin_mini dev tessin_mini prod
Chrome 1 2
Firefox
Safari

1: 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 the file:// 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.

@gaearon
Copy link
Collaborator

gaearon commented Aug 11, 2017

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.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

😭 Thanks for clarifying!

You're absolutely right. That was the cause. Removing that property from the Webpack config clears the issue up in the tessin_mini project.

@gaearon
Copy link
Collaborator

gaearon commented Aug 11, 2017

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.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

It's interesting that I use the devtool: "eval" setting in react-virtualized's Webpack config also but it doesn't show this error.

I'm not super familiar with Webpack. 😄

Edit This was working for the RV project b'c of redbox-react + react-transform-catch-errors.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

For what it's worth, I ran through the all of the devtools settings and here's what I found:

devtool works?
default
eval
cheap-eval-source-map
cheap-source-map
cheap-module-eval-source-map
cheap-module-source-map
eval-source-map
source-map
inline-source-map
hidden-source-map
nosources-source-map

It looks like the default setting (and any of the *eval ones) will cause problems.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 11, 2017

I'll put up a PR that adds addition verbiage to the cross-origin (dev-mode) error to mention this.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 12, 2017

FYI @leidegre the solution for this issue is to replace:

devtool: "eval"

with:

devtool: "cheap-module-source-map"

in your Webpack config.

@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 12, 2017

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:

A cross-origin error was thrown so React doesn't have access to the actual error object in development. See https://fb.me/react-crossorigin-error for more details.

At the specified URL we'll have a blurb that explains the technique we're using in dev-mode and mentions both the <script> tag crossorigin attribute for CDNs and the Webpack devtools setting.

bvaughn added a commit to bvaughn/react that referenced this issue Aug 12, 2017
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
bvaughn added a commit to bvaughn/react that referenced this issue Aug 12, 2017
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
@leidegre
Copy link
Contributor

leidegre commented Aug 12, 2017

@bvaughn @gaearon So I'm just super happy that we could root cause this, thanks for the hard work! Enjoy your weekend!

bvaughn added a commit that referenced this issue Aug 14, 2017
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
@bvaughn
Copy link
Contributor Author

bvaughn commented Aug 14, 2017

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

@leidegre
Copy link
Contributor

LGTM!

@sultan99
Copy link

The problem A cross-origin error was thrown. React doesn't have access to the actual error object in development. comes with react v16 in development mode as soon as I switch it to the production mode it works fine.

No one of these helped:

  • 'Access-Control-Allow-Origin': '*'
  • <script crossorigin="anonymous" ...
  • devtool: 'cheap-module-source-map'

@jjjjw
Copy link
Contributor

jjjjw commented Oct 18, 2017

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:

  ...  
  output: {
    crossOriginLoading: 'anonymous',
    ...
  }

This adds the crossorigin attribute to the JSONP script tags used to load the bundles.

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 19, 2017

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.

@jjjjw
Copy link
Contributor

jjjjw commented Oct 19, 2017

@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: reactjs/react.dev#187

@DominikSerafin
Copy link

DominikSerafin commented Nov 11, 2017

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

devServer: {
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
      "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
    },
}

Tried

output: {
  crossOriginLoading: 'anonymous',
}

Tried crossorigin script tag


Nothing works

@StalkAlex
Copy link

StalkAlex commented Nov 17, 2017

Experiencing same problem in Chrome, nothing works as @DominikSerafin mentioned, but there is no such error in Firefox.

@edongashi
Copy link

Getting same behavior as two previous comments in chrome.

@the-spyke
Copy link

Updated dependencies today and started to get this issue in tests. Not sure what is just as fast, but in my project eval is 2-3 times faster in rebuilds by webpack-dev-server watch.

@DominikSerafin
Copy link

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...

@sapegin
Copy link

sapegin commented Dec 19, 2017

I’ve noticed that I have this issue when Chrome DevTools are open, crossOriginLoading and devtool = cheap-module-source-map doesn’t change anything. Works fine in Firefox with open developer tools.

@evelant
Copy link

evelant commented Jan 25, 2018

I'm still getting this as well no matter my webpack settings.

@IanVS
Copy link

IanVS commented Feb 2, 2018

@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.

christinebrass added a commit to TrueCar/gluestick that referenced this issue Feb 7, 2018
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
@BrendonSled
Copy link

Try checking your package.json, remove the -d flag.

That's what finally worked for me.

@DominikSerafin
Copy link

DominikSerafin commented Feb 11, 2018

@BrendonSled

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.

@herodrigues
Copy link

herodrigues commented Feb 11, 2018

@IanVS there was something wrong in my project. I developed a "clean" extension using React and everything is ok.

See my comment #10441 (comment)

@BrendonSled
Copy link

@DominikSerafin no I mean for webpack cli, https://webpack.js.org/api/cli/#shortcuts

@dehghani-mehdi
Copy link

dehghani-mehdi commented Feb 26, 2018

Following solution works for me:

webpack.config.js

devServer: {
    headers: {
        'Access-Control-Allow-Origin': '*'
    }
}

package.json

"start": "webpack-dev-server --devtool source-map --history-api-fallback --hot"

And crossorigin on script tag.

christinebrass pushed a commit to TrueCar/gluestick that referenced this issue Mar 2, 2018
* 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
@igorushko
Copy link

Please, make an option for disable this behavior. I want to use eval (performance, compatibility). And if switch to a cheap-module-source-map, I just get a duplicate error in the console.

@Omelettesheep
Copy link

When i use DllReferencePlugin , the problem will happen. And have tried the ways above, none of them works......

aleph-naught2tog pushed a commit to aleph-naught2tog/itg-react-scripts that referenced this issue Aug 16, 2018
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).
@prgrmrwy
Copy link

prgrmrwy commented Dec 9, 2019

For what it's worth, I ran through the all of the devtools settings and here's what I found:

devtool works?
default
eval ✖
cheap-eval-source-map ✖
cheap-source-map ✓
cheap-module-eval-source-map ✖
cheap-module-source-map ✓
eval-source-map ✖
source-map ✓
inline-source-map ✓
hidden-source-map ✓
nosources-source-map ✓
It looks like the default setting (and any of the *eval ones) will cause problems.

i try the table above ,bug i get the inverse result 🤣, im confused..

@necolas necolas added the React Core Team Opened by a member of the React Core Team label Jan 8, 2020
@fgblomqvist
Copy link

Want to highlight, similarly to @programmerwy, that it started working for me, even with the source map set to eval-source-map, once I added crossorigin 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.

@WolvenSpirit
Copy link

WolvenSpirit commented Aug 5, 2021

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.

@wenfangdu
Copy link

wenfangdu commented Sep 2, 2022

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 and localhost are cross-origin, e.g. serving the app via 127.0.0.1 but visiting it via localhost will trigger this issue, hope that helps.

@xiongwei9
Copy link

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 and localhost are cross-origin, e.g. serving the app via 127.0.0.1 but visiting it via localhost will trigger this issue, hope that helps.

Yes, I have the same issue. I visit my devServer xxx.com with http-proxy, but js script is in localhost:3000
image

fix output of webpack config:

output: {
  publicPath: '/'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DOM React Core Team Opened by a member of the React Core Team Type: Needs Investigation
Projects
None yet