Using Next.js Script Component for Third-Party Scripts
📖 Scenario: You are building a Next.js website that needs to include a third-party analytics script. To keep your site fast and safe, you want to load this script properly using Next.js features.
🎯 Goal: Learn how to add a third-party script using the Next.js Script component to load it safely and efficiently.
📋 What You'll Learn
Create a Next.js functional component named Analytics
Import the Script component from next/script
Add a Script component with the exact src URL https://example.com/analytics.js
Set the strategy prop of Script to afterInteractive
Add an id prop with value analytics-script to the Script component
💡 Why This Matters
🌍 Real World
Many websites need to include third-party scripts like analytics, chat widgets, or ads. Using Next.js Script component helps load these scripts safely and efficiently.
💼 Career
Knowing how to properly add third-party scripts in Next.js is important for frontend developers to optimize site performance and maintain security.
Progress0 / 4 steps
1
Create the Analytics component and import Script
Create a functional component called Analytics in Next.js. Import the Script component from next/script at the top.
NextJS
Hint
Use import Script from 'next/script' and define function Analytics().
2
Add the Script component with src URL
Inside the Analytics component, return a Script component with the src prop set to "https://example.com/analytics.js".
NextJS
Hint
Use <Script src="https://example.com/analytics.js" /> inside the return.
3
Set the strategy prop to afterInteractive
Modify the Script component to include the strategy prop with the value "afterInteractive".
NextJS
Hint
Add strategy="afterInteractive" inside the Script tag.
4
Add an id prop to the Script component
Add an id prop with the value "analytics-script" to the Script component inside the Analytics component.
NextJS
Hint
Add id="analytics-script" inside the Script tag.
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
Step 1: Understand the role of the Script component
The Script component is designed to load third-party scripts safely in Next.js apps.
Step 2: Identify what it controls
It allows control over when the script runs using the strategy prop, improving performance and safety.
Final Answer:
To safely load third-party scripts with control over when they run -> Option B
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
Step 1: Recall the official import path
The Script component is imported from the 'next/script' package in Next.js.
Step 2: Check each option
Only import Script from 'next/script'; uses the correct import path and syntax.
Final Answer:
import Script from 'next/script'; -> Option A
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
Step 1: Understand the 'lazyOnload' strategy
The 'lazyOnload' strategy loads the script after the main page load, so it doesn't block rendering.
Step 2: Analyze the component output
The div with 'Hello World' renders immediately, while the script loads in the background.
Final Answer:
The script loads after the page has loaded, and 'Hello World' is displayed immediately -> Option A
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
Step 1: Check how inline scripts are added
Inline scripts in Next.js Script component require the 'dangerouslySetInnerHTML' prop, not children.
Step 2: Verify the given code
The code incorrectly places JavaScript code as children, which is invalid and causes an error.
Final Answer:
Script component cannot have children; inline scripts require the 'dangerouslySetInnerHTML' prop -> Option D
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
Step 1: Understand the strategies
'afterInteractive' loads scripts after the page is interactive, suitable for user-triggered loading.
Step 2: Apply conditional rendering
Render the Script component only after user interaction (like a button click) to delay loading until needed.
Final Answer:
Use strategy="afterInteractive" and conditionally render Script after user click -> Option C