Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Add Sitemap in Next.js 14 [Static & Dynamic]


๐Ÿ“š Add Sitemap in Next.js 14 [Static & Dynamic]


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

A sitemap is like a map for your website. It helps search engines find all the pages on your site. If you have lots of pages, a sitemap makes it easier for search engines to find and show them in search results.

In this post, I'll explain how to make a Sitemap from scratch using Next.js.

How to Add Sitemap in Next js
Setting up the Next.js. Project

Before we dive into the code, let's ensure we have everything set up:

  • Install the latest version of Next.js or the minimum version of Next.js 13.

Once the project is set up now let's understand the Code.

Adding Sitemap in Nextjs

1. Create a Sitemap.js File in the App Folder

The first thing is you need to create a new sitemap.js file in your Next.js App folder.

If you using TypeScript then you can add a sitemap.ts to avoid getting bugs during development.

In this file, we need to return a function and the name of the function shows as a sitemap, here is a proper example mentioned below:

export default async function sitemap() {

return //More code here

}

2. Use route details in an Array for Static site

For those of you who have a static website and not using a dynamic route then make sure you added all the route details in an array of objects.

Here is a good example of how can you show the details the route details.

  const tools: PostType[] = [
    { url: "dummy-post-1", _updatedAt: "2024-03-21T14:59:28Z" },
    { url: "dummy-post-2", _updatedAt: "2024-03-23T14:59:28Z" },
    { url: "dummy-post-3", _updatedAt: "2024-03-24T14:59:28Z" },
    { url: "dummy-post-4", _updatedAt: "2024-03-25T14:59:28Z" },
    { url: "dummy-post-5", _updatedAt: "2024-03-27T14:59:28Z" },
    { url: "dummy-post-6", _updatedAt: "2024-03-28T14:59:28Z" },
  ];

Remember to also add the publish date of the page according to the route as shown in the above example.

3. Map through the Stored Array details and return it

Now we need to map through this stored array data and return it.

If you're fetching data from an API, you don't necessarily need to store all the details in an array of objects.

Instead, you can directly map through the details after fetching the data in your app.

 export default async function sitemap() {

  const routes = [
    { url: "dummy-post-1", _updatedAt: "2024-03-21T14:59:28Z" },
    { url: "dummy-post-2", _updatedAt: "2024-03-23T14:59:28Z" },
    { url: "dummy-post-3", _updatedAt: "2024-03-24T14:59:28Z" },
    { url: "dummy-post-4", _updatedAt: "2024-03-25T14:59:28Z" },
    { url: "dummy-post-5", _updatedAt: "2024-03-27T14:59:28Z" },
    { url: "dummy-post-6", _updatedAt: "2024-03-28T14:59:28Z" },
  ];


return routes.map((route) => ({
    url: `https://localhost:3000/${route.url}`,
    lastModified: route._updatedAt,
  }));

  }

In the example above, I made an array called "tools" storing URL data and update times.

Then, I changed the names a bit. The URL data is now stored in a property called "URL" and "_updatedAt" is now called "lastModified."

So, now the function returns data where each object has a "URL" property containing the website URL and a "lastModified" property showing the last time the data was updated.

Add your domain name instead of localhost:3000 in production.

Now if you save the file and visit http://localhost:3000/sitemap.xml you will see that Next.js has successfully generated a sitemap for us.

Sitemap in Next.js

I hope I can explain to you how to add a sitemap to Next.js projects.

If you're using versions 13, 14, or the latest one, this method will work for you.

Check out my other articles from my blog, SingleSyntax.com, to delve into more about Next.js and other frontend tips. Make sure to visit the blog for more insightful content!

...



๐Ÿ“Œ Add Sitemap in Next.js 14 [Static & Dynamic]


๐Ÿ“ˆ 57.15 Punkte

๐Ÿ“Œ Add a Dynamic Sitemap to Next.js Website Using Pages or App Directory


๐Ÿ“ˆ 42.73 Punkte

๐Ÿ“Œ Content*Builder sitemap/sitemap.inc.php path[cb] privilege escalation


๐Ÿ“ˆ 39.7 Punkte

๐Ÿ“Œ Mambo sitemap 2.0.0 sitemap.xml.php mosConfig_absolute_path privilege escalation


๐Ÿ“ˆ 39.7 Punkte

๐Ÿ“Œ Unlock the Power of Static Sites with Next.js: A Guide to Static Site Generation


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ Getting Started with Next.js: Part 5 - Static Site Generation and Incremental Static Regeneration


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ Dynamic sitemap with django


๐Ÿ“ˆ 30.03 Punkte

๐Ÿ“Œ Harnessing Next.js and Firebase: Dynamic Static Path Generation for SSG


๐Ÿ“ˆ 28.28 Punkte

๐Ÿ“Œ NextJS Add XML Sitemap


๐Ÿ“ˆ 26.77 Punkte

๐Ÿ“Œ Dynamic & Static Application Security Testing: Die besten DAST- & SAST-Tools


๐Ÿ“ˆ 26.7 Punkte

๐Ÿ“Œ How to create a sitemap with Next.js app directory


๐Ÿ“ˆ 25.63 Punkte

๐Ÿ“Œ Creating a Sitemap File for a Next.js Project


๐Ÿ“ˆ 25.63 Punkte

๐Ÿ“Œ t3n Daily: Adobe & Figma, Ethereum & NFT, Steuer & Homeoffice, KI & Gruselfrau


๐Ÿ“ˆ 25.19 Punkte

๐Ÿ“Œ What Is Static IP Address? How to Configure Static IP Address in Ubuntu


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ How to Fix Static Sound in Windows 10 โ€“ Guide on Static Noise


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ How to Fix Static Sound in Windows 10 โ€“ Guide on Static Noise


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Static Variables in Java โ€“ Why and How to Use Static Methods


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Static Malware Analysis Vs. Dynamic Malware Analysis


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Android app deobfuscation using static-dynamic cooperation


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ George Hotz: 3 Problems of Autonomous Driving: Static, Dynamic, Counterfactual | AI Podcast Clips


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ A static injector of dynamic library for application


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ DAST vs SAST โ€“ Dynamic Application Security Testing vs Static


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ A discussion on static vs dynamic linking


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Iblessing - An iOS Security Exploiting Toolkit, It Mainly Includes Application Information Collection, Static Analysis And Dynamic Analysis


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Dynamic and Static Analysis of Adware VBA Macro Word Document


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ static IP vs dynamic IP


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ VB2018 paper and video: Android app deobfuscation using static-dynamic cooperation


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ DAST vs. SAST: Fact check on static and dynamic application security testing


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Static Vs Dynamic IPs: Differences, Similarities and Cybersecurity Considerations


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Static Vs Dynamic IPs: Differences, Similarities and Cybersecurity Considerations


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Static Vs Dynamic IPs: Differences, Similarities and Cybersecurity Considerations


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Migration of a Dynamic Website to a Static Website


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Whatโ€™s the difference between dynamic and static analysis


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ UI Testing with AI: From Static Checks to Dynamic Validation


๐Ÿ“ˆ 22.5 Punkte

๐Ÿ“Œ Static vs. dynamic IP addresses: What's the difference and why you need to know


๐Ÿ“ˆ 22.5 Punkte











matomo