Add documentation about <script crossorigin> for React CDN usage

This commit is contained in:
Dan Abramov
2017-08-03 16:14:51 +01:00
committed by Dan Abramov
parent 80255c8d3b
commit bc472d8389
2 changed files with 18 additions and 4 deletions
+18 -4
View File
@@ -218,21 +218,35 @@ Learn [how to tell if your website is serving the right version of React](/react
If you don't want to use npm to manage client packages, the `react` and `react-dom` npm packages also provide single-file distributions in `dist` folders, which are hosted on a CDN:
```html
<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script crossorigin src="https://unpkg.com/react@15/dist/react.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
```
The versions above are only meant for development, and are not suitable for production. Minified and optimized production versions of React are available at:
```html
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script crossorigin src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
```
To load a specific version of `react` and `react-dom`, replace `15` with the version number.
If you use Bower, React is available via the `react` package.
#### Why the `crossorigin` Attribute?
If you serve React from a CDN, we recommend to keep the [`crossorigin`](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes) attribute set:
```html
<script crossorigin src="..."></script>
```
We also recommend to verify that the CDN you are using sets the `Access-Control-Allow-Origin: *` HTTP header:
![Access-Control-Allow-Origin: *](/react/img/docs/cdn-cors-header.png)
This enables better [error handling experience](/react/blog/2017/07/26/error-handling-in-react-16.html) in React 16 and later.
<script>
/**
* The code below is based on a snippet from React Native Getting Started page.
Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB