nextjs-app-sitemap-generator
v1.0.5
Published
A package to generate sitemaps for Next.js applications
Downloads
15
Maintainers
Readme
nextjs-app-sitemap-generator
Table of Contents
Introduction
nextjs-app-sitemap-generator
is a utility package designed to simplify the generation of sitemap.xml
and robots.txt
files for Next.js projects. It helps in managing SEO by creating comprehensive sitemaps and correctly configured robots.txt files based on the provided options.
Installation
To install this package, run:
npm install nextjs-app-sitemap-generator
or
yarn add nextjs-app-sitemap-generator
After installing the package, a generate-sitemap.mjs file will be created in the root of your project. If the file is not created automatically, you should manually create it.
Usage
Initializing the Sitemap
To initialize the sitemap and robots.txt generation, import the initializeSitemap function and provide a configuration object.
import { initializeSitemap } from 'nextjs-app-sitemap-generator';
initializeSitemap({
baseUrl: process.env.SITE_URL || 'https://example.com',
robotsTxtOptions: {
policies: [
{ userAgent: '*', allow: '/' },
{ userAgent: '*', disallow: '/api' },
{ userAgent: '*', disallow: '/admin' },
{ userAgent: '*', disallow: '/admin-login' },
]
},
sitemapOptions: {
unauthenticated: false,
authenticated: true,
ignore: ["ignore-path1", "ignore-path2"],
otherPages: [
"other-path1",
"other-path2"
],
}
});
This example demonstrates how to set up the sitemap generation in a Next.js project. The baseUrl
can be set via an environment variable or a hardcoded string. The robotsTxtOptions
and sitemapOptions
are used to configure the generated robots.txt and sitemap.xml files, respectively.
Configuration Options
Sitemap Options
| Option | Type | Description | |-----------------|---------|-------------------------------------------------------| | unauthenticated | Boolean | Generate sitemap for unauthenticated pages if true. | | authenticated | Boolean | Generate sitemap for authenticated pages if true. | | ignore | Array | List of paths to ignore in the sitemap generation. | | otherPages | Array | List of other dynamic pages to include in the sitemap.|
robots.txt Options
| Option | Type | Description | |-----------|-----------------|-------------------------------------------------------| | policies | Array | List of policy objects defining rules for different user agents. | | userAgent | String | The user agent to apply the policy to. | | allow | String/Array | Paths to allow for the user agent. | | disallow | String/Array | Paths to disallow for the user agent. |
Folder Structure
The package expects the following folder structure in your Next.js project:
src
└── app
├── (unauthenticated)
│ ├── test
│ │ └── page.tsx
│ └── example
│ └── page.tsx
├── (authenticated)
│ ├── dashboard
│ │ └── page.tsx
│ └── settings
│ └── page.tsx
├── _document.js
├── _app.js
└── ...
- The
(unauthenticated)
folder contains pages that do not require authentication. - The
(authenticated)
folder contains pages that require authentication. - Other pages and components can be placed in the root of the
app
directory or other subdirectories.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitLab.
License
This project is licensed under the MIT License. See the LICENSE file for details.