-
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 examples #31181
Merged
Merged
Script examples #31181
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bcf39bf
Add script component examples
goncy 6675ac7
Update script examples
goncy bc8f777
Refactor code
goncy 90c9c9a
Merge branch 'canary' into script-examples
goncy 91bb4d5
Remove unused files
goncy 9acae0c
Merge branch 'script-examples' of github.com:vercel/next.js into scri…
goncy 394d6a5
Merge branch 'canary' into script-examples
goncy e8b54fa
Merge branch 'canary' into script-examples
goncy cb57ea5
Fix linter
goncy 4d130fc
Merge branch 'script-examples' of github.com:vercel/next.js into scri…
goncy 6570ff7
Merge branch 'canary' into script-examples
goncy 87a18d6
Merge branch 'canary' into script-examples
goncy 69efe73
Fix lint error
goncy a0011bb
Merge branch 'script-examples' of github.com:vercel/next.js into scri…
goncy 85b9a65
Merge branch 'canary' into script-examples
goncy 3adea74
Merge branch 'canary' into script-examples
goncy 16d1cc6
Fix prettier
goncy ce35189
Merge branch 'script-examples' of github.com:vercel/next.js into scri…
goncy 537d182
Merge branch 'canary' into script-examples
goncy 50e6459
Merge branch 'canary' into script-examples
goncy 3ca1a7c
Merge branch 'canary' into script-examples
goncy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# vercel | ||
.vercel |
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,33 @@ | ||
# Script component | ||
|
||
This example shows different strategies that can be used for the [`next/script` component](https://nextjs.org/docs/basic-features/script): | ||
|
||
- [Loading Polyfills](./pages/polyfill.js) | ||
- [Lazy loading](./pages/lazy.js) | ||
- [Executing code after loading](./pages/onload.js) | ||
- [Inline scripts](./pages/inline.js) | ||
- [Forwarding attributes](./pages/attributes.js) | ||
|
||
## Preview | ||
|
||
Preview the example live on [StackBlitz](http://stackblitz.com/): | ||
|
||
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/script-component) | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/script-component&project-name=script-component&repository-name=script-component) | ||
|
||
## How to use | ||
|
||
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: | ||
|
||
```bash | ||
npx create-next-app --example script-component script-component-app | ||
# or | ||
yarn create next-app --example script-component script-component-app | ||
``` | ||
|
||
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
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,13 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"next": "latest", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2" | ||
} | ||
} |
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,20 @@ | ||
import Link from 'next/link' | ||
|
||
import '../styles/global.css' | ||
|
||
const MyApp = ({ Component, pageProps, router }) => { | ||
const pathname = router.pathname | ||
|
||
return ( | ||
<> | ||
<Component {...pageProps} /> | ||
{pathname !== '/' && ( | ||
<Link href="/"> | ||
<a>See all examples</a> | ||
</Link> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default MyApp |
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,22 @@ | ||
import Script from 'next/script' | ||
|
||
export default function Inline() { | ||
return ( | ||
<> | ||
{/* Attributes are forwarded */} | ||
<Script | ||
src="https://www.google-analytics.com/analytics.js" | ||
id="analytics" | ||
nonce="XUENAJFW" | ||
data-test="analytics" | ||
/> | ||
|
||
<main> | ||
<h1>Forwarded attributes</h1> | ||
<h5> | ||
Open devtools and check that attributes has been forwarded correctly. | ||
</h5> | ||
</main> | ||
</> | ||
) | ||
} |
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,36 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Index() { | ||
return ( | ||
<main> | ||
<h1>Script component examples</h1> | ||
<ul> | ||
<li> | ||
<Link href="/polyfill"> | ||
<a>Polyfill</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/lazy"> | ||
<a>Lazy Loading</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/onload"> | ||
<a>Executing code after loading</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/inline"> | ||
<a>Inline scripts</a> | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/attributes"> | ||
<a>Forwarding attributes</a> | ||
</Link> | ||
</li> | ||
</ul> | ||
</main> | ||
) | ||
} |
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,29 @@ | ||
import Script from 'next/script' | ||
|
||
export default function Inline() { | ||
return ( | ||
<> | ||
{/* Execute arbitrary code */} | ||
<Script id="show-banner" strategy="lazyOnload"> | ||
{`document.getElementById('banner').classList.remove('hidden')`} | ||
</Script> | ||
|
||
{/* Or */} | ||
|
||
{/* <Script | ||
id="show-banner" | ||
dangerouslySetInnerHTML={{ | ||
__html: `document.getElementById('banner').classList.remove('hidden')` | ||
}} | ||
/> */} | ||
|
||
<main> | ||
<h1>Inline scripts</h1> | ||
<h5 id="banner" className="hidden"> | ||
This is initially hidden but its being shown because the `Script` | ||
component removed the `hidden` class. | ||
</h5> | ||
</main> | ||
</> | ||
) | ||
} |
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,42 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import Script from 'next/script' | ||
|
||
export default function Lazyload() { | ||
const [log, setLog] = useState([]) | ||
|
||
const addLog = useCallback( | ||
(text) => { | ||
setLog((log) => log.concat({ time: new Date(), text })) | ||
}, | ||
[setLog] | ||
) | ||
|
||
useEffect(() => { | ||
addLog(`Page loaded window.FB is undefined`) | ||
}, [addLog]) | ||
|
||
return ( | ||
<> | ||
{/* We lazy load the FB SDK */} | ||
<Script | ||
src="https://connect.facebook.net/en_US/sdk.js" | ||
strategy="lazyOnload" | ||
onLoad={() => | ||
addLog(`script loaded correctly, window.FB has been populated`) | ||
} | ||
/> | ||
|
||
<main> | ||
<h1>Lazy Loading FB sdk</h1> | ||
<h5>You can check `window.FB` on browser console</h5> | ||
<ul> | ||
{log.map(({ time, text }) => ( | ||
<li key={+time}> | ||
{time.toISOString()}: {text} | ||
</li> | ||
))} | ||
</ul> | ||
</main> | ||
</> | ||
) | ||
} |
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,46 @@ | ||
import { useMemo, useState } from 'react' | ||
import Script from 'next/script' | ||
|
||
export default function Onload() { | ||
const [stripe, setStripe] = useState(null) | ||
const methods = useMemo( | ||
() => | ||
stripe | ||
? Object.entries(stripe.stripe).filter( | ||
([_key, value]) => typeof value === 'function' | ||
) | ||
: [], | ||
[stripe] | ||
) | ||
|
||
function handleLoad() { | ||
const stripe = window.Stripe('pk_test_1234') | ||
|
||
console.log('Stripe loaded: ', stripe) | ||
|
||
setStripe({ stripe }) | ||
} | ||
|
||
return ( | ||
<> | ||
{/* We load Stripe sdk */} | ||
<Script | ||
id="stripe-js" | ||
src="https://js.stripe.com/v3/" | ||
onLoad={handleLoad} | ||
/> | ||
|
||
<main> | ||
<h1>Executing code after loading</h1> | ||
<div> | ||
<p>Stripe methods: </p> | ||
<ul> | ||
{methods.map(([method]) => ( | ||
<li key={method}>{method}</li> | ||
))} | ||
</ul> | ||
</div> | ||
</main> | ||
</> | ||
) | ||
} |
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,48 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import Script from 'next/script' | ||
|
||
import s from '../styles/polyfill.module.css' | ||
|
||
export default function Polyfill() { | ||
const ref = useRef() | ||
const [lastIntersection, setIntersection] = useState(new Date()) | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
(intersections) => { | ||
const isIntersecting = intersections[0]?.isIntersecting | ||
|
||
if (isIntersecting) { | ||
setIntersection(new Date()) | ||
} | ||
}, | ||
{ | ||
rootMargin: '45px', | ||
} | ||
) | ||
|
||
observer.observe(ref.current) | ||
|
||
return () => observer.disconnect() | ||
}, []) | ||
|
||
return ( | ||
<> | ||
{/* We ensure that intersection observer is available by polyfilling it */} | ||
<Script | ||
src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver" | ||
strategy="beforeInteractive" | ||
/> | ||
|
||
<main className={s.container}> | ||
<h1>IntersectionObserver Polyfill</h1> | ||
<h5>Scroll down to see when was the last intersection</h5> | ||
<section className={s.section}> | ||
<span ref={ref}> | ||
Last intersection at {lastIntersection.toTimeString()} | ||
</span> | ||
</section> | ||
</main> | ||
</> | ||
) | ||
} |
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,8 @@ | ||
* { | ||
font-family: sans-serif; | ||
box-sizing: border-box; | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} |
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,6 @@ | ||
.section { | ||
height: 400vh; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} |
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 like your idea of putting together all the different strategies into a single example! I may do the same and consolidate all the other examples into a separate, single example focused on showing different third-party providers (example:
script-component-recipes
)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.
Thanks! If you need any help with that just let me know 🙌