Unverified Commit 650ebed7 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

doc: prettify docs for next script (#26572)



x-ref: https://github.com/vercel/next.js/pull/26518#issuecomment-867566010

## Documentation / Examples

- [x] Make sure the linting passes
parent 0b79b6d7
...@@ -35,7 +35,7 @@ Previously, you needed to define `script` tags inside the `Head` of your Next.js ...@@ -35,7 +35,7 @@ Previously, you needed to define `script` tags inside the `Head` of your Next.js
// pages/index.js // pages/index.js
import Head from 'next/head' import Head from 'next/head'
function Home() { export default function Home() {
return ( return (
<> <>
<Head> <Head>
...@@ -54,7 +54,7 @@ With `next/script`, you no longer need to wrap scripts in `next/head`. Further, ...@@ -54,7 +54,7 @@ With `next/script`, you no longer need to wrap scripts in `next/head`. Further,
// pages/index.js // pages/index.js
import Script from 'next/script' import Script from 'next/script'
function Home() { export default function Home() {
return ( return (
<> <>
<Script src="https://www.google-analytics.com/analytics.js" /> <Script src="https://www.google-analytics.com/analytics.js" />
...@@ -69,33 +69,54 @@ function Home() { ...@@ -69,33 +69,54 @@ function Home() {
```js ```js
import Script from 'next/script' import Script from 'next/script'
;<Script
export default function Home() {
return (
<>
<Script
src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver" src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver"
strategy="beforeInteractive" strategy="beforeInteractive"
/> />
</>
)
}
``` ```
### Lazy-Loading ### Lazy-Loading
```js ```js
import Script from 'next/script' import Script from 'next/script'
;<Script
export default function Home() {
return (
<>
<Script
src="https://connect.facebook.net/en_US/sdk.js" src="https://connect.facebook.net/en_US/sdk.js"
strategy="lazyOnload" strategy="lazyOnload"
/> />
</>
)
}
``` ```
### Executing Code After Loading (`onLoad`) ### Executing Code After Loading (`onLoad`)
```js ```js
import Script from 'next/script' import Script from 'next/script'
;<Script
export default function Home() {
return (
<>
<Script
id="stripe-js" id="stripe-js"
src="https://js.stripe.com/v3/" src="https://js.stripe.com/v3/"
onLoad={() => { onLoad={() => {
this.setState({ stripe: window.Stripe('pk_test_12345') }) this.setState({ stripe: window.Stripe('pk_test_12345') })
}} }}
/> />
</>
)
}
``` ```
### Inline Scripts ### Inline Scripts
...@@ -120,10 +141,17 @@ import Script from 'next/script' ...@@ -120,10 +141,17 @@ import Script from 'next/script'
```js ```js
import Script from 'next/script' import Script from 'next/script'
;<Script
export default function Home() {
return (
<>
<Script
src="https://www.google-analytics.com/analytics.js" src="https://www.google-analytics.com/analytics.js"
id="analytics" id="analytics"
nonce="XUENAJFW" nonce="XUENAJFW"
data-test="analytics" data-test="analytics"
/> />
</>
)
}
``` ```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment