( src: TrustedScriptURL | string, script?: HTMLScriptElement )
| 146 | } |
| 147 | |
| 148 | function appendScript( |
| 149 | src: TrustedScriptURL | string, |
| 150 | script?: HTMLScriptElement |
| 151 | ): Promise<unknown> { |
| 152 | return new Promise((resolve, reject) => { |
| 153 | script = document.createElement('script') |
| 154 | |
| 155 | // The order of property assignment here is intentional. |
| 156 | // 1. Setup success/failure hooks in case the browser synchronously |
| 157 | // executes when `src` is set. |
| 158 | script.onload = resolve |
| 159 | script.onerror = () => |
| 160 | reject(markAssetError(new Error(`Failed to load script: ${src}`))) |
| 161 | |
| 162 | // 2. Configure the cross-origin attribute before setting `src` in case the |
| 163 | // browser begins to fetch. |
| 164 | script.crossOrigin = process.env.__NEXT_CROSS_ORIGIN! |
| 165 | |
| 166 | // 3. Finally, set the source and inject into the DOM in case the child |
| 167 | // must be appended for fetching to start. |
| 168 | script.src = src as string |
| 169 | document.body.appendChild(script) |
| 170 | }) |
| 171 | } |
| 172 | |
| 173 | // We wait for pages to be built in dev before we start the route transition |
| 174 | // timeout to prevent an un-necessary hard navigation in development. |
no test coverage detected