⚡ Unlock faster workflows with Prismic’s newest features!See what's new and upcoming
Tech stack
·6 min read

Top 10 Tips for SEO Optimization on Next.js Websites & Blogs

Performance and SEO are crucial for any website, whether it’s a blog, news publication, ecommerce app, landing page, or other types of websites and applications. Everyone wants to secure and maintain a spot in the top 3 positions on search engine result pages (SERP). However, many websites fall short due to poor optimization of performance and SEO. This usually occurs because website builders don’t know the right strategies to implement.

If you’re building with Next.js and find yourself in a similar position, then you’re in the right place. In this article, we will explore 10 strategies to optimize the performance and SEO of your Next.js blog and websites.

Ready? Let’s dive in!

Top Next.js SEO Optimization Tips

Performance optimization tips

1. Optimize fonts

Fonts can have a significant impact on load times, so it's important to use the right formats for different browsers and limit the number of font variations.

The negative impact of web fonts on Core Web Vitals

Next.js provides a font module called next/font that automatically handles font optimization and avoids issues like zero layout shift. Here’s an example code of the font module in action.

import { Inter } from 'next/font/google'
 
const inter = Inter({
  subsets: ['latin'],
  display: 'swap',
})
 
export default function RootLayout({ children }) {
  return (
    <html lang="en" className={inter.className}>
      <body>{children}</body>
    </html>
  )
}

2. Optimize images

You can handle image optimization with the next/image component, which automatically optimizes images for different screen sizes and formats. This ensures that the right image size is delivered to the user's device, reducing load times.

The image component also lazyloads images It uses the browser’s native lazy loading ability to only load images when they enter the viewport. It can also add a blur effect to images while they load, though this is an optional feature.

import Image from 'next/image'
import profilePic from './me.png'
 
export default function Page() {
  return (
    <Image
      src={profilePic}
      alt="Picture of the author"
    />
  )
}

Third-party scripts can also slow down your site. It's important to load them asynchronously or defer their loading until necessary. This prevents them from blocking the main thread and ensures that critical content is loaded first.

3. Optimize third-party scripts

Once again, Next.js comes to the rescue when it comes to optimizing third-party scripts for performance. It provides a custom Script component via its next/script module for loading and optimization of third-party scripts. The component provides 3 loading strategies for scripts:

  • beforeInteractive: Loads the script before the page becomes interactive. It is useful for essential scripts that need to be available as soon as possible. Use it sparingly, as it can delay the interactive state of the page.
  • afterInteractive: Loads the script after the page has become interactive. It is the default strategy and is suitable for most scripts that do not need to block the initial rendering of the page.
  • lazyOnload: Loads the script when the browser is idle, typically after all critical resources have been loaded. It is suitable for analytics scripts and other non-essential scripts that do not impact the user experience and that are not time-sensitive.
import Script from 'next/script'
 
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
      <Script src="https://example.com/script.js" />
    </html>
  )
}

4. Minimize and optimize CSS and JavaScript

Minimizing and optimizing CSS and JavaScript reduces the file size, which helps your website load faster. This is especially important for mobile users or those with slower internet connections.

If you’re building with Next.js, you’re in luck because Next.js minifies and optimizes your files. This means you don’t have to do it yourself. The framework handles this with the Next.js compiler. When you run next build, Next.js compiles and minifies the code for production to help achieve the best performance and support all modern browsers.

Deliver a fast website with a visual page builder!

Prismic is a headless solution with a visual page builder for your marketing team to release pages independently.

Metadata optimization tips

5. Optimize meta tags

Optimizing meta tags is essential for improving your site's SEO and can drive more organic traffic your way. Meta tags provide search engines with important information about your pages, such as the title and meta description

You can add well-optimized meta tags, to your blog with the next/head component. Here’s a sample snippet of this in action.

import Head from 'next/head'

export default function BlogPost() {
  return (
    <>
      <Head>
        <title>My Blog Post</title>
        <meta name="description" content="This is my blog post about optimizing Next.js" />
        <meta name="keywords" content="Next.js, SEO, optimization" />
      </Head>
      <div>
        <h1>My Blog Post</h1>
        <p>This is my blog post content.</p>
      </div>
    </>
  )
}

Technical SEO tips

6. Add schema markup

Adding structured data like Schema.org markup to your Next.js blog helps search engines understand your content better and can improve your site's visibility in search engine results pages (SERPs). It also makes you eligible for rich results, if done correctly!

You can use JSON-LD (JavaScript Object Notation for Linked Data), a lightweight format for structured data, to add schema markup. Here’s a sample code for a blog page with multiple blog cards.

// blog-page.js
import Head from 'next/head';
import Script from 'next/script';

export default function BlogPage() {
  const blogPosts = [
    {
      "@type": "BlogPosting",
      "headline": "How to Optimize Your Next.js Site for SEO",
      "image": "https://yourdomain.com/images/seo-optimization.jpg",
      "author": {
        "@type": "Person",
        "name": "John Doe"
      },
      "datePublished": "2024-07-01",
      "dateModified": "2024-07-01",
      "description": "A comprehensive guide on optimizing your Next.js site for better SEO."
    },
    // Add more blog posts as needed
  ];

  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "Blog",
    "url": "https://yourdomain.com/blog",
    "name": "Your Blog Name",
    "description": "A blog about web development and optimization.",
    "blogPost": blogPosts
  };

  return (
    <div>
      <Head>
        <title>My awesome blog</title>
        <meta name="description" content="A blog about web development and optimization." />
      </Head>
      <Script
        id="blog-schema"
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
      />
      <div>
        <h1>Welcome to my Next.js + Prismic-powered blog</h1>
        <div className="blog-cards">
          {blogPosts.map((post, index) => (
             {/* Render your blog cards here */}
          ))}
        </div>
      </div>
    </div>
  );
}

7. Add a sitemap.xml file

A sitemap helps search engines understand the structure of your website and Next.js provides built-in support for generating sitemaps using the App Router. This approach involves creating a sitemap.js or sitemap.ts file in your project, which programmatically generates a sitemap.

Once you’ve created a sitemap file, submit it to Google Search Console to monitor searchbots’ crawling of it there.

Here’s an example sitemap:

// app/sitemap.js
export default function Sitemap(){
  return [
    {
      url: 'https://yourdomain.com',
      lastModified: new Date().toISOString(),
      changeFrequency: 'yearly',
      priority: 1.0,
    },
    {
      url: 'https://yourdomain.com/about',
      lastModified: new Date().toISOString(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: 'https://yourdomain.com/blog',
      lastModified: new Date().toISOString(),
      changeFrequency: 'weekly',
      priority: 0.5,
    },
  ];
}

In this example:

  • url: The URL of the page
  • lastModified: The date when the page was last modified
  • changeFrequency: How frequently the page is likely to change (e.g., daily, weekly, monthly, yearly)
  • priority: The priority of the page relative to other pages on the site (values range from 0.0 to 1.0)

When you build your Next.js project, the sitemap will be generated based on the URLs provided in the sitemap.ts file. Here’s the generated sitemap.xml for the above code:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://yourdomain.com</loc>
    <lastmod>2024-07-01T12:00:00.000Z</lastmod>
    <changefreq>yearly</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://yourdomain.com/about</loc>
    <lastmod>2024-07-01T12:00:00.000Z</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://yourdomain.com/blog</loc>
    <lastmod>2024-07-01T12:00:00.000Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>
</urlset>

After generating your sitemap, it's important to submit it to Google Search Console to ensure that Google can efficiently crawl and index your site.

8. Add a robots.txt file

The robots.txt file tells search engine crawlers which URLs they can access on your site and which they should avoid. This file helps manage the crawling of your site to ensure that sensitive or non-essential pages aren't indexed by search engines. The snippet below shows a programmatically generated robots.txt file.

// app/robots.js
export default function robots() {
  return {
    rules: {
      userAgent: '*',
      allow: '/',
      disallow: '/private/',
    },
    sitemap: 'https://yourdomain.com/sitemap.xml',
  }
}

When you build your Next.js project, this will generate a robots.txt file with the following content:

User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://yourdomain.com/sitemap.xml

Bonus SEO techniques

9. Create high-quality and SEO-optimized content

Technically speaking, this is not a programmatic strategy, neither does it have anything to do with Next.js. However, this is a low hanging fruit that you should prioritize for the best SEO performance. Why? Because while you need to optimize your website for performance, you also need to optimize the content for your readers.

It won’t matter how fast your website is if its filled with poor content that doesn’t solve your target audience’s problems or match their search intent. Your site may appear at the top of SERP results for a while. However, if users continually bounce off seconds after coming in, it’ll send a signal to search engines that your content isn’t matching searcher’s expectations, and the search engines will eventually drop your site’s SEO rankings.

Luckily, this doesn’t have to happen. Here are some effective strategies you can use to create high-quality and SEO-optimized content:

  1. Conduct thorough keyword research to identify and target the most relevant search terms for your audience
  2. Craft engaging and informative content that directly addresses the pain points and interests of your target readers
  3. Use a clear and structured format with headers, bullet points, and concise paragraphs to enhance readability and user experience
  4. Incorporate multimedia elements like images, videos, and infographics to make your content more appealing and shareable
  5. Regularly update and refresh your content to ensure it remains accurate, relevant, and aligned with current SEO trends and algorithms

10. Monitor performance

Use tools like Google PageSpeed Insights or Lighthouse to regularly monitor your blog’s performance, as well as Google Search Console. Pay attention to metrics like loading times, Core Web Vitals, and overall performance scores, and make improvements as needed.

Conclusion

By implementing performance and SEO strategies outlined in this article, you can enhance your Next.js blog or website's load times, improve user experience, and increase your visibility in search results.

Further your Next.js learning

Put some of these techniques to practice by building a full Next.js website! See the power of Prismic as a Headless website builder alongside Next.js. In this tutorial, you'll create a trendy, dark, and modern website and use GSAP to add wonderful animations, both on page load and on scroll. You'll also use Tailwind CSS to style the site and add things like meta data to make sure it's super polished.

Article written by

Nefe Emadamerho-Atori

Nefe is an experienced front-end developer and technical writer who enjoys learning new things and sharing his knowledge with others.

More posts
Nefe Emadamerho-Atori

Join the discussion

Hit your website goals

Websites success stories from the Prismic Community

How Arcadia is Telling a Consistent Brand Story

Read Case Study

How Evri Cut their Time to Ship

Read Case Study

How Pallyy Grew Daily Visitors from 500 to 10,000

Read Case Study