<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Den Smirnoff</title>
    <description>The latest articles on Forem by Den Smirnoff (@trlz).</description>
    <link>https://forem.com/trlz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3804450%2F2a858277-23b7-4de1-adc8-857ce5cb8ef6.png</url>
      <title>Forem: Den Smirnoff</title>
      <link>https://forem.com/trlz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/trlz"/>
    <language>en</language>
    <item>
      <title>Optimizing Next.js Performance: A Practical Case Study (96+ Lighthouse Mobile)</title>
      <dc:creator>Den Smirnoff</dc:creator>
      <pubDate>Wed, 04 Mar 2026 19:30:35 +0000</pubDate>
      <link>https://forem.com/trlz/optimizing-nextjs-performance-a-practical-case-study-96-lighthouse-mobile-31da</link>
      <guid>https://forem.com/trlz/optimizing-nextjs-performance-a-practical-case-study-96-lighthouse-mobile-31da</guid>
      <description>&lt;p&gt;Performance in Next.js is rarely about one “magic” trick.&lt;br&gt;
It’s usually the result of dozens of small architectural decisions made consistently.&lt;/p&gt;

&lt;p&gt;In this article, I’ll walk through the exact techniques that helped me achieve 96+ Lighthouse performance on mobile on a real production project built with Next.js (App Router).&lt;/p&gt;
&lt;h2&gt;
  
  
  Real Production Example
&lt;/h2&gt;

&lt;p&gt;Instead of discussing performance in theory, let’s look at a real production case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project:&lt;/strong&gt; &lt;a href="https://playtrust.net/" rel="noopener noreferrer"&gt;playtrust.net&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Type:&lt;/strong&gt; Content-driven website&lt;br&gt;
&lt;strong&gt;Stack:&lt;/strong&gt; Next.js, ASP.NET Core, Nginx&lt;br&gt;
&lt;strong&gt;Lighthouse (Mobile) result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnz23p5v25dgr43nfpqp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnz23p5v25dgr43nfpqp.jpg" alt="Lighthouse mobile performance" width="800" height="634"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This isn’t a minimal landing page. The page includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A hero (LCP) image&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dynamic content&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Analytics scripts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client components&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server rendering&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;External image sources&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Primary optimization focus:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;LCP image optimization&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client bundle minimization&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Aggressive image caching&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hydration control&lt;br&gt;
Let’s break down the exact technical decisions that led to this result.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  1. Use &lt;code&gt;font-display: swap&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Custom fonts often block rendering (FOIT — Flash of Invisible Text).&lt;br&gt;
If the browser waits for fonts before rendering text, you increase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First Contentful Paint (FCP)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Total Blocking Time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;LCP&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If using &lt;code&gt;next/font&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Inter } from 'next/font/google'

