-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
next/script: Correctly apply async and defer props #52939
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9c6cf4c
next/script: Correctly apply async and defer props
domdomegg 9556454
Fix build
domdomegg 01482a5
Standardize head and script props rendering
domdomegg 515e187
Merge branch 'canary' into fix-52935
domdomegg 130f0f0
Add tests, harmonize next/head and next/script code
domdomegg a98c6ab
Address review feedback
domdomegg e9c26d0
Merge branch 'canary' into fix-52935
domdomegg bb5628a
Merge branch 'canary' into fix-52935
domdomegg b071027
Merge branch 'canary' into fix-52935
timneutkens 28d4a2a
Address review feedback
domdomegg 610548f
Merge branch 'canary' into fix-52935
domdomegg b85a8f1
Fix CI
domdomegg ac22e77
Merge branch 'canary' of github.com:vercel/next.js into fix-52935
domdomegg b693bf0
Fix typo
domdomegg 2267650
Merge remote-tracking branch 'upstream/canary' into fix-52935
domdomegg c9e638f
Merge branch 'canary' into fix-52935
domdomegg 3fa006d
Merge branch 'canary' into fix-52935
domdomegg 1ba1141
Merge branch 'canary' into fix-52935
samcx 194cc9f
Merge branch 'canary' into fix-52935
domdomegg 848510f
Fix lint issue
domdomegg 97a8e66
Merge branch 'canary' into fix-52935
samcx 132a960
Merge branch 'canary' into fix-52935
domdomegg 9256f1f
Merge branch 'canary' into fix-52935
ijjk 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 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
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,59 @@ | ||
const DOMAttributeNames: Record<string, string> = { | ||
acceptCharset: 'accept-charset', | ||
className: 'class', | ||
htmlFor: 'for', | ||
httpEquiv: 'http-equiv', | ||
noModule: 'noModule', | ||
} | ||
|
||
const ignoreProps = [ | ||
'onLoad', | ||
'onReady', | ||
'dangerouslySetInnerHTML', | ||
'children', | ||
'onError', | ||
'strategy', | ||
'stylesheets', | ||
] | ||
|
||
function isBooleanScriptAttribute( | ||
attr: string | ||
): attr is 'async' | 'defer' | 'noModule' { | ||
return ['async', 'defer', 'noModule'].includes(attr) | ||
} | ||
|
||
export function setAttributesFromProps(el: HTMLElement, props: object) { | ||
for (const [p, value] of Object.entries(props)) { | ||
if (!props.hasOwnProperty(p)) continue | ||
if (ignoreProps.includes(p)) continue | ||
|
||
// we don't render undefined props to the DOM | ||
if (value === undefined) { | ||
continue | ||
} | ||
|
||
const attr = DOMAttributeNames[p] || p.toLowerCase() | ||
|
||
if (el.tagName === 'SCRIPT' && isBooleanScriptAttribute(attr)) { | ||
// Correctly assign boolean script attributes | ||
// https://github.com/vercel/next.js/pull/20748 | ||
;(el as HTMLScriptElement)[attr] = !!value | ||
} else { | ||
el.setAttribute(attr, String(value)) | ||
} | ||
|
||
// Remove falsy non-zero boolean attributes so they are correctly interpreted | ||
// (e.g. if we set them to false, this coerces to the string "false", which the browser interprets as true) | ||
if ( | ||
value === false || | ||
(el.tagName === 'SCRIPT' && | ||
isBooleanScriptAttribute(attr) && | ||
(!value || value === 'false')) | ||
) { | ||
// Call setAttribute before, as we need to set and unset the attribute to override force async: | ||
// https://html.spec.whatwg.org/multipage/scripting.html#script-force-async | ||
el.setAttribute(attr, '') | ||
el.removeAttribute(attr) | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
test/development/pages-dir/client-navigation/fixture/pages/script.js
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,11 @@ | ||
import React from 'react' | ||
import Script from 'next/script' | ||
|
||
export default () => ( | ||
<div> | ||
<h1>I am a page to test next/script</h1> | ||
<Script src="/test-async-true.js" async /> | ||
<Script src="/test-async-false.js" async={false} /> | ||
<Script src="/test-defer.js" defer /> | ||
</div> | ||
) |
File renamed without changes.
Empty file.
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
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
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.
Couldn't we avoid setting the initial value here for the
'false'
case and then that should remove the need for thesetAttribute(attr, ''); removeAttribute(attr)
condition below?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.
Unfortunately, this will have the same problem that this PR is trying to fix:
false
, this will enter this if condition and set the attribute tofalse
, which HTML casts to"false"
which is interpreted as true (non-empty string)"false"
, this will enter the else condition and set the attribute to"false"
which is interpreted as true (non-empty string)This also breaks for script elements, which when inserted by JavaScript automatically default to async=true. So for async=false we need to do this setAttribute/removeAttribute trick explicitly. See https://html.spec.whatwg.org/multipage/scripting.html#script-force-async