-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Script component docs #25471
Merged
Merged
Script component docs #25471
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4311622
Script component docs
janicklas-ralph 24d4d26
Merge branch 'canary' of github.com:vercel/next.js into script
janicklas-ralph b54bfff
Update docs with examples and feedback changes
janicklas-ralph 45e2dd3
Fix typo
janicklas-ralph 2862abf
Add example
janicklas-ralph e6db822
Merge branch 'canary' of github.com:vercel/next.js into script
janicklas-ralph 53fdcd1
Update text
janicklas-ralph fee393f
Apply suggestions from code review
ijjk e4e70a5
Merge branch 'canary' into script
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
description: Next.js helps you optimize loading third-party scripts with the built-in next/script component. | ||
--- | ||
|
||
# Script Component | ||
|
||
Since version **11**, Next.js has a built-in Script component. | ||
|
||
Example of usage | ||
|
||
```js | ||
import Script from 'next/script' | ||
|
||
// Before | ||
<Script | ||
async | ||
src="https://www.google-analytics.com/analytics.js" | ||
/> | ||
|
||
// After | ||
<Script | ||
src="https://www.google-analytics.com/analytics.js" | ||
/> | ||
``` | ||
|
||
Three loading strategies will be initially supported for wrapping third-party scripts: | ||
|
||
- beforeInteractive | ||
- script is fetched and executed _before_ page is interactive (i.e. before self-bundled javascript is executed) | ||
- script is injected in SSR’s HTML - similar to self-bundled JS | ||
- afterInteractive (**default**) | ||
- script is fetched and executed _after_ page is interactive (i.e. after self-bundled javascript is executed) | ||
- script is injected during hydration and will execute soon after hydration | ||
- lazyOnload | ||
- script is injected at onload, and will execute in a subsequent idle period (using `requestIdleCallback`) | ||
|
||
NOTE: above strategies work the same for inline scripts wrapped with ScriptLoader. | ||
|
||
Example scenarios | ||
|
||
```js | ||
import Script from 'next/script' | ||
|
||
|
||
// Loading polyfills before-interactive | ||
<Script | ||
src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver" | ||
strategy="beforeInteractive" | ||
></Script> | ||
|
||
// Lazy load FB scripts | ||
<Script | ||
src="https://connect.facebook.net/en_US/sdk.js" | ||
strategy="lazyOnload" | ||
></Script> | ||
|
||
// Use the onLoad callback to execute code on script load | ||
<Script id="stripe-js" src="https://js.stripe.com/v3/" onLoad={() => { | ||
this.setState({stripe: window.Stripe('pk_test_12345')}); | ||
}}></Script> | ||
|
||
// Loading strategy works for inline scripts too | ||
<Script strategy="lazyOnload"> | ||
{`document.getElementById('banner').removeClass('hidden')`} | ||
</Script> | ||
|
||
// or | ||
<Script | ||
dangerouslySetInnerHTML={{ | ||
__html: `document.getElementById('banner').removeClass('hidden')`, | ||
}} | ||
> | ||
</Script> | ||
|
||
// All script attributes are forwarded to the final element | ||
<Script | ||
src="https://www.google-analytics.com/analytics.js" | ||
id="analytics" | ||
nonce="XUENAJFW" | ||
data-test="analytics" | ||
></Script> | ||
``` | ||
|
||
## Which third-party scripts to wrap with Script Loader | ||
|
||
We recommend the following Script Loader strategies for these categories of third-party scripts | ||
|
||
| Loading strategy | third-party categories | | ||
| ----------------- | -------------------------------------------------------------------------------------------- | | ||
| beforeInteractive | polyfill.io<br>Bot detection, security & authentication<br>User consent management (GDPR) | | ||
| afterInteractive | Tag-managers<br>Analytics | | ||
| lazyOnload | customer relationship management eg. Google feedback, chat support widget<br>social networks | |
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.
I wonder if we need to explain / link to hydration.
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.
Do we explain how hydration happens in NextJS or just what hydration means in react?
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.
We don't anywhere currently, as far as I know