const inter = Inter({
  subsets: ['latin'],
  display: 'swap',
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If using custom fonts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@font-face {
  font-family: 'Inter';
  src: url('/fonts/inter.woff2') format('woff2');
  font-display: swap;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the browser to render text immediately using a fallback font, then swap once the custom font loads.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Minimize Global CSS
&lt;/h2&gt;

&lt;p&gt;Large global CSS files are render-blocking resources.&lt;/p&gt;

&lt;p&gt;Strategy used in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;global.css&lt;/code&gt; -&amp;gt; only resets, variables, base typography&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Component styles -&amp;gt; CSS Modules&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Heavy UI blocks -&amp;gt; dynamically imported&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import dynamic from 'next/dynamic'

const HeavyComponent = dynamic(() =&amp;gt; import('./HeavyComponent'), {
  ssr: false,
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reduces initial CSS payload and improves render timing.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Load Analytics Properly
&lt;/h2&gt;

&lt;p&gt;Third-party scripts are one of the most common performance killers.&lt;br&gt;
Always use &lt;code&gt;next/script&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Script from 'next/script'

&amp;lt;Script
  src="https://www.googletagmanager.com/gtag/js?id=XXXX"
  strategy="afterInteractive"
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Available strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;beforeInteractive&lt;/code&gt; (rarely needed)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;afterInteractive&lt;/code&gt; (recommended default)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;lazyOnload&lt;/code&gt; (best for low-priority scripts)&lt;br&gt;
Never inject analytics directly in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; without strategy control.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Optimize Your LCP Image
&lt;/h2&gt;

&lt;p&gt;On most content-heavy pages, LCP defines your Lighthouse score.&lt;br&gt;
On the reference page, LCP is the hero image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.1 Use WebP (or AVIF)&lt;/strong&gt;&lt;br&gt;
All hero images are converted before deployment.&lt;/p&gt;

&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Smaller file size&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Faster transfer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better LCP stability&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4.2 Proper &lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt; Configuration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Image
  src="/images/hero.webp"
  alt="Duck Hunters"
  priority
  fetchPriority="high"
  width={360}
  height={216}
  sizes="(max-width: 768px) 216px, 360px"
  quality={85}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;priority&lt;/code&gt; -&amp;gt; enables preload&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;fetchPriority="high"&lt;/code&gt; -&amp;gt; increases network priority&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;width / height&lt;/code&gt; -&amp;gt; prevents CLS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sizes -&amp;gt; correct responsive behavior&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;quality -&amp;gt; avoid 100 unless necessary&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4.3 Set Container Dimensions in Advance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fully eliminate layout shifts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div style={{ width: '360px', height: '216px' }}&amp;gt;
  &amp;lt;Image ... /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Predefining layout space ensures stable rendering and CLS = 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Pre-Download External Images at Build Time
&lt;/h2&gt;

&lt;p&gt;Originally, images were fetched from an external storage (S3-like service). That introduced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Extra DNS lookup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TLS handshake&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Potential cold start delays&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unstable TTFB&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; download images during build and store them in &lt;code&gt;/public&lt;/code&gt;.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function generateStaticParams() {
  ...{fetch}...
  const articles = res.data ? res.data.items : []

  if (!fs.existsSync(IMG_DIR)) {
    fs.mkdirSync(IMG_DIR, { recursive: true })
  }

  for (const article of articles) {
    const urls = [
      article.coverImg ? `${BASE_URL + IMG_URL}${article.coverImg}` : null,
    ].filter(Boolean)

    for (const url of urls) {
      const filename = url!.split('/').pop()!
      const path = `${IMG_DIR}/${filename}`

      if (!fs.existsSync(path)) {
        const resp = await fetch(url!)
        const buffer = await resp.arrayBuffer()
        fs.writeFileSync(path, Buffer.from(buffer))
      }
    }
  }

  return articles.map((x) =&amp;gt; ({
    slug: x.slug,
  }))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result: stable LCP without relying on external image latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Configure Image Caching (Nginx Example)
&lt;/h2&gt;

&lt;p&gt;If you’re not using Cloudflare, configure caching at the server level.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location /images/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_cache images_cache;
    proxy_cache_valid 200 365d;
    add_header X-Cache $upstream_cache_status;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Reduces server load&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lowers TTFB&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Speeds up repeat visits&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Be Careful with &lt;code&gt;"use client"&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In the App Router, everything is a Server Component by default.&lt;br&gt;
Every &lt;code&gt;"use client"&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ships JavaScript to the browser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increases bundle size&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adds hydration cost&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can delay above-the-fold rendering&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule applied:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Hero section -&amp;gt; Server Component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Above-the-fold -&amp;gt; minimal client logic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interactive parts -&amp;gt; below the fold&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Monitor TTFB
&lt;/h2&gt;

&lt;p&gt;If your TTFB is high:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check server performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid heavy SSR computations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use caching aggressively&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimize network chain&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable HTTP/2 or HTTP/3&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;High performance in Next.js is not about hacks.&lt;br&gt;
It’s about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Reducing render-blocking resources&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Controlling client bundle size&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Treating LCP as a first-class citizen&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Proper caching strategy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear separation of Server vs Client Components&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Performance is an architectural decision — not a final polish step.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>webperf</category>
    </item>
  </channel>
</rss>
