Next.js sitemap

Found out that my posts were not indexed by Google.

Search with site:https://minho42.com didn't show all posts but only few pages.

Confirmed above with URL inspection from Google Search Console

Here's how I generate a sitemap for this blog (Next.js on Netlify) using next-sitemap

npm i -D next-sitemap
touch next-sitemap.config.js

Paste below to next-sitemap.config.js

/** @type {import('next-sitemap').IConfig} */ module.exports = { siteUrl: process.env.SITE_URL || 'https://example.com', generateRobotsTxt: true, // (optional) // ...other options }

Adding postbuild command to package.json worked locally

"scripts": { ... "build": "next build && next export", "postbuild": "next-sitemap", ... },

and generated following files in /public/

However, this didn't work on Netlify as is, and an easy way to fix this was just to add the above postbuild command inside the build command like so

"scripts": { ... "build": "next build && next-sitemap && next export", ... }
❯ curl https://minho42.com/sitemap.xml <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>https://minho42.com/sitemap-0.xml</loc></sitemap> </sitemapindex>%