Repository: vercel/next.js
Pull request: [turbopack] Install late loaded script and link tags at the end of the `head` instead of the end of the `body`

### What?
Append CSS and JS chunks to `document.head` instead of `document.body` in the Turbopack runtime.

### Why?
This change improves webpack compatibility by ensuring that dynamically loaded CSS and JS resources are appended to the document head rather than the body, matching webpack's behavior.   We don't believe this should have a performance or behavior impact, however aligning with webpack should eliminate rare incompatibilities with applications that might be confused by the presence of these script tags in the body.

One delta with webpack does remain, webpack removes the added script tags from the DOM after they load apparently to workaround a memory leak in IE.  We are not bothering with this.

See also: https://github.com/webpack/webpack/blob/a11302288d2a8851ff89405a122c1d04709574ea/lib/runtime/LoadScriptRuntimeModule.js#L167

Closes PACK-5028

Changed files:
filename: turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts
patch: @@ -187,7 +187,8 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map()
             // loaded instantly.
             resolver.resolve()
           }
-          document.body.appendChild(link)
+          // Append to the `head` for webpack compatibility.
+          document.head.appendChild(link)
         }
       } else if (isJs(chunkUrl)) {
         const previousScripts = document.querySelectorAll(
@@ -210,7 +211,8 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map()
           script.onerror = () => {
             resolver.reject()
           }
-          document.body.appendChild(script)
+          // Append to the `head` for webpack compatibility.
+          document.head.appendChild(script)
         }
       } else {
         throw new Error(`can't infer type of chunk from URL ${chunkUrl}`)

filename: turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/b1abf_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js
patch: @@ -1602,7 +1602,8 @@ async function loadWebAssemblyModule(_source, wasmChunkPath, _edgeModule) {
                         // loaded instantly.
                         resolver.resolve();
                     };
-                    document.body.appendChild(link);
+                    // Append to the `head` for webpack compatibility.
+                    document.head.appendChild(link);
                 }
             } else if (isJs(chunkUrl)) {
                 const previousScripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
@@ -1623,7 +1624,8 @@ async function loadWebAssemblyModule(_source, wasmChunkPath, _edgeModule) {
                     script.onerror = ()=>{
                         resolver.reject();
                     };
-                    document.body.appendChild(script);
+                    // Append to the `head` for webpack compatibility.
+                    document.head.appendChild(script);
                 }
             } else {
                 throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);