-
Notifications
You must be signed in to change notification settings - Fork 28.5k
App Router Preinitialize all required scripts except one for bootstrap #53705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
export default async function Page() { | ||
return ( | ||
<div> | ||
This fixture is to assert where the bootstrap scripts and other required | ||
scripts emit during SSR | ||
</div> | ||
) | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick: since this is used four times, you could keep it DRY with a shared function:
Then you can
getSrc(0)
orgetSrc(i)
laterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't bundle the server code all the time yet (I think) and I'm not sure what level of optimization we will end up applying to our bundle when we do. Absent an optimizing step that would inline these function calls it's more performant to just inline the string concatenation.
I realize this is a drop in the bucket compared to overhead in the rest of Next but I think in the long run we're going to want to start caring more about good perf by default and in aggregate little things like this will add up eventually.
There are a couple reasons this would be slower, first a function call itself has overhead. But second the allocation of a new function to create the closure for
assetPrefix
andqs
. There's obviously a tradeoff (getRequireScripts itself is a function that won't be inlined) but since this repeated string concat is clear in it's intention and self contained I think the repetition is not particularly taxing mental cost and it's not a place we're likely to mistakenly update in a broken way in the future.I could make a
getSrc(prefix, filename, querystring)
function that is only allocated once for the module but then it's not really saving much character-wise and still adds a function call overhead so I think it's good how it is