|
| 1 | +--- |
| 2 | +description: Next.js helps you optimize loading third-party scripts with the built-in next/script component. |
| 3 | +--- |
| 4 | + |
| 5 | +# Script Component |
| 6 | + |
| 7 | +Since version **11**, Next.js has a built-in Script component. |
| 8 | + |
| 9 | +Example of usage |
| 10 | + |
| 11 | +```js |
| 12 | +import Script from 'next/script' |
| 13 | + |
| 14 | +// Before |
| 15 | +<Script |
| 16 | + async |
| 17 | + src="https://www.google-analytics.com/analytics.js" |
| 18 | +/> |
| 19 | + |
| 20 | +// After |
| 21 | +<Script |
| 22 | + src="https://www.google-analytics.com/analytics.js" |
| 23 | +/> |
| 24 | +``` |
| 25 | + |
| 26 | +Three loading strategies will be initially supported for wrapping third-party scripts: |
| 27 | + |
| 28 | +- beforeInteractive |
| 29 | + - script is fetched and executed _before_ page is interactive (i.e. before self-bundled javascript is executed) |
| 30 | + - script is injected in SSR’s HTML - similar to self-bundled JS |
| 31 | +- afterInteractive (**default**) |
| 32 | + - script is fetched and executed _after_ page is interactive (i.e. after self-bundled javascript is executed) |
| 33 | + - script is injected during hydration and will execute soon after hydration |
| 34 | +- lazyOnload |
| 35 | + - script is injected at onload, and will execute in a subsequent idle period (using `requestIdleCallback`) |
| 36 | + |
| 37 | +NOTE: above strategies work the same for inline scripts wrapped with ScriptLoader. |
| 38 | + |
| 39 | +Example scenarios |
| 40 | + |
| 41 | +```js |
| 42 | +import Script from 'next/script' |
| 43 | + |
| 44 | + |
| 45 | +// Loading polyfills before-interactive |
| 46 | +<Script |
| 47 | + src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver" |
| 48 | + strategy="beforeInteractive" |
| 49 | +></Script> |
| 50 | + |
| 51 | +// Lazy load FB scripts |
| 52 | +<Script |
| 53 | + src="https://connect.facebook.net/en_US/sdk.js" |
| 54 | + strategy="lazyOnload" |
| 55 | +></Script> |
| 56 | + |
| 57 | +// Use the onLoad callback to execute code on script load |
| 58 | +<Script id="stripe-js" src="https://js.stripe.com/v3/" onLoad={() => { |
| 59 | + this.setState({stripe: window.Stripe('pk_test_12345')}); |
| 60 | +}}></Script> |
| 61 | + |
| 62 | +// Loading strategy works for inline scripts too |
| 63 | +<Script strategy="lazyOnload"> |
| 64 | + {`document.getElementById('banner').removeClass('hidden')`} |
| 65 | +</Script> |
| 66 | + |
| 67 | +// or |
| 68 | +<Script |
| 69 | + dangerouslySetInnerHTML={{ |
| 70 | + __html: `document.getElementById('banner').removeClass('hidden')`, |
| 71 | + }} |
| 72 | +> |
| 73 | +</Script> |
| 74 | + |
| 75 | +// All script attributes are forwarded to the final element |
| 76 | +<Script |
| 77 | + src="https://www.google-analytics.com/analytics.js" |
| 78 | + id="analytics" |
| 79 | + nonce="XUENAJFW" |
| 80 | + data-test="analytics" |
| 81 | +></Script> |
| 82 | +``` |
| 83 | + |
| 84 | +## Which third-party scripts to wrap with Script Loader |
| 85 | + |
| 86 | +We recommend the following Script Loader strategies for these categories of third-party scripts |
| 87 | + |
| 88 | +| Loading strategy | third-party categories | |
| 89 | +| ----------------- | -------------------------------------------------------------------------------------------- | |
| 90 | +| beforeInteractive | polyfill.io<br>Bot detection, security & authentication<br>User consent management (GDPR) | |
| 91 | +| afterInteractive | Tag-managers<br>Analytics | |
| 92 | +| lazyOnload | customer relationship management eg. Google feedback, chat support widget<br>social networks | |
0 commit comments