Challenges

Sitemaps & Robots.txt

Rewrite the API route

Usually, we keep the sitemap at /sitemap.xml. In our case, it's located at /api/sitemap. Our sitemap is still valid and could work with search engines. But it's nice to keep it in the standard location.

Luckily, we have Next.js rewrites. With that, we can fix this issue.

šŸ’” With rewrites, we can map an incoming HTTP request with the content from some other place. In Next.js, incoming request is called as the source & the other place as the destination.

We can use a page, API route, static file, or even an external URL as the destination.

To implement this, create a file called next.config.js in your app root, and add the following content:

module.exports = {
    async rewrites() {
        return [
            {
                source: '/sitemap.xml',
                destination: '/api/sitemap',
            },
        ]
    },
}

As you can see, now we map /sitemap.xml to /api/sitemap.

Now restart your app & visit http://localhost:3009/sitemap.xml to access the sitemap.


Q: Select another reason to use `next.config.js`
authenticating...

šŸ™ We need your help

We need your help to keep maintain & add new content to this course. Here's how you can support us: