next-runtime-sitemap
Version:
Generates a sitemap for nextjs apps at runtime, using a crawl of the local filesystem
138 lines (84 loc) โข 4.23 kB
Markdown
# ๐ฐ๏ธ next-runtime-sitemap
**Generate sitemaps at runtime for Next.js projects, based on your actual deployed filesystem.**
[](https://www.npmjs.com/package/next-runtime-sitemap)
[](https://github.com/grissly-man/next-runtime-sitemap/issues)
## โจ Features
- ๐ง **Runtime sitemap generation** โ no need for build-time crawling or prerendering.
- ๐ **Filesystem-aware** โ inspects your deployed `.next` directory to find generated pages.
- ๐ **Works with revalidation** โ dynamic routes are supported via `force-static` + `revalidate`.
- ๐ชถ **Now supports Pages Router** โ your sitemap can now include pages from both `app/` and `pages/` directories.
## ๐ง Requirements
> โ ๏ธ **Not compatible with serverless platforms like Vercel**
> Serverless functions run in isolated environments and only have access to files that are statically imported.
> Since this package inspects the deployed filesystem at runtime, it must run in a persistent environment (e.g., custom Node.js server, Docker, or self-hosted deployment).
- โ
Sitemap route must be hosted via the **App Router** (`app/` directory).
- โ
Supports both App Router and Pages Router pages in the generated sitemap.
- โ
You must enable `force-static` + `revalidate` on dynamic routes for them to persist to the filesystem.
- โ
Static routes are automatically captured.
- โ Fully dynamic (e.g., `force-dynamic`) routes are **not** includedโthey donโt emit files.
## ๐ ๏ธ Installation
```bash
npm install next-runtime-sitemap
```
## ๐ฆ Usage
Add a route file to your Next.js app at:
```
app/sitemaps/[sitemap]/route.ts
```
Inside that file:
```ts
import { generateAppRouterSitemap } from "next-runtime-sitemap/dist/app";
module.exports = generateAppRouterSitemap();
```
> ๐ This route **must** live inside the App Router, but it can generate a sitemap that includes both App and Pages Router pages.
## ๐ Configuration
### Base URL
### Internationalization (i18n)
If your project uses [Next.js internationalized routing](https://nextjs.org/docs/advanced-features/i18n-routing), the sitemap will now automatically include both:
- All localized routes (e.g., `mysite.com/en/about`)
- Their de-localized equivalents (e.g., `mysite.com/about`), mapped from the default locale
To enable this behavior, pass your default locale explicitly when generating the sitemap:
```ts
import { generateAppRouterSitemap } from "next-runtime-sitemap/dist/app";
module.exports = generateAppRouterSitemap({ defaultLocale: "en" });
```
You must provide a `SITEMAP_GEN_BASE_URL` environment variable.
Itโs used to generate absolute URLs in your sitemap.
For example, in `.env.local`:
```
SITEMAP_GEN_BASE_URL=https://your-domain.com
```
This package uses [`dotenv-workflow`](https://www.npmjs.com/package/dotenv-workflow), so it will respect `.env`, `.env.local`, `.env.development`, etc.
## ๐ Related
This package depends on [`next-dynamic-sitemap`](https://www.npmjs.com/package/next-dynamic-sitemap) internally for utility functions, but:
- `next-dynamic-sitemap` is for **build-time** sitemap generation.
- `next-runtime-sitemap` is for **runtime** sitemap generation.
Use the right one depending on your architecture.
## ๐ Example Output
Visit `/sitemaps/sitemap.xml` in your browser, or curl it:
```bash
curl https://your-domain.com/sitemaps/sitemap.xml
```
Youโll get a standard XML sitemap that reflects your currently built static and dynamic (with revalidation) pagesโincluding those from the Pages Router.
## ๐งช Scripts
```bash
npm run build # Compile TypeScript
npm test # Run tests inside the /test directory
npm run format # Format code with Prettier
npm run check-format # Check code formatting
```
## ๐ License
[ISC License](./LICENSE)
## ๐ Issues / Feedback
File issues or feature requests at:
๐ [https://github.com/grissly-man/next-runtime-sitemap/issues](https://github.com/grissly-man/next-runtime-sitemap/issues)