How to Improve PageSpeed Score with Next.js

Pagespeed is crucial for both user experience and SEO. For Next.js applications, optimizing your PageSpeed score can significantly impact how users interact with your site and how search engines rank your pages. In this comprehensive guide, I'll walk through proven strategies to boost your Next.js application's performance.

Why PageSpeed Matters

Before diving into optimization techniques, let's understand why PageSpeed is worth your attention:

  • User Experience: Sites loading in 5 seconds see 25% more bounce rates than those loading in 1 second

  • SEO Impact: Google uses page speed as a ranking factor, particularly for mobile searches

  • Conversion Rates: Faster sites convert users more effectively - every second counts

Understanding PageSpeed Insights Metrics

PageSpeed Insights measures your site's performance through several key metrics:

  • First Contentful Paint (FCP): How quickly content first appears

  • Largest Contentful Paint (LCP): When the largest content element becomes visible

  • Cumulative Layout Shift (CLS): Measures visual stability

  • Time to Interactive (TTI): When the page becomes fully interactive

  • Total Blocking Time (TTFB): Measures responsiveness

Each contributes to your overall score, with mobile and desktop being scored separately.

Key Optimization Strategies

1. Image Optimization

Images are often the largest culprits in slowing down websites. Next.js provides tools to address this:

  • Use the Next.js Image Component:

import Image from 'next/image'

// Instead of standard HTML img tag
<Image
  src="/profile.jpg"
  width={400}
  height={300}
  alt="Profile"
  loading="lazy"
/>
  • Implement Responsive Images: One of the biggest mistakes is loading massive images for small displays:

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <img src="medium.jpg" alt="Image description" loading="lazy">
</picture>
  • Use Modern Formats: Implement WebP or AVIF formats for better compression. Tools like compressor.io can help reduce image sizes before uploading.

2. Lazy Loading Implementation

  • Defer Non-Critical Resources: Use the Next.js Image component with lazy loading to only load images when they're about to enter the viewport:

<Image src="/path/to/image.jpg" alt="Image" loading="lazy" />
  • Prevent Layout Shifts: Ensure images have defined dimensions to avoid layout shifts during loading.

3. Reduce JavaScript Payload

  • Analyze Bundle Sizes: Use @next/bundle-analyzer to visualize and identify large libraries and dependencies:

npm install @next/bundle-analyzer

Then configure in your next.config.js:

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true'
})
module.exports = withBundleAnalyzer({})
  • Implement Dynamic Imports: Load components only when needed:

const Component = dynamic(() => import('./HeavyComponent'));

4. Server-Side Rendering (SSR) & Static Site Generation (SSG)

Next.js shines with its built-in rendering options:

  • Use SSR for Dynamic Content: Implement getServerSideProps for pages that need fresh data on every request:

export async function getServerSideProps(context) {
  const data = await fetchAPI('/api/data')
  return { props: { data } }
}
  • Leverage SSG for Static Content: Use getStaticProps for pages with content that doesn't change frequently:

export async function getStaticProps() {
  const data = await fetchAPI('/api/static-data')
  return {
    props: { data },
    revalidate: 60 // Re-generate page every 60 seconds if needed
  }
}

5. Optimize Component Rendering

  • Memoize Components: Prevent unnecessary re-renders with React's optimization hooks:

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
  • Use React.memo: Wrap components to skip re-rendering when props haven't changed:

const OptimizedComponent = React.memo(function MyComponent(props) {
  // Your component logic
});

6. CORS Implementation

If you're making custom API calls, ensure proper CORS handling to avoid performance penalties:

function responseJson(payload, data, responseInit) {
  const response = Response.json(data, responseInit);
  response.headers.append("Access-Control-Allow-Origin", "*");
  response.headers.append("Access-Control-Allow-Credentials", "true");
  return response;
}

7. Font Optimization

  • Preload Critical Fonts: Avoid the flash of unstyled text:

<link
  rel="preload"
  href="/fonts/my-font.woff2"
  as="font"
  type="font/woff2"
  crossOrigin="anonymous"
/>
  • Use Next.js Font System: Leverage the built-in font optimization:

import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export default function MyApp({ Component, pageProps }) {
  return (
    <main className={inter.className}>
      <Component {...pageProps} />
    </main>
  )
}

Regular Performance Monitoring

Don't optimize once and forget. Set up continuous monitoring:

  1. Regular Lighthouse Audits: Schedule regular tests to catch regressions

  2. Performance Budgets: Set thresholds for key metrics

  3. User-Centric Metrics: Monitor real user metrics (RUM) for actual user experiences

Conclusion

Improving your Next.js application's PageSpeed score isn't just about applying a few tricks—it requires a holistic approach to optimization. By implementing these strategies, you'll enhance not only your PageSpeed score but also provide a better experience for your users while improving your search engine rankings.

Remember that performance optimization is an ongoing process. As your application evolves, continue to monitor and refine these techniques to maintain optimal performance.

Raymond Yeh

Raymond Yeh

Published on 29 April 2025

Get engineers' time back from marketing!

Don't let managing a blog on your site get in the way of your core product.

Wisp empowers your marketing team to create and manage content on your website without consuming more engineering hours.

Get started in few lines of codes.

Choosing a CMS
Related Posts
Mastering Mobile Performance: A Complete Guide to Improving Next.js Lighthouse Scores

Mastering Mobile Performance: A Complete Guide to Improving Next.js Lighthouse Scores

Comprehensive guide to improving Next.js mobile Lighthouse scores. Learn to optimize Core Web Vitals, reduce static chunks, and implement effective third-party script management.

Read Full Story
SEO Meets Performance: Optimizing Next.js Without Losing Rankings

SEO Meets Performance: Optimizing Next.js Without Losing Rankings

Struggling with slow Next.js apps and falling SEO rankings? Learn how to optimize React's hydration process and implement lazy loading without sacrificing your search visibility.

Read Full Story
SEO Checklist for Next.js with App Router

SEO Checklist for Next.js with App Router

Boost your Next.js app's SEO with our guide! Learn to overcome technical challenges, use SSR/SSG, optimize images, and more to enhance search engine visibility.

Read Full Story
Loading...