NextJS sitemap route
The current setup has been tested with NextJS 9.2.2
Create a file called
Api Routes are special pages that allow you to export a request handler function that will be run as a serverless function. Using Api Routes avoid you the pain of overriding the default server script, although if have already done it what follows is pretty much the same.
Install the following package to build the sitemap xml with confidence.
npm install xmlbuilder
If you have dynamic routes you should get a server side fetching library, I'm going to use
isomorphic-unfetch but feel free to choose your favorite one.
Please note that the syntax may be slightly different on other libraries.
npm install isomorphic-unfetch
Create environment variables using a dotenv file or some other techinque.
For demonstration i will suppose that your cms exposes a graphql endpoint.
When the code is up and running you can see the result by visiting https://example.com/api/sitemap.