# <Script>

> Controlling third-party script execution.

Subject: Next.js | Lesson 51 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/script-optimization
Previous: [<Font>](https://codewithanimation.in/nextjs/font-optimization.md) | Next: [Third-Party Integrations](https://codewithanimation.in/nextjs/third-party-integrations.md)

## The <Script> Component

Modern websites rely on heavy third-party scripts for analytics, ads, and customer support widgets.

The Next.js `<Script>` component allows you to explicitly control *when* these scripts load and execute so they don't interfere with your core application code.

## Main Thread Blocking

JavaScript is single-threaded. If a massive third-party analytics script starts parsing in the middle of your page load, the browser cannot render the UI or make your buttons interactive.

By pushing low-priority scripts to execute later, you ensure your app feels lightning fast to the user.

## Execution Strategies

You pass a `strategy` prop to `<Script>`:

1. `beforeInteractive`: Use for critical scripts like bot detection or cookie consent. Executes before React hydrates.

2. `afterInteractive` (Default): Use for tag managers and analytics. Executes immediately after the page becomes interactive.

3. `lazyOnload`: Use for low-priority scripts like chat widgets or social media embeds. Waits until the browser is completely idle.

## Common mistake: The Hydration Killer

A classic beginner mistake is placing a heavy Intercom or Zendesk script tag in the root layout.

The script blocks React from hydrating the page, making the entire application completely unresponsive to clicks for several seconds.

## Try it: The Render Timeline

Watch how changing the Script strategy physically moves the heavy execution block across the browser's render timeline, freeing up the main thread.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/script-optimization

## Check yourself

### Question 1

Which `<Script>` strategy should you use for a heavy customer support chat widget?

- A. beforeInteractive
- B. afterInteractive
- C. lazyOnload
- D. worker

Correct answer: C

Chat widgets are notoriously heavy and low-priority. `lazyOnload` tells the browser to wait until the page is fully loaded and completely idle before downloading the widget.

- Why not A: This blocks the entire page render. Never use this for heavy widgets.
- Why not B: This still runs early enough that it might compete with your app's code.
- Why not D: This is an experimental strategy to run scripts in Web Workers, not the standard for chat widgets.

## Remember this

- Standard `<script>` tags block the main thread and ruin performance.
- Use `<Script strategy="afterInteractive">` (the default) for analytics.
- Use `lazyOnload` for heavy, non-critical scripts like chat widgets.
- Use `beforeInteractive` sparingly, only for critical scripts like cookie consent.
