-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently all scripts that are required for every page are loaded as …
…part of the bootstrap scripts API in React. Unfortunately this loads them all as sync scripts and thus requires preloading which increases their priority higher than they might otherwise be causing things like images to load later than desired, blocking paint. We can improve this by only using one script for bootstrapping and having the rest pre-initialized. This only works because all of these scripts are webpack runtime or chunks and can be loaded in any order asynchronously. With this change we should see improvements in LCP and other metrics as preloads for images are favored over loading scripts
- Loading branch information
Showing
4 changed files
with
100 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { BuildManifest } from '../get-page-files' | ||
|
||
import ReactDOM from 'react-dom' | ||
|
||
export function getRequiredScripts( | ||
buildManifest: BuildManifest, | ||
assetPrefix: string, | ||
SRIManifest: undefined | Record<string, string>, | ||
qs: string | ||
): [() => void, string | { src: string; integrity: string }] { | ||
let preinitScripts: () => void | ||
let preinitScriptCommands: string[] = [] | ||
let bootstrapScript: string | { src: string; integrity: string } = '' | ||
const files = buildManifest.rootMainFiles | ||
if (files.length === 0) { | ||
throw new Error( | ||
'Invariant: missing bootstrap script. This is a bug in Next.js' | ||
) | ||
} | ||
if (SRIManifest) { | ||
bootstrapScript = { | ||
src: `${assetPrefix}/_next/` + files[0] + qs, | ||
integrity: SRIManifest[files[0]], | ||
} | ||
for (let i = 1; i < files.length; i++) { | ||
const src = `${assetPrefix}/_next/` + files[i] + qs | ||
const integrity = SRIManifest[files[i]] | ||
preinitScriptCommands.push(src, integrity) | ||
} | ||
preinitScripts = () => { | ||
// preinitScriptCommands is a double indexed array of src/integrity pairs | ||
for (let i = 0; i < preinitScriptCommands.length; i += 2) { | ||
ReactDOM.preinit(preinitScriptCommands[i], { | ||
as: 'script', | ||
integrity: preinitScriptCommands[i + 1], | ||
}) | ||
} | ||
} | ||
} else { | ||
bootstrapScript = `${assetPrefix}/_next/` + files[0] + qs | ||
for (let i = 1; i < files.length; i++) { | ||
const src = `${assetPrefix}/_next/` + files[i] + qs | ||
preinitScriptCommands.push(src) | ||
} | ||
preinitScripts = () => { | ||
// preinitScriptCommands is a singled indexed array of src values | ||
for (let i = 0; i < preinitScriptCommands.length; i++) { | ||
ReactDOM.preinit(preinitScriptCommands[i], { | ||
as: 'script', | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return [preinitScripts, bootstrapScript] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default async function Page() { | ||
return ( | ||
<div id="anchor">hello, bootstrap scripts should appear after this div</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters