Software Studio Pro
If you want to become a creator, contact us for your account creation.
BLOG ON
UPDATED ON - 19th May, 2024
PUBLISHED BY
Frontend Developer
In Next.js, generating a dynamic sitemap involves creating an endpoint that generates the sitemap dynamically based on your website's content. You can achieve this by using the getServerSideProps or getInitialProps functions in your Next.js pages to fetch data and generate the sitemap.
Here's what you need to create a dynamic sitemap in Next.js. Inside pages folder create a file named exactly sitemap.xml.js, so the path where you will create this file would be:
function generateSiteMap(data) {
const xmlUrls = data.map((item) => {
return `
<url>
<loc>${item.path}</loc>
<lastmod>${item.date}</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
`;
})
.join("");
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.softwarestudiopro.com</loc>
<lastmod>2024-01-20</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://www.softwarestudiopro.com/contact-us</loc>
<lastmod>2024-01-20</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
${xmlUrls}
</urlset>
`;
}
function SiteMap() {
// getServerSideProps will do the heavy lifting, no need to do anything here
}
export async function getServerSideProps({ res }) {
// We make an API call / create a dataset to gather the URLs for our site
const request = await fetch(EXTERNAL_DATA_URL);
const data = await request.json();
// We generate the XML sitemap with the data assuming we get an array as a response in data
const sitemap = generateSiteMap(data);
res.setHeader("Content-Type", "text/xml");
// we send the XML to the browser
res.write(sitemap);
res.end();
return {
props: {},
};
}
export default SiteMap;
And voila! You're all set now. You're sitemap will be generated. You can check it out by navigating on the path /sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.softwarestudiopro.com</loc>
<lastmod>2024-01-20</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://www.softwarestudiopro.com/contact-us</loc>
<lastmod>2024-01-20</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
</urlset>
In this blog, we implemented sitemap for our Next.js app. Remember to update the data array and generateSitemap function according to your application's specific requirements and data sources. Additionally, you might want to consider caching strategies for performance optimization, especially if your content doesn't change frequently.
Micro-frontend Architecture: The Future of Scalable Web Applications
Published on - 20th August, 2024
Key Benefits of Outsourcing Software Development for Startups
Published on - 7th August, 2024
AI for Documentation: Benefits of Using AI for Documentation with Relevant AI Tools
Updated on - 25th July, 2024
SEO for 2024: Complete Guide to Increase Website Ranking
Updated on - 16th July, 2024
Beginners Guide to AI: Mastering Prompt Engineering Techniques for LLMs like ChatGPT + Examples
Updated on - 25th July, 2024