Skip to content
Permalink
Browse files

feat: add configuration for sitemap index and multiple sitemaps

fix #6

BREAKING CHANGE: Drop support for Nuxt.js 1.x
  • Loading branch information...
NicoPennec committed Sep 23, 2019
1 parent 3428a30 commit e78e4a26fff976d83120c2cfe02763aaba1ffc0e
Showing with 830 additions and 199 deletions.
  1. +194 −51 README.md
  2. +62 −16 lib/builder.js
  3. +21 −19 lib/cache.js
  4. +76 −16 lib/generator.js
  5. +95 −7 lib/middleware.js
  6. +26 −67 lib/module.js
  7. +91 −0 lib/options.js
  8. +24 −6 lib/routes.js
  9. +1 −1 package.json
  10. +21 −1 test/__snapshots__/module.test.js.snap
  11. +31 −14 test/fixture/nuxt.config.js
  12. +188 −1 test/module.test.js
245 README.md
@@ -1,4 +1,5 @@
# Sitemap Module

[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/sitemap/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/sitemap)
[![npm](https://img.shields.io/npm/dt/@nuxtjs/sitemap.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/sitemap)
[![CircleCI](https://img.shields.io/circleci/project/github/nuxt-community/sitemap-module.svg?style=flat-square)](https://circleci.com/gh/nuxt-community/sitemap-module)
@@ -13,26 +14,49 @@
## Features

- Module based on the awesome **[sitemap.js](https://github.com/ekalinin/sitemap.js) package** ❤️
- Automatically add the static routes to the sitemap
- Create **sitemap** or **sitemap index**
- Automatically add the static routes to each sitemap
- Works with **all modes** (universal, spa, generate)
- For **Nuxt 1.x** and higher
- For **Nuxt 2.x** and higher

---

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [Sitemap Configuration](#sitemap-configuration)
- [Sitemap Index Configuration](#sitemap-index-configuration)
- [Routes Declaration](#routes-declaration)

## Installation

> npm install @nuxtjs/sitemap --save
or

## Setup
> yarn add @nuxtjs/sitemap
- Add the `@nuxtjs/sitemap` dependency with `yarn` or `npm` to your project.
## Usage

- Add `@nuxtjs/sitemap` to the `modules` section of `nuxt.config.js`:
- Add `@nuxtjs/sitemap` to the `modules` section of your `nuxt.config.js` file:

```js
modules: [
'@nuxtjs/sitemap'
]
```
> **notice:** If you use other modules (eg. `nuxt-i18n`), always declare the sitemap module at end of array (eg. `modules: ['nuxt-i18n', '@nuxtjs/sitemap']`)

- Configure it:
> **notice:**
> If you use other modules (eg. `nuxt-i18n`), always declare the sitemap module at end of array
> eg. `modules: ['nuxt-i18n', '@nuxtjs/sitemap']`
- Add a custom configuration with the `sitemap` property.

You can set a single item of [sitemap](#sitemap-configuration) or [sitemap index](#sitemap-index-configuration) or an array of item:

```js
// Setup a simple sitemap.xml
{
modules: [
'@nuxtjs/sitemap'
@@ -56,41 +80,96 @@
}
```
## Configuration
```js
// Setup a sitemap index and its linked sitemaps
{
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
path: '/sitemapindex.xml',
hostname: 'https://example.com',
lastmod: '2017-06-30',
sitemaps: [
{
path: '/sitemap-foo.xml',
routes: ['foo/1', 'foo/2'],
gzip: true
}, {
path: '/folder/sitemap-bar.xml',
routes: ['bar/1', 'bar/2'],
exclude: ['/**']
}
]
}
```
```js
// Setup several sitemaps
{
modules: [
'@nuxtjs/sitemap'
],
sitemap: [
{
path: '/sitemap-products.xml',
routes: [
// array of URL
]
}, {
path: '/sitemap-news.xml',
routes: () => // promise or function
}, {
path: '/sitemapindex.xml',
sitemaps: [{
// array of Sitemap configuration
}]
}
}
}
```
## Sitemap Options
### `routes` - array or promise function
### `routes`
- Default: `[]` or [`generate.routes`](https://nuxtjs.org/api/configuration-generate#routes) value from your `nuxt.config.js`
The `routes` parameter follows the same way than the `generate` [configuration](https://nuxtjs.org/api/configuration-generate).
See as well the [routes](#routes-1) examples below.
### `path` (optional)
See as well the [routes declaration](#routes-declaration) examples below.
### `path` (optional) - string
- Default: `/sitemap.xml`
The URL path of the generated sitemap.
### `hostname` (optional)
- Default:
### `hostname` (optional) - string
- Default:
1. `sitemap.hostname` value from your `nuxt.config.js`
2. [`build.publicPath`](https://nuxtjs.org/api/configuration-build/#publicpath) value from your `nuxt.config.js`
3. [`os.hostname()`](https://nodejs.org/api/os.html#os_os_hostname) for **generate** or **spa** mode, or dynamically based on request URL (`headers.host`) for **universal** mode
This value is **mandatory** for generation sitemap file, and you should explicitly provide it for **generate** or **spa** mode.
### `cacheTime` (optional)
### `cacheTime` (optional) - number
- Default: `1000 * 60 * 15` (15 Minutes)
Defines how frequently should sitemap **routes** being updated.
Defines how frequently should sitemap **routes** being updated (value in milliseconds).
Please note that after each invalidation, `routes` will be evaluated again. (See [routes declaration](#routes-declaration) section)
Please note that after each invalidation, `routes` will be evaluated again. (See [routes](#routes-1) section)
### `exclude` (optional) - string array
### `exclude` (optional)
- Default: `[]`
The `exclude` parameter is an array of [glob patterns](https://github.com/isaacs/minimatch#features) to exclude static routes from the generated sitemap.
### `filter` (optional)
### `filter` (optional) - function
- Default: `undefined`
If `filter` option is set as a function, all routes will be filtered through it.
@@ -125,12 +204,14 @@ Examples:
}
```
### `gzip` (optional)
### `gzip` (optional) - boolean
- Default: `false`
Enable the creation of the `.xml.gz` sitemap compressed with gzip.
### `xmlNs` (optional)
### `xmlNs` (optional) - string
- Default: `undefined`
Set the XML namespaces by override all default `xmlns` attributes in `<urlset>` element.
@@ -145,19 +226,22 @@ Set the XML namespaces by override all default `xmlns` attributes in `<urlset>`
}
```
### `xslUrl` (optional)
### `xslUrl` (optional) - string
- Default: `undefined`
The URL path of the XSL file to style the sitemap.
### `trailingSlash` (optional)
### `trailingSlash` (optional) - boolean
- Default: `false`
Add a trailing slash to each route URL (eg. `/page/1` => `/page/1/`)
> **notice:** To avoid [duplicate content](https://support.google.com/webmasters/answer/66359) detection from crawlers, you have to configure an HTTP 301 redirect between the 2 URLs (see [redirect-module](https://github.com/nuxt-community/redirect-module) or [nuxt-trailingslash-module](https://github.com/WilliamDASILVA/nuxt-trailingslash-module)).
### `defaults` (optional)
### `defaults` (optional) - object
- Default: `{}`
The `defaults` parameter set the default options for all routes.
@@ -179,7 +263,87 @@ The `defaults` parameter set the default options for all routes.
See available options: https://github.com/ekalinin/sitemap.js#usage
## Routes
## Sitemap Index Configuration
### `path` (optional) - string
- Default: `/sitemapindex.xml`
The URL path of the generated sitemap index.
### `hostname` (optional) - string
Set the `hostname` value to each sitemap linked to its sitemap index.
### `sitemaps` - array of object
- Default: `[]`
Array of [sitemap configuration](#sitemap-configuration]) linked to the sitemap index.
```js
// nuxt.config.js
sitemap: {
path: '/sitemapindex.xml',
hostname: 'https://example.com',
sitemaps: [
{
path: '/sitemap-foo.xml',
// ...
}, {
path: '/folder/sitemap-bar.xml',
// ...
}
]
}
```
```xml
<!-- generated sitemapindex.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap-foo.xml</loc>
</sitemap>
<sitemap>
<loc>https://example.com/folder/sitemap-bar.xml</loc>
</sitemap>
</sitemapindex>
```
See more [examples](#usage) above.
### `gzip` (optional) - boolean
- Default: `false`
Enable the creation of the `.xml.gz` sitemap index compressed with gzip.
### `xmlNs` (optional) - string
- Default: `undefined`
Set the XML namespaces by override all default `xmlns` attributes in `<sitemapindex>` element.
```js
// nuxt.config.js
{
sitemap: {
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
}
}
```
### `xslUrl` (optional) - string
- Default: `undefined`
The URL path of the XSL file to style the sitemap index.
## Routes Declaration
By default, the dynamic routes are ignored by the sitemap module.
Nuxt cannot automatically provide this type of complex routes.
@@ -198,6 +362,8 @@ If you want the module to add any route with dynamic parameters, you have to set
eg. add routes for `/users/:id` in the configuration:
### From a static list
```js
// nuxt.config.js
@@ -212,7 +378,7 @@ eg. add routes for `/users/:id` in the configuration:
}
```
### Function which returns a Promise
### From a function which returns a Promise
```js
// nuxt.config.js
@@ -227,35 +393,12 @@ const axios = require('axios')
}
}
}
```
### Function with a callback
**This feature is deprecated**. Use a promise-based approach instead.
```js
// nuxt.config.js
const axios = require('axios')
{
sitemap: {
routes (callback) {
axios.get('https://jsonplaceholder.typicode.com/users')
.then(res => {
let routes = res.data.map(user => '/users/' + user.username)
callback(null, routes)
})
.catch(callback)
}
}
}
```
## License
[MIT License](./LICENSE)
### Contributors
- [Nicolas PENNEC](https://github.com/NicoPennec)
- [Nicolas Pennec](https://github.com/NicoPennec)
- [Pooya Parsa](https://github.com/pi0)

0 comments on commit e78e4a2

Please sign in to comment.
You can’t perform that action at this time.