Unable to retrieve react-dom.development.js from unpkg
Asked Answered
F

1

5

I'm learning ReactJS and trying to run the code at https://raw.githubusercontent.com/kirupa/kirupa/master/reactjs/helloworld_batman.htm (this is from the instructions at https://www.kirupa.com/react/building_your_first_react_app.htm).

Using Chrome, "Batman" is never displayed and Chrome developer tools logs the following error:

Access to script at 'https://unpkg.com/[email protected]/umd/react-dom.development.js' (redirected from 'https://unpkg.com/react-dom@16/umd/react-dom.development.js') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

enter image description here

Sure enough, when requesting react-dom.development.js directly the Network tab reveals that the header is missing.

Other items of note:

  • The header IS present when requesting react.development.js directly
  • The instructions at https://reactjs.org/docs/cdn-links.html show retrieving the files from unpkg in this way
  • The problem does not occur in IE 11

What am I doing wrong?

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>React! React! React!</title>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/[email protected]/babel.min.js"></script>

  <style>
    #container {
      padding: 50px;
      background-color: #EEE;
    }
    #container h1 {
      font-size: 144px;
      font-family: sans-serif;
      color: #0080a8;
    }
  </style>
</head>

<body>
  <div id="container"></div>
  <script type="text/babel">
    var destination = document.querySelector("#container");
    ReactDOM.render(React.createElement(
      "h1",
      null,
      "Batman"
    ), destination);
  </script>
</body>

</html>
Faggoting answered 28/1, 2019 at 13:49 Comment(3)
The code I'm running can be found at github.com/kirupa/kirupa/blob/master/reactjs/helloworld.htmFaggoting
I have added your code from that link as a snippet to your question. However, it seems to work..Filing
I get the same error on my projects as well as on the code snippet above. It seems to work using a cached version from last week. Disabling my cache caused me to experience this issue.Pergrim
F
6

It turns out if I take out "crossorigin" then it works in Chrome!

There's a discussion of this at With <script crossorigin='anonymous'>, why is a script "blocked by CORS policy"?

Faggoting answered 29/1, 2019 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.