MCPcopy
hub / github.com/vercel/next.js / appendScript

Function appendScript

packages/next/src/client/route-loader.ts:148–171  ·  view source on GitHub ↗
(
  src: TrustedScriptURL | string,
  script?: HTMLScriptElement
)

Source from the content-addressed store, hash-verified

146}
147
148function 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.

Callers 1

maybeExecuteScriptFunction · 0.85

Calls 2

markAssetErrorFunction · 0.85
rejectFunction · 0.50

Tested by

no test coverage detected