FrontEnd Web

FrontendWeb is a platform for writers and readers to read and write a new post about frontend…

NextJs

How to Add a sitemap for Static Site Generation (SSG) in the next.js app?

Rajdeep Singh
FrontEnd Web
Published in
4 min readApr 25, 2022

--

How to Add a sitemap for Static Site Generation (SSG) in the nextjs app?
How to Add a sitemap for Static Site Generation (SSG) in the next.js app?

Static Site Generation (SSG)

Steps:

Note:

Install NPM Package

npm i next-sitemap -D

Config NPM package

next-sitemap.js
/** @type {import('next-sitemap').IConfig} */// Default code you can customize according to your requirements.module.exports = {siteUrl: process.env.SITE_URL || 'https://example.com',generateRobotsTxt: true, // (optional)// REST CODE READ DOCS  ...}

Config NPM Script

"postbuild": "next-sitemap"
"scripts": {    "dev": "next dev",    "build": "next build",    "start": "next start",    "lint": "next lint",    "postbuild": "next-sitemap"},

Create Sitemap

npm run build
> officialstaticblog@0.1.0 build
> next build
info - Loaded env from /home/rajdeepsingh/Downloads/officialstaticblog/.env
info - Checking validity of types
info - Creating an optimized production build
info - Compiled successfully
info - Collecting page data
[ ] info - Generating static pages (0/88)[]
[]
info - Generating static pages (88/88)
info - Finalizing page optimization
Page Size First Load JS
┌ ● / (679 ms) 3.02 kB 82.5 kB
├ /_app 0 B 79.4 kB
├ ○ /404 192 B 79.6 kB
├ λ /api/hello 0 B 79.4 kB
├ ● /blog/[slug] (1690 ms) 1 kB 91.6 kB
├ ├ /blog/how-is-npm-install-command
├ ├ /blog/how-to-add-css-in-next-js
├ ├ /blog/how-to-capture-screenshots-in-raspberry-pi-4
├ └ [+7 more paths]
├ ● /category/[slug] (3444 ms) 903 B 91.5 kB
├ ├ /category/npm (446 ms)
├ ├ /category/npmcli
├ ├ /category/npm-install-command
├ ├ /category/nextjs
├ ├ /category/next
├ ├ /category/nextjs-framework
├ ├ /category/nextjs-tutorial
├ └ [+29 more paths]
├ ○ /Search (543 ms) 2.58 kB 82 kB
└ ● /tag/[slug] (3619 ms) 896 B 91.5 kB
├ /tag/npm
├ /tag/npmcli
├ /tag/npm-install-command
└ [+35 more paths]
+ First Load JS shared by all 79.4 kB
├ chunks/framework-fc97f3f1282ce3ed.js 44.9 kB
├ chunks/main-551bef8982a15171.js 28.2 kB
├ chunks/pages/_app-817c4c49804bf4a7.js 5.65 kB
├ chunks/webpack-fd82975a6094609f.js 727 B
└ css/98ed866367cddfc5.css 299 B
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
○ (Static) automatically rendered as static HTML (uses no initial props)
● (SSG) automatically generated as static HTML + JSON (uses getStaticProps)
> officialstaticblog@0.1.0 postbuild
> next-sitemap
Loaded env from /home/rajdeepsingh/Downloads/officialstaticblog/.env
✅ [next-sitemap] Generated index sitemap and 1 sitemap(s)
http://localhost:3000/sitemap.xml (index)
http://localhost:3000/sitemap-0.xml

Note:

Other or Another method

Conclusion

--

--

FrontEnd Web
FrontEnd Web

Published in FrontEnd Web

FrontendWeb is a platform for writers and readers to read and write a new post about frontend developers, full-stack developers, Web developers, and Computer science. It is an open-source community that joins and gathers frontend developers to build and share knowledge with other

Rajdeep Singh
Rajdeep Singh

Written by Rajdeep Singh

Follow me if you learn more about JavaScript | TypeScript | React.js | Next.js | Linux | NixOS | Frontend Developer | https://linktr.ee/officialrajdeepsingh

Responses (2)