2022-10-22

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 Cloudflare) 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/

  • robots.txt
  • sitemap.xml
  • sitemap-0.xml

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",
  ...
}

Note

If you are using Cloudflare Pages, make sure to change Settings/Builds & deployments/Build configuration as above build script instead of preset build command Cloudflare gives you when you select Framework preset: Next.js (Static HTML Export)

Done.