|
| 1 | +--- |
| 2 | +title: preinit |
| 3 | +canary: true |
| 4 | +--- |
| 5 | + |
| 6 | +<Canary> |
| 7 | + |
| 8 | +The `preinit` function is currently only available in React's Canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels). |
| 9 | + |
| 10 | +</Canary> |
| 11 | + |
| 12 | +<Note> |
| 13 | + |
| 14 | +[React-based frameworks](/learn/start-a-new-react-project) frequently handle resource loading for you, so you might not have to call this API yourself. Consult your framework's documentation for details. |
| 15 | + |
| 16 | +</Note> |
| 17 | + |
| 18 | +<Intro> |
| 19 | + |
| 20 | +`preinit` lets you eagerly fetch and evaluate a stylesheet or external script. |
| 21 | + |
| 22 | +```js |
| 23 | +preinit("https://example.com/script.js", {as: "style"}); |
| 24 | +``` |
| 25 | + |
| 26 | +</Intro> |
| 27 | + |
| 28 | +<InlineToc /> |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Reference {/*reference*/} |
| 33 | + |
| 34 | +### `preinit(href, options)` {/*preinit*/} |
| 35 | + |
| 36 | +To preinit a script or stylesheet, call the `preinit` function from `react-dom`. |
| 37 | + |
| 38 | +```js |
| 39 | +import { preinit } from 'react-dom'; |
| 40 | + |
| 41 | +function AppRoot() { |
| 42 | + preinit("https://example.com/script.js", {as: "script"}); |
| 43 | + // ... |
| 44 | +} |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +[See more examples below.](#usage) |
| 49 | + |
| 50 | +The `preinit` function provides the browser with a hint that it should start downloading and executing the given resource, which can save time. Scripts that you `preinit` are executed when they finish downloading. Stylesheets that you preinit are inserted into the document, which causes them to go into effect right away. |
| 51 | + |
| 52 | +#### Parameters {/*parameters*/} |
| 53 | + |
| 54 | +* `href`: a string. The URL of the resource you want to download and execute. |
| 55 | +* `options`: an object. It contains the following properties: |
| 56 | + * `as`: a required string. The type of resource. Its possible values are `script` and `style`. |
| 57 | + * `precedence`: a string. Required with stylesheets. Says where to insert the stylesheet relative to others. Stylesheets with higher precedence can override those with lower precedence. The possible values are `reset`, `low`, `medium`, `high`. |
| 58 | + * `crossOrigin`: a string. The [CORS policy](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) to use. Its possible values are `anonymous` and `use-credentials`. It is required when `as` is set to `"fetch"`. |
| 59 | + * `integrity`: a string. A cryptographic hash of the resource, to [verify its authenticity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity). |
| 60 | + * `nonce`: a string. A cryptographic [nonce to allow the resource](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce) when using a strict Content Security Policy. |
| 61 | + * `fetchPriority`: a string. Suggests a relative priority for fetching the resource. The possible values are `auto` (the default), `high`, and `low`. |
| 62 | + |
| 63 | +#### Returns {/*returns*/} |
| 64 | + |
| 65 | +`preinit` returns nothing. |
| 66 | + |
| 67 | +#### Caveats {/*caveats*/} |
| 68 | + |
| 69 | +* Multiple calls to `preinit` with the same `href` have the same effect as a single call. |
| 70 | +* In the browser, you can call `preinit` in any situation: while rendering a component, in an effect, in an event handler, and so on. |
| 71 | +* In server-side rendering or when rendering Server Components, `preinit` only has an effect if you call it while rendering a component or in an async context originating from rendering a component. Any other calls will be ignored. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Usage {/*usage*/} |
| 76 | + |
| 77 | +### Preiniting when rendering {/*preiniting-when-rendering*/} |
| 78 | + |
| 79 | +Call `preinit` when rendering a component if you know that it or its children will use a specific resource, and you're OK with the resource being evaluated and thereby taking effect immediately upon being downloaded. |
| 80 | + |
| 81 | +<Recipes titleText="Examples of preiniting"> |
| 82 | + |
| 83 | +#### Preiniting an external script {/*preiniting-an-external-script*/} |
| 84 | + |
| 85 | +```js |
| 86 | +import { preinit } from 'react-dom'; |
| 87 | + |
| 88 | +function AppRoot() { |
| 89 | + preinit("https://example.com/script.js", {as: "script"}); |
| 90 | + return ...; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +If you want the browser to download the script but not to execute it right away, use [`preload`](/reference/react-dom/preload) instead. If you want to load an ESM module, use [`preinitModule`](/reference/react-dom/preinitModule). |
| 95 | + |
| 96 | +<Solution /> |
| 97 | + |
| 98 | +#### Preiniting a stylesheet {/*preiniting-a-stylesheet*/} |
| 99 | + |
| 100 | +```js |
| 101 | +import { preinit } from 'react-dom'; |
| 102 | + |
| 103 | +function AppRoot() { |
| 104 | + preinit("https://example.com/style.css", {as: "style", precedence: "medium"}); |
| 105 | + return ...; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +The `precedence` option, which is required, lets you control the order of stylesheets within the document. Stylesheets with higher precedence can overrule those with lower precedence. |
| 110 | + |
| 111 | +If you want to download the stylesheet but not to insert it into the document right away, use [`preload`](/reference/react-dom/preload) instead. |
| 112 | + |
| 113 | +<Solution /> |
| 114 | + |
| 115 | +</Recipes> |
| 116 | + |
| 117 | +### Preiniting in an event handler {/*preiniting-in-an-event-handler*/} |
| 118 | + |
| 119 | +Call `preinit` in an event handler before transitioning to a page or state where external resources will be needed. This gets the process started earlier than if you call it during the rendering of the new page or state. |
| 120 | + |
| 121 | +```js |
| 122 | +import { preinit } from 'react-dom'; |
| 123 | + |
| 124 | +function CallToAction() { |
| 125 | + const onClick = () => { |
| 126 | + preinit("https://example.com/wizardStyles.css", {as: "style"}); |
| 127 | + startWizard(); |
| 128 | + } |
| 129 | + return ( |
| 130 | + <button onClick={onClick}>Start Wizard</button> |
| 131 | + ); |
| 132 | +} |
| 133 | +``` |
0 commit comments