Bird
Raised Fist0
NextJSframework~10 mins

Script component for third-party scripts in NextJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Script component for third-party scripts
Start Next.js Page
↓
Import Script Component
↓
Add <Script> with src and strategy
↓
Next.js Loads Script Based on Strategy
↓
Script Executes in Browser
↓
Page Renders with Script Effects
This flow shows how Next.js loads and runs third-party scripts using the Script component with different loading strategies.
Execution Sample
NextJS
import Script from 'next/script';

export default function Page() {
  return (
    <>
      <Script src="https://example.com/script.js" strategy="lazyOnload" />
      <div>Hello World</div>
    </>
  );
}
This code loads a third-party script lazily after the page loads, then renders a simple message.
Execution Table
StepActionScript StatePage Render StateNotes
1Page starts renderingNot loadedNo DOM yetInitial render begins
2Script component parsedNot loadedPreparing DOMScript tag added with lazyOnload
3Page DOM mountsNot loaded<div>Hello World</div>Page content visible, script not loaded yet
4Browser idle after loadLoading script<div>Hello World</div>Script starts loading lazily
5Script loaded and executedLoaded & executed<div>Hello World</div>Script effects available now
6Page fully interactiveLoaded & executed<div>Hello World</div>User can interact with page and script
💡 Script loaded lazily after page load, page rendered immediately with content.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
scriptStateNot loadedNot loadedLoaded & executedLoaded & executed
pageRenderStateNo DOM<div>Hello World</div><div>Hello World</div><div>Hello World</div>
Key Moments - 2 Insights
Why does the page content appear before the script loads?
Because the script uses strategy="lazyOnload", Next.js renders the page immediately and loads the script only after the page is fully loaded (see execution_table step 3 and 4).
What happens if we change strategy to "beforeInteractive"?
The script loads and executes before the page becomes interactive, delaying page interactivity but ensuring script is ready early (not shown in this trace but changes scriptState timing).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the script start loading?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Script State' column in execution_table rows for when loading begins.
According to variable_tracker, what is the pageRenderState after step 3?
A<div>Hello World</div>
BScript loaded
CNo DOM yet
DPage fully interactive
💡 Hint
Look at the 'pageRenderState' row and the 'After Step 3' column.
If we remove the Script component, how would the execution_table change?
AScript would load immediately
BPage would not render
CScript State would remain 'Not loaded' throughout
DPage would show an error
💡 Hint
Without Script, no script loading steps appear; see scriptState in variable_tracker.
Concept Snapshot
Next.js Script component loads third-party scripts.
Use 'src' for script URL.
Use 'strategy' to control loading time:
- 'beforeInteractive': load early,
- 'lazyOnload': load after page load.
Page renders immediately; script loads per strategy.
Full Transcript
This visual trace shows how Next.js uses the Script component to load third-party scripts. The page starts rendering and adds the Script tag with a lazyOnload strategy. The page content appears immediately while the script loads after the page finishes loading. The script state changes from not loaded to loaded and executed after the browser is idle. Variables track script loading and page rendering states. Key moments clarify why the page shows content before the script loads and how changing strategy affects timing. Quiz questions test understanding of script loading steps and page rendering states.

Practice

(1/5)
1. What is the main purpose of the Script component in Next.js?
easy
A. To replace all JavaScript code in the app
B. To safely load third-party scripts with control over when they run
C. To style components using CSS
D. To manage server-side rendering of React components

Solution

  1. Step 1: Understand the role of the Script component

    The Script component is designed to load third-party scripts safely in Next.js apps.
  2. Step 2: Identify what it controls

    It allows control over when the script runs using the strategy prop, improving performance and safety.
  3. Final Answer:

    To safely load third-party scripts with control over when they run -> Option B
  4. Quick Check:

    Script component purpose = safe third-party script loading [OK]
Hint: Remember: Script controls when and how scripts load [OK]
Common Mistakes:
  • Confusing Script with styling or rendering components
  • Thinking Script replaces all JavaScript
  • Assuming Script manages server rendering
