Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/rendering-script-defer-async.md
1---2title: Use defer or async on Script Tags3impact: HIGH4impactDescription: eliminates render-blocking5tags: rendering, script, defer, async, performance6---78## Use defer or async on Script Tags910**Impact: HIGH (eliminates render-blocking)**1112Script tags without `defer` or `async` block HTML parsing while the script downloads and executes. This delays First Contentful Paint and Time to Interactive.1314- **`defer`**: Downloads in parallel, executes after HTML parsing completes, maintains execution order15- **`async`**: Downloads in parallel, executes immediately when ready, no guaranteed order1617Use `defer` for scripts that depend on DOM or other scripts. Use `async` for independent scripts like analytics.1819**Incorrect (blocks rendering):**2021```tsx22export default function Document() {23return (24<html>25<head>26<script src="https://example.com/analytics.js" />27<script src="/scripts/utils.js" />28</head>29<body>{/* content */}</body>30</html>31)32}33```3435**Correct (non-blocking):**3637```tsx38export default function Document() {39return (40<html>41<head>42{/* Independent script - use async */}43<script src="https://example.com/analytics.js" async />44{/* DOM-dependent script - use defer */}45<script src="/scripts/utils.js" defer />46</head>47<body>{/* content */}</body>48</html>49)50}51```5253**Note:** In Next.js, prefer the `next/script` component with `strategy` prop instead of raw script tags:5455```tsx56import Script from 'next/script'5758export default function Page() {59return (60<>61<Script src="https://example.com/analytics.js" strategy="afterInteractive" />62<Script src="/scripts/utils.js" strategy="beforeInteractive" />63</>64)65}66```6768Reference: [MDN - Script element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer)69