Creating a sitemap in Next js is no complicated task. In Next js, you can generate a dynamic sitemap without installing additional libraries or npm packages. All you need is a js file in the Next js pages folder. Let's see how we can achieve this.
According to Google, there are three types of site maps.
Our focus will be on creating an
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/some-page-needs-attention</loc>
<lastmod>2018-06-04</lastmod>
</url>
</urlset>
Since we create a dynamic sitemap, we have to get the required data by calling an API.
The required data is the URL and last modified date of each page we intend to make searchable by search engines.
Therefore, our API would ideally return a list of URLs and corresponding modified dates for each URL.
Something similar to the below
[{"url":"https://www.example.com","lastmod":"2020-12-21"},
{"url":"https://www.example.com/web/about","lastmod":"2020-12-01"},
{"url":"https://www.example.com/web/desclaimer","lastmod":"2020-12-01"},
{"url":"https://www.example.com/web/privacy","lastmod":"2020-12-01"}]
We will build our sitemap based on the above response
Now, let's create the sitemap file. The search engines look for the site map in the root, i.e.,
const Sitemap = () => {};
const toUrl = (route) =>
`<url><loc>${route.url}</loc><lastmod>${route.lastmod}</lastmod></url>`;
const createSitemap = (urlList) =>
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
${urlList.map((url) => toUrl(url)).join("")}
</urlset>`;
export async function getServerSideProps({ res, req }) {
const siteMapJson = await fetch(`https://www.exampleapi.com/getsitemap`);
const urlList = await siteMapJson.json();
const sitemap = createSitemap(urlList);
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return { props: { results : {urlList}}}
};
export default Sitemap;
That's all to it. If your API returns a valid JSON, you can access your sitemap at
If you are in a hurry and want to create a dynamic sitemap for your website, you can skip the below. If you are curious about how the above code works, read on.
The method
fetch(`https://www.mysiteapi.com/getsitemap`);
const urlList = await siteMapJson.json();
const sitemap = createSitemap(urlList);
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return { props: { results : {urlList}}}
};
In lines 1 and 2, we get the URL list to generate the sitemap by calling the API.
Therefore, the variable
const toUrl = (route) =>
`<url><loc>${route.url}</loc><lastmod>${route.lastmod}</lastmod></url>`;
const createSitemap = (urlList) =>
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
${urlList.map((url) => toUrl(url)).join("")}
</urlset>`;
The above code generates the sitemap and stores it in the variable sitemap as a string value.
Now we have to send this to the client. The
The purpose of a sitemap is to help search engines crawl your website's pages. A sitemap is useful when pages end up without any internal links pointing to them, making them hard to find. A sitemap can also help search engines to understand your website structure. Below are some general guidelines to consider when you create a sitemap.