| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | // This client file is in the shared folder because it applies to both SSR and browser contexts. |
| 11 | // It is the configuration of the FlightClient behavior which can run in either environment. |
| 12 | |
| 13 | import type {HintCode, HintModel} from '../server/ReactFlightServerConfigDOM'; |
| 14 | |
| 15 | import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals'; |
| 16 | |
| 17 | import {getCrossOriginString} from './crossOriginStrings'; |
| 18 | |
| 19 | export function dispatchHint<Code: HintCode>( |
| 20 | code: Code, |
| 21 | model: HintModel<Code>, |
| 22 | ): void { |
| 23 | const dispatcher = ReactDOMSharedInternals.d; /* ReactDOMCurrentDispatcher */ |
| 24 | switch (code) { |
| 25 | case 'D': { |
| 26 | const refined = refineModel(code, model); |
| 27 | const href = refined; |
| 28 | dispatcher.D(/* prefetchDNS */ href); |
| 29 | return; |
| 30 | } |
| 31 | case 'C': { |
| 32 | const refined = refineModel(code, model); |
| 33 | if (typeof refined === 'string') { |
| 34 | const href = refined; |
| 35 | dispatcher.C(/* preconnect */ href); |
| 36 | } else { |
| 37 | const href = refined[0]; |
| 38 | const crossOrigin = refined[1]; |
| 39 | dispatcher.C(/* preconnect */ href, crossOrigin); |
| 40 | } |
| 41 | return; |
| 42 | } |
| 43 | case 'L': { |
| 44 | const refined = refineModel(code, model); |
| 45 | const href = refined[0]; |
| 46 | const as = refined[1]; |
| 47 | if (refined.length === 3) { |
| 48 | const options = refined[2]; |
| 49 | dispatcher.L(/* preload */ href, as, options); |
| 50 | } else { |
| 51 | dispatcher.L(/* preload */ href, as); |
| 52 | } |
| 53 | return; |
| 54 | } |
| 55 | case 'm': { |
| 56 | const refined = refineModel(code, model); |
| 57 | if (typeof refined === 'string') { |
| 58 | const href = refined; |
| 59 | dispatcher.m(/* preloadModule */ href); |
| 60 | } else { |
| 61 | const href = refined[0]; |
| 62 | const options = refined[1]; |
| 63 | dispatcher.m(/* preloadModule */ href, options); |
| 64 | } |
| 65 | return; |
| 66 | } |
| 67 | case 'X': { |
| 68 | const refined = refineModel(code, model); |
| 69 | if (typeof refined === 'string') { |
| 70 | const href = refined; |
| 71 | dispatcher.X(/* preinitScript */ href); |
| 72 | } else { |
| 73 | const href = refined[0]; |
| 74 | const options = refined[1]; |
| 75 | dispatcher.X(/* preinitScript */ href, options); |
| 76 | } |
| 77 | return; |
| 78 | } |
| 79 | case 'S': { |
| 80 | const refined = refineModel(code, model); |
| 81 | if (typeof refined === 'string') { |
| 82 | const href = refined; |
| 83 | dispatcher.S(/* preinitStyle */ href); |
| 84 | } else { |
| 85 | const href = refined[0]; |
| 86 | const precedence = refined[1] === 0 ? undefined : refined[1]; |
| 87 | const options = refined.length === 3 ? refined[2] : undefined; |
| 88 | dispatcher.S(/* preinitStyle */ href, precedence, options); |
| 89 | } |
| 90 | return; |
| 91 | } |
| 92 | case 'M': { |
| 93 | const refined = refineModel(code, model); |
| 94 | if (typeof refined === 'string') { |
| 95 | const href = refined; |
| 96 | dispatcher.M(/* preinitModuleScript */ href); |
| 97 | } else { |
| 98 | const href = refined[0]; |
| 99 | const options = refined[1]; |
| 100 | dispatcher.M(/* preinitModuleScript */ href, options); |
| 101 | } |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Flow is having trouble refining the HintModels so we help it a bit. |
| 108 | // This should be compiled out in the production build. |
| 109 | function refineModel<T>(code: T, model: HintModel<any>): HintModel<T> { |
| 110 | return model; |
| 111 | } |
| 112 | |
| 113 | export function preinitModuleForSSR( |
| 114 | href: string, |
| 115 | nonce: ?string, |
| 116 | crossOrigin: ?string, |
| 117 | ) { |
| 118 | ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ |
| 119 | .M(/* preinitModuleScript */ href, { |
| 120 | crossOrigin: getCrossOriginString(crossOrigin), |
| 121 | integrity: undefined, |
| 122 | nonce, |
| 123 | fetchPriority: undefined, |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | export function preinitScriptForSSR( |
| 128 | href: string, |
| 129 | nonce: ?string, |
| 130 | crossOrigin: ?string, |
| 131 | ) { |
| 132 | ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */ |
| 133 | .X(/* preinitScript */ href, { |
| 134 | crossOrigin: getCrossOriginString(crossOrigin), |
| 135 | integrity: undefined, |
| 136 | fetchPriority: undefined, |
| 137 | nonce, |
| 138 | }); |
| 139 | } |