vite-sitemap
Version:
Vite plugin for generating sitemaps
105 lines (79 loc) • 3.32 kB
Markdown
[](https://www.npmjs.com/package/vite-sitemap)
[](https://www.npmjs.com/package/vite-sitemap)
[](https://github.com/saeedhosan/vite-sitemap/blob/main/src/types.ts)
[](https://github.com/saeedhosan/vite-sitemap/blob/main/LICENSE.md)
## Introduction
The `vite-sitemap` plugin makes it easier to generate sitemaps for Vite projects.
It simplifies sitemap creation by producing a standardized, well-structured final output.
## Installation
Install the plugin using your preferred package manager:
```bash
npm i -D vite-sitemap
```
## Usage
To use the plugin, import it in your `vite.config.js` or `vite.config.ts` file and add it to the `plugins` array:
```ts
import { defineConfig } from "vite";
import sitemap from "vite-sitemap";
export default defineConfig({
plugins: [
sitemap({
base: "https://www.example.com",
urls: [
"about",
"privacy-policy",
"terms-and-conditions",
// Add more paths here
],
}),
],
});
```
### Configuration Options
The `sitemap` plugin accepts an options object with the following properties:
| Option | Required | Default | Description |
| ------------ | -------- | --------------- | ------------------------------------------------ |
| `base` | Yes | `"/"` | The website base url |
| `urls` | No | `[]` | An array of paths (strings) or Entry. |
| `changefreq` | No | `'daily'` | Frequency for URLs. Can be overridden per Entry. |
| `filename` | No | `'sitemap.xml'` | The name of the sitemap file. |
| `robotsTxt` | No | `undefined` | The robotsTxt accept string, null, false |
#### Configuration types
**Changefreq** : "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never"
**Entry** : `path`:string | `lastmod`: iso datetime string | `changefreq` : Changefreq
#### Example Usage
```ts
import { defineConfig } from "vite";
import sitemap from "vite-sitemap";
export default defineConfig({
plugins: [
sitemap({
base: "https://www.example.com",
urls: ["about", "contact", "privacy-policy"],
filename: "custom-sitemap.xml", // optional
changefreq: "weekly", // optional - default daily
}),
],
});
```
#### Example entry
```ts
sitemap({
changefreq: "weekly", // optional - default daily
base: "https://www.example.com",
urls: [
"about",
"blog",
{ path: "contact", changefreq: "monthly", priority: 0.7 },
{ path: "privacy-policy", lastmod: new Date().toISOString(), priority: 0.5 },
],
});
```
### Robots txt
```ts
sitemap({ robotsTxt: null }); // disabled to auto generate
sitemap({ robotsTxt: false });
sitemap({ robotsTxt: "User-agent: *" }); // with custom content
```
## Contributing
We welcome contributions! Please see our [CONTRIBUTING.md](.github/CONTRIBUTING.md) guide for more details.