next-dynamic-sitemap
Version:
A lightweight, zero-touch sitemap generator for any nextjs project
101 lines (65 loc) β’ 2.12 kB
Markdown
# πΊοΈ next-dynamic-sitemap
A zero-config dynamic sitemap generator for **Next.js** apps using the **App Router** or **Pages Router** β with full support for `generateStaticParams` and `getStaticPaths`. Automatically includes `lastModified` data from your static param functions.
## β¨ Features
- β
Works with both **App Router** (`generateStaticParams`) and **Pages Router** (`getStaticPaths`)
- π°οΈ Supports custom `lastModified` dates per route
- ποΈ Outputs to `public/sitemap.xml` automatically
- π§ Configurable via `.env` (via [`dotenv-flow`](https://www.npmjs.com/package/dotenv-flow))
- π§ Smart enough to skip dynamic routes without params
## π¦ Installation
```bash
npm install next-dynamic-sitemap
```
## π Usage
You can invoke the generator manually:
```bash
npx sitemap-gen
```
Or add it to your Next.js build step:
```json
{
"scripts": {
"build": "sitemap-gen && next build"
}
}
```
## π οΈ Example: App Router
```ts
// app/projects/[slug]/page.tsx
export async function generateStaticParams() {
const projects = await getAllProjects();
return projects.map((project) => ({
slug: project.slug,
lastModified: new Date(project.updatedAt),
}));
}
```
## π Output
The sitemap will be generated at:
```
public/sitemap.xml
```
## π Required Environment Variable
Set the base URL of your site via an env file (`.env`, `.env.local`, etc.):
```
SITEMAP_GEN_BASE_URL=https://example.com
```
This is used to fully qualify URLs in the sitemap.
## π§ͺ Works With
- β
Static Routes
- β
Dynamic Routes with `generateStaticParams` or `getStaticPaths`
- β
Catch-all / Optional Catch-all routes (as long as theyβre statically generated)
- β οΈ **Does not** include server-only or client-only routes that arenβt statically known at build time
## π‘ Tips
- Want custom priority, changefreq, or more? Thatβs coming soon β or open an issue/PR!
## π Bugs? Ideas?
[Open an issue](https://github.com/grissly-man/next-dynamic-sitemap/issues) or send a PR β contributions welcome!