2. Which of the following is the correct way to import the Script component in a Next.js file?
easy
A. import Script from 'next/script';
B. import { Script } from 'react';
C. import Script from 'nextjs/script';
D. import Script from 'nextjs/components';

Solution

  1. Step 1: Recall the official import path

    The Script component is imported from the 'next/script' package in Next.js.
  2. Step 2: Check each option

    Only import Script from 'next/script'; uses the correct import path and syntax.
  3. Final Answer:

    import Script from 'next/script'; -> Option A
  4. Quick Check:

    Correct import path = 'next/script' [OK]
Hint: Next.js Script always imports from 'next/script' [OK]
Common Mistakes:
  • Importing from 'react' instead of 'next/script'
  • Using incorrect package names like 'nextjs/script'
  • Using named import instead of default import
3. What will happen when you use the following code in a Next.js component?
import Script from 'next/script';

export default function Page() {
  return (
    <>
      
medium
A. The script loads after the page has loaded, and 'Hello World' is displayed immediately
B. The script blocks page rendering until it loads
C. The script never loads because strategy is incorrect
D. The script loads before the page content and delays 'Hello World' display

Solution

  1. Step 1: Understand the 'lazyOnload' strategy

    The 'lazyOnload' strategy loads the script after the main page load, so it doesn't block rendering.
  2. Step 2: Analyze the component output

    The div with 'Hello World' renders immediately, while the script loads in the background.
  3. Final Answer:

    The script loads after the page has loaded, and 'Hello World' is displayed immediately -> Option A
  4. Quick Check:

    lazyOnload = script loads after page [OK]
Hint: lazyOnload means script loads after page content [OK]
Common Mistakes:
  • Thinking script blocks rendering
  • Confusing strategy names
  • Assuming script never loads
4. Identify the error in this Next.js code using the Script component:
import Script from 'next/script';

export default function Page() {
  return (
    <>
      
      
Welcome
</> ); }
medium
A. The Script component must have a src attribute when using strategy
B. Inline script content must be wrapped in a <script> tag inside Script
C. The strategy 'beforeInteractive' is invalid
D. Script component cannot have children; inline scripts require the 'dangerouslySetInnerHTML' prop

Solution

  1. Step 1: Check how inline scripts are added

    Inline scripts in Next.js Script component require the 'dangerouslySetInnerHTML' prop, not children.
  2. Step 2: Verify the given code

    The code incorrectly places JavaScript code as children, which is invalid and causes an error.
  3. Final Answer:

    Script component cannot have children; inline scripts require the 'dangerouslySetInnerHTML' prop -> Option D
  4. Quick Check:

    Inline scripts need dangerouslySetInnerHTML [OK]
Hint: Inline scripts use dangerouslySetInnerHTML, not children [OK]
Common Mistakes:
  • Putting inline JS code as children inside Script
  • Assuming strategy names are invalid
  • Thinking src is mandatory for all Script uses
5. You want to load a third-party analytics script only after the user interacts with the page (e.g., clicks a button). Which is the best way to do this using Next.js Script component?
hard
A. Use strategy="lazyOnload" and place Script in the component
B. Use strategy="worker" to run script in a web worker
C. Use strategy="afterInteractive" and conditionally render Script after user click
D. Use strategy="beforeInteractive" to load script immediately

Solution

  1. Step 1: Understand the strategies

    'afterInteractive' loads scripts after the page is interactive, suitable for user-triggered loading.
  2. Step 2: Apply conditional rendering

    Render the Script component only after user interaction (like a button click) to delay loading until needed.
  3. Final Answer:

    Use strategy="afterInteractive" and conditionally render Script after user click -> Option C
  4. Quick Check:

    Conditional afterInteractive = user-triggered load [OK]
Hint: Render Script after user action with afterInteractive strategy [OK]
Common Mistakes:
  • Using lazyOnload which loads regardless of user action
  • Using beforeInteractive which loads too early
  • Assuming worker strategy runs scripts like normal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard