Top 5 SEO Optimization Tips in Next.js 14






Learn how to add SEO optimization support using the Astro framework.
Get the latest articles delivered to your inbox. No spam, unsubscribe anytime.
Since the original release of Next.js there have been some major version changes and improvements to the framework in the area of SEO optimization.
After the release of Next.js 13 there has been a lot of focus to support React Server Components (RSC). In this article we'll take a look at the top 5 SEO optimization tips for Next.js, including:
Read on to learn about these essential common tips and best practices for SEO optimization in Next.js.
Next.js has updated its SEO optimization API which has been revised to be more in alignment with React Server Components (RSC).
As writing Server Side Rendered (SSR) code becomes much easier with RSC in Next.js, the SEO API is now also RSC.
The new framework API changes make it very simple to provide extensive coverage for page SEO optimization, including:
Static Site Generation (SSG)Content Layout Shifting (CLS)The good news is that the new API changes provides these capabilities. So you can focus on development rather than building your own API or third party library to do the job.
Page titles and descriptions are probably one of the primary ways that a website can convey the intent of the page content to the user as well as search engines.
The primary reasons to use unique page titles are:
<h1> tagNext.js has changed the way it sets page titles. It used to dynamically hoist them on the client side but now they must be added on RSC components.
Consider the following project structure with a root layout, nested product layout and a product page.tsx.
1your-project2 └── app3 ├── product4 │ ├── page.tsx5 │ └── layout.tsx6 └── layout.tsx (root layout)
<Head> componentThe first change that should be made is to switch to the new <Head> component from next/head in the root layout, this way Next.js can manage the head tag output.
You can include a <title> tag as default base title in the root layout.
1import Head from 'next/head';23export const RootLayout = ({ children }) => {4 return (5 <html lang="en">6 <Head>7 <title>ACME Corp</title>8 {/* define any global meta tags here */}9 </Head>10 <body>11 {children}12 </body>13 </html>14 );15};1617export default RootLayout;
There are two options for adding page titles.
Metadata title onlyMetadata template with a placeholderYou can add titles by simply exporting a named metadata object from any RSC component modules, including:
Next.js will compute the title based on every nested child metadata export it finds on route segments.
To simply add just a page title, you can just set the title field to a string value.
1export const metadata: Metadata = {2 title: 'ACME Corp'3}
To create a template, simply set the title field to an object with the following fields.
template: a string with a placehoder %s for the page.default: a default title if nested pages don't provide a unique title.Using a template allows you to still keep your website name in the title including the more specific page title.
1export const metadata: Metadata = {2 title: {3 template: '%s | ACME Corp',4 default: 'ACME Corp'5 }6}
Let's add a template export for the product layout so that all the product pages can use the same template.
1import { Metadata } from 'next';23export const metadata: Metadata = {4 title: {5 template: '%s | ACME Corp',6 default: 'ACME Corp'7 }8}
Each nested product page that inherits the layout template metadata from its parent route segment, can specify a unique page title and description.
1import { Metadata } from 'next';23export const metadata: Metadata = {4 title: "Product Page",5 description: "Some product page description"6}
If you access the /product route, the title and description in the browser would read.
1Product Page | ACME Corp23Some product page description
You can have as many nested metadata exports as needed. Next.js will use the closest nested parent template in the route segments path to build the title.
A couple tips on RSC components:
layout.tsx and treat them like hubs for many pages the same relevent content.metadata on page.tsx RSC components.If your page.tsx uses the use client indicator, you may need to rethink how you'll export the metadata title and description since it can only be added on RSC components.
It's usually better to have page.tsx as an RSC and break the client code into smaller nested client components if possible.
<meta> tags, also known as Metadata in the Next.js framework, are tags that are added to your page to further inform search engines about your content, including:
keywords: A space delimited string of keywords.category: The category meta name property.icons: The icons for the document. Defaults to rel="icon".robots: The robots setting for the document.viewport: The viewport setting for the document.authors: a list of authors for a paritculuar page.These are just a few of the basic meta tags from the Metadata API but the API has very thorough coverage for just about all of the tags you'll need for SEO optimization.
Metadata tags provide information which is used by search engines to indicate the type of content on a web page.
Utimately, the actual content will determine how it ranks on a search engine's results page but the meta tags should also be added to support the type of content on your page.
All of the <head> tags can be set by exporting the generateMetadata function and returning the Metadata object. This is the same Metadata object used to define the title and template as we saw earlier.
The generateMetadata function is used when you need to gather meta information that might be fetched async.
For example, let's say we add a details page that has a product id=1234 param and fetches the meta information.
1{2 "id": "1234",3 "title": "Product 1234",4 "description": "Product 1234 description",5 "author": {6 "url": "http://acme.example.com/url/to/bio/lc-bio",7 "name": "Lloyd Christmas"8 },9 "keywords": ["nextjs, SEO"],10 "category": "SEO Optimization",11 "icons": [12 "http://acme.example.com/icon.png"13 ]14 }
1import { Metadata } from 'next';23export async function generateMetadata({4 params: { id },5}: {6 params: { id: string };7}): Promise<Metadata> {8 const { title, description, author, keywords, category, icons } = (await fetch(9 `http://acme.example.com/api/product/detail/${id}`10 )) as any;1112 return {13 title,14 description,15 authors: [author],16 keywords,17 category,18 icons: icons.map((url) => { rel: "icon", url }),19 /*20 robots might not come from product info but could21 be set for all product pages since the page uses an [id] slug22 */23 robots: { index: true, follow: true },24 /*25 viewport could just be set at the root layout but26 to demonstrate how it can be set for any page.27 */28 viewport: {29 width: 'device-width',30 initialScale: '1.0'31 }32 };33}3435function Page({ params: { id } }: { params: { id: string } }) {36 return (37 <section>38 <h1>Product Details</h1>39 </section>40 );41}4243export default Page;
Accessing the new app route http://acme.example.com/product/detail/1234 will generate the following HTML tags in the head.
1<head>2 <title>Product 1234 | ACME Corp</title>3 <meta name="viewport" content="width=device-width, initial-scale=1.0">4 <meta name="description" content="Product 1234 description" />5 <meta name="keywords" content="nextjs, SEO" />6 <meta name="category" content="SEO Optimization" />7 <meta name="author" content="Lloyd Christmas" />8 <link rel="author" href="http://acme.example.com/url/to/bio/lc-bio"></link>9 <link rel="icon" href="http://acme.example.com/icon.png" />10 <meta name="robots" content="index, follow" />11</head>
Using the Metadata object makes adding a single or an array of tags using object notation very easy.
The Open Graph protocol was created by facebook to standardize the use of meta tags to improve the way links are shared on presented on social media.
X (Twitter) has its own set of meta tags to control the way card posts are displayed but will default to Open Graph meta tags if X meta tags are not present.
By defining some simple <meta> tags, any website can greatly improve and optimize how their URL links are displayed.
Your web page content or landing page Ads might not display as expected on these platforms without these tags.
The Open Graph meta tags allow for a rich user experience and will draw more attention and trust with users on social media.
This is really essential for promotional advertisements or even blog articles but in general it's good practice and easy to do on Next.js using the Metadata object.
For example, here's what a post with Open Graph meta tags would look like.

And here's an example without Open Graph.

Without these tags, social media platforms will just use the standard meta tags and probably won't display as you would expect.
The great thing about the new API is that everything is built into the Metadata object, including Open Graph support.
So now we can build on the previous meta tags and add some Open Graph tags to the example.
Here's a revised version of generateMetadata function we created earlier.
1import { Metadata } from 'next';23export async function generateMetadata({4 params: { id },5}: {6 params: { id: string };7}): Promise<Metadata> {89 const siteUrl = "https://acme.com";10 const siteName = "ACME";1112 const { title, description, author, keywords, category, icons } = (await fetch(13 `http://acme.example.com/api/product/detail/${id}`14 )) as any;15 return {16 title,17 description,18 authors: [author],19 keywords,20 category,21 icons: icons.map((url) => { rel: "icon", url }),22 robots: { index: true, follow: true },23 viewport: {24 width: 'device-width',25 initialScale: '1.0'26 }2728 // adding Open Graph and X/Twitter specific tags here29 openGraph: {30 type: 'website',31 title,32 description,33 url: siteUrl,34 siteName: siteName,35 },36 twitter: {37 title,38 description,39 creator: author?.name,40 card: 'summary_large_image',41 site: siteName,42 },43 };44}4546function Page({ params: { id } }: { params: { id: string } }) {47 return (48 <section>49 <h1>Product Details</h1>50 </section>51 );52}5354export default Page;
By addding these additional fields to the Metadata object response, it will generate the following tags in the <head>, assuming we are using the same JSON sample product data.
1<head>2 <title>Product 1234 | ACME Corp</title>3 <meta name="description" content="Product 1234 description" />4 <meta name="keywords" content="nextjs, SEO" />5 <meta name="category" content="SEO Optimization" />6 <meta name="author" content="Lloyd Christmas" />7 <link rel="author" href="example.com/url/to/bio/lc-bio"></link>8 <meta name="viewport" content="width=device-width, initial-scale=1.0">9 <link rel="icon" href="http://acme.example.com/icon.png" />10 <meta name="robots" content="index, follow" />1112 <!-- Open Graph and X (Twitter) here -->13 <meta property="og:title" content="Product 1234 | ACME Corp">14 <meta property="og:description" content="Product 1234 description">15 <meta property="og:url" content="http://acme.example.com">16 <meta property="og:site_name" content="ACME Corp">17 <meta property="og:type" content="website">18 <meta name="twitter:card" content="summary_large_image">19 <meta name="twitter:site" content="ACME Corp">20 <meta name="twitter:creator" content="Lloyd Christmas">21 <meta name="twitter:title" content="Product 1234 | ACME Corp">22 <meta name="twitter:description" content="Product 1234 description">23</head>
The Open Graph and Twitter types in Next.js are quite extensive, so I've narrowed it down to the bare minimum you'll need.
To get more information on these types, you can inspect theMetadatamodule and see themetadata-interface.d.tsfile that opens in your IDE.
The Metadata object can even generate arbitrary <meta> tags by using the other object key.
For example:
1export function generateMetadata(): Metadata {2 return {3 other: {4 'some:arbitrary:key': 'SEO Optimization'5 }6 }7}
1<head>2 <meta name="some:arbitrary:key" content="SEO Optimization">3</head>
A sitemap.xml file is what search engines will use as an index to discover all of the available public pages that you want the search engine bots to crawl and parse.
Here's a snippet from the Next.js docs of what a sitemap.xml file looks like.
1<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">2 <url>3 <loc>https://acme.com</loc>4 <lastmod>2023-04-06T15:02:24.021Z</lastmod>5 <changefreq>yearly</changefreq>6 <priority>1</priority>7 </url>8 <url>9 <loc>https://acme.com/about</loc>10 <lastmod>2023-04-06T15:02:24.021Z</lastmod>11 <changefreq>monthly</changefreq>12 <priority>0.8</priority>13 </url>14 <url>15 <loc>https://acme.com/blog</loc>16 <lastmod>2023-04-06T15:02:24.021Z</lastmod>17 <changefreq>weekly</changefreq>18 <priority>0.5</priority>19 </url>20</urlset>
Here's what these fields represent:
loc: Url to access the page.lastmod: Used to indicate when the file was last modified.changefreq: This field represents the change frequency of modifications.priority: This is the imporance of the url compared to the other urls on the site and it ranges from 0.0 - 1.0.Most of the research says lastmod, changefreq and priority are most likely ignored by search engines since they might not be properly maintained by Webmasters.
As long as url and lastmod are added to this list is probably good enough.
As search engine bots crawl your website they can only crawl to other pages if there is a relative link from one page to the other page. However, this is not always the case and many pages will only be accessible by direct link only.
So a sitemap.xml file serves as a complete index of all the available URL's you want the bot to crawl and parse.
It can contain up to 50,000 URL entries but if you need more you can generate multiple sitemap.xml pages or have nested sitemap.xml files in subdirectories.
Next.js provides the sitemap.ts file and will call the default exported function at build time and automatically generate a sitemap.xml.
You can have multiple sitemaps.ts at any route segment but in most cases, placing one at the app root is usually sufficient enough for most websites.
Here's a simple example of how a blog site might generate a sitmap.xml file.
1import { MetadataRoute } from 'next';2import { getPosts } from '@/lib/blog/posts';3import moment from 'moment';45export default async function sitemap(): MetadataRoute.Sitemap {6 const siteUrl = "https://acme.com";78 const posts = await getPosts().map((post) => {9 const {slug, dateModified} = post;10 return {11 url: `${siteUrl}/blog/${slug}`,12 dateModified: moment(`${dateModified}`).toDate()13 };14 });15 return [16 {17 url: `${siteUrl}/dashboard`,18 lastmod: moment().toDate()19 },20 {21 url: `${siteUrl}/resources`22 lastmod: moment().toDate()23 },24 {25 url: `${siteUrl}/blog`26 lastmod: moment().toDate()27 },28 ...posts29 ];30}
And the output for the sitemap.xml would be as follows.
1<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">2 <url>3 <loc>https://acme.com/dashboard</loc>4 <lastmod>2024-03-15T07:52:57.880Z</lastmod>5 </url>6 <url>7 <loc>https://acme.com/resources</loc>8 <lastmod>2024-03-15T07:52:57.880Z</lastmod>9 </url>10 <url>11 <loc>https://acme.com/blog</loc>12 <lastmod>2024-03-15T07:52:57.880Z</lastmod>13 </url>14 <url>15 <loc>https://acme.com/blog/blog_1</loc>16 <lastmod>2024-03-15T07:52:57.880Z</lastmod>17 </url>18 <url>19 <loc>https://acme.com/blog/blog_2</loc>20 <lastmod>2024-03-28T19:24:55.853Z</lastmod>21 </url>22</urlset>
In the latest version of Next.js they have provided a finalized version of their <Image> API tag, making it very easy to optimize image resources.
Images are no doubt the primary focal point of websites but can also come with a performance impact.
If not used in an optimized way they can also have an unpleasent end user experience with content shifting around during loading.
Slow page load times can also have an impact on SEO page ranking. The longer it takes your page to load, the lower it might be ranked on a search engine results page (SERP).
The <Image> component from the next/image API provides some benefits, including:
image/avif and image/webp.To use the <Image> component, simply import from the next/image and also import the local image from your public directory.
For this example I'll just import a local hero background image.
image/jpg.I'm passing the following props to the <Image> component:
src: A local import image.alt: Requires an alt attribute for Accessibility.width: Requires a width of the image in pixels, unless the image is local static import.height: Requires a height of the image in pixels, unless the image is a local static import.The width and height should be used to set the optimal dimensions of the image you want. Next.js will generate an optimized image based on these settings.
You can still use classes and styles for styling to fit the web page layout.
1import Image from 'next/image';2import HeroImage from '@/public/demo/home-decor-bg.jpg';34function Page() {5 return (6 <div>7 <Image8 src={HeroImage}9 width={1200}10 height={800}11 alt="Image of SEO optimize image using Next.js image API"12 className="w-full"13 />14 </div>15 );16}1718export default Page;
The image will now be cached automatically and optimized and served through an API endpoint.
If you open a Chrome inspector network tab, you'll see that the image is now being loaded from a reserved internal Next.js endpoint for images.
localhost:3000/_next/image

After using the <Image> component, we can now see the following details:
image/webp.So the file size has been reduced to half of its original size and the image content type is now image/webp and fully cached.
The file size is optimized and will greatly reduce layout CLS shifting when loading.
You can imagine how larger background or banner images will benefit from this new feature.
Now that we've examined some of the new features in Next.js for SEO optimization and performance, I hope you can apply them to your project for some huge benefits.
Just remember that performance is an essential part of SEO optimization among other factors.
As React web application make their migration to being mostly server side rendered (SSR) or even statically render (SSG), we'll definitely see more great features from the Next.js team to accelerate web application development.