Skip to content
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 9 commits into from
Jun 15, 2021
47 changes: 47 additions & 0 deletions docs/basic-features/script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
description: Next.js supports built-in third-party Script loading optimization
ijjk marked this conversation as resolved.
Show resolved Hide resolved
---

# Script Component

Since version **10.2**, Next.js has built-in Script component.
ijjk marked this conversation as resolved.
Show resolved Hide resolved

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 3Ps:
ijjk marked this conversation as resolved.
Show resolved Hide resolved

- before-interactive
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear how I use these options - is there a prop you pass to the component?

- 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
- after-interactive (**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
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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

- lazy-onload
- script is injected at onload, and will execute in a subsequent idle period (using rIC)
ijjk marked this conversation as resolved.
Show resolved Hide resolved

NOTE: above strategies work the same for inline scripts wrapped with ScriptLoader.

## Which 3Ps to wrap with Script Loader

We recommend the following Script Loader strategies for these categories of 3Ps
ijjk marked this conversation as resolved.
Show resolved Hide resolved

| Loading strategy | 3P categories |
| ------------------ | -------------------------------------------------------------------------------------------- |
| before-interactive | polyfill.io<br>Bot detection, security & authentication<br>User consent management (GDPR) |
| after-interactive | Tag-managers<br>Analytics |
| lazy-onload | customer relationship management eg. Google feedback, chat support widget<br>social networks |