Skip to content

Commit

Permalink
fix: lastmod on sitemapindex
Browse files Browse the repository at this point in the history
fix #112
  • Loading branch information
NicoPennec committed Apr 23, 2020
1 parent 939af8e commit 56d586b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
27 changes: 26 additions & 1 deletion README.md
Expand Up @@ -359,6 +359,30 @@ Array of [sitemap configuration](#sitemap-options]) linked to the sitemap index.

See more [examples](#usage) above.

### `lastmod` (optional) - string

Set the `lastmod` value to each sitemap linked to its sitemap index.

In addition, the `lastmod` can be defined for each linked sitemap.

```js
// nuxt.config.js

{
sitemap: {
lastmod: "2020-01-01",
sitemaps: [
{
path: '/sitemap-foo.xml',
lastmod: "2020-01-02"
}, {
path: '/sitemap-bar.xml'
}
]
}
}
```

### `gzip` (optional) - boolean

- Default: `false`
Expand All @@ -376,7 +400,8 @@ Set the XML namespaces by override all default `xmlns` attributes in `<sitemapin

{
sitemap: {
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"',
sitemaps: [...]
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion lib/builder.js
Expand Up @@ -88,7 +88,7 @@ function createSitemapIndex(options, base = null, req = null) {
const path = join('.', options.gzip ? `${options.path}.gz` : options.path)
const hostname = getHostname(options.hostname ? options : { ...options, hostname: defaultHostname }, req, base)
const url = new URL(path, hostname)
return url.href
return { url: url.href, lastmod: options.lastmod }
})

// Set lastmod for each sitemap
Expand Down
17 changes: 10 additions & 7 deletions lib/generator.js
Expand Up @@ -15,14 +15,15 @@ const { excludeRoutes } = require('./routes')
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
async function generateSitemaps(options, globalCache, nuxtInstance) {
async function generateSitemaps(options, globalCache, nuxtInstance, depth = 0) {
const isSitemapIndex = options && options.sitemaps && Array.isArray(options.sitemaps) && options.sitemaps.length > 0

if (isSitemapIndex) {
await generateSitemapIndex(options, globalCache, nuxtInstance)
await generateSitemapIndex(options, globalCache, nuxtInstance, depth)
} else {
await generateSitemap(options, globalCache, nuxtInstance)
await generateSitemap(options, globalCache, nuxtInstance, depth)
}
}

Expand All @@ -32,10 +33,11 @@ async function generateSitemaps(options, globalCache, nuxtInstance) {
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
async function generateSitemap(options, globalCache, nuxtInstance) {
async function generateSitemap(options, globalCache, nuxtInstance, depth = 0) {
// Init options
options = setDefaultSitemapOptions(options, nuxtInstance)
options = setDefaultSitemapOptions(options, nuxtInstance, depth > 0)

// Init cache
const cache = {}
Expand Down Expand Up @@ -64,8 +66,9 @@ async function generateSitemap(options, globalCache, nuxtInstance) {
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
async function generateSitemapIndex(options, globalCache, nuxtInstance) {
async function generateSitemapIndex(options, globalCache, nuxtInstance, depth = 0) {
// Init options
options = setDefaultSitemapIndexOptions(options)

Expand All @@ -86,7 +89,7 @@ async function generateSitemapIndex(options, globalCache, nuxtInstance) {

// Generate linked sitemaps
await Promise.all(
options.sitemaps.map((sitemapOptions) => generateSitemaps(sitemapOptions, globalCache, nuxtInstance))
options.sitemaps.map((sitemapOptions) => generateSitemaps(sitemapOptions, globalCache, nuxtInstance, depth + 1))
)
}

Expand Down
7 changes: 4 additions & 3 deletions lib/middleware.js
Expand Up @@ -29,7 +29,7 @@ function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
if (isSitemapIndex) {
registerSitemapIndex(options, globalCache, nuxtInstance, depth)
} else {
registerSitemap(options, globalCache, nuxtInstance)
registerSitemap(options, globalCache, nuxtInstance, depth)
}
}

Expand All @@ -39,12 +39,13 @@ function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
* @param {number} depth
*/
function registerSitemap(options, globalCache, nuxtInstance) {
function registerSitemap(options, globalCache, nuxtInstance, depth = 0) {
const base = nuxtInstance.options.router.base

// Init options
options = setDefaultSitemapOptions(options, nuxtInstance)
options = setDefaultSitemapOptions(options, nuxtInstance, depth > 0)

// Init cache
const cache = {}
Expand Down
14 changes: 11 additions & 3 deletions lib/options.js
Expand Up @@ -5,11 +5,12 @@ const DEFAULT_NUXT_PUBLIC_PATH = '/_nuxt/'
/**
* Set default options for a sitemap config
*
* @param {Object} options
* @param {Nuxt} nuxtInstance
* @param {Object} options
* @param {Nuxt} nuxtInstance
* @param {boolean} isLinkedToSitemapIndex
* @returns {Object}
*/
function setDefaultSitemapOptions(options, nuxtInstance) {
function setDefaultSitemapOptions(options, nuxtInstance, isLinkedToSitemapIndex = false) {
const defaults = {
path: '/sitemap.xml',
hostname:
Expand All @@ -25,6 +26,7 @@ function setDefaultSitemapOptions(options, nuxtInstance) {
xmlNs: undefined,
xslUrl: undefined,
trailingSlash: false,
lastmod: undefined,
defaults: {},
}

Expand All @@ -43,6 +45,11 @@ function setDefaultSitemapOptions(options, nuxtInstance) {
logger.fatal('The `path` option is either empty or missing in your config for a sitemap', options)
}

/* istanbul ignore if */
if (sitemapOptions.lastmod && !isLinkedToSitemapIndex) {
logger.warn('The `lastmod` option is only available in the config of a sitemap linked to a sitemap index')
}

sitemapOptions.pathGzip = sitemapOptions.gzip ? `${sitemapOptions.path}.gz` : sitemapOptions.path

return sitemapOptions
Expand All @@ -59,6 +66,7 @@ function setDefaultSitemapIndexOptions(options) {
path: '/sitemapindex.xml',
hostname: undefined,
sitemaps: [],
lastmod: undefined,
gzip: false,
xmlNs: undefined,
xslUrl: undefined,
Expand Down
15 changes: 9 additions & 6 deletions test/module.test.js
Expand Up @@ -398,18 +398,21 @@ describe('sitemapindex - minimal configuration', () => {
describe('sitemapindex - advanced configuration', () => {
let nuxt = null
let xml = null
const lastmod = new Date().toISOString()
const today = new Date().toISOString()
const yesterday = new Date(new Date() - 1000 * 60 * 60 * 24).toISOString()

beforeAll(async () => {
nuxt = await startServer({
...config,
sitemap: {
path: '/sitemapindex.xml',
hostname: 'https://example.com/',
lastmod: today,
sitemaps: [
{
path: '/sitemap-foo.xml',
routes: ['foo/1', 'foo/2'],
lastmod: yesterday,
},
{
hostname: 'https://example.fr/',
Expand All @@ -418,7 +421,6 @@ describe('sitemapindex - advanced configuration', () => {
},
],
gzip: true,
lastmod,
xmlNs: 'xmlns="https://example.com/schemas/sitemap/0.9"',
xslUrl: 'sitemapindex.xsl',
},
Expand All @@ -432,16 +434,17 @@ describe('sitemapindex - advanced configuration', () => {
expect(xml).toContain('<loc>https://example.fr/sitemap-bar.xml</loc>')
})

test('custom lastmod', () => {
expect(xml).toContain(`<lastmod>${today}</lastmod>`)
expect(xml).toContain(`<lastmod>${yesterday}</lastmod>`)
})

test('gzip enabled', async () => {
const gz = await getGzip('/sitemapindex.xml.gz')
const sitemap = gunzipSync(gz).toString()
expect(xml).toEqual(sitemap)
})

test('custom lastmod', () => {
expect(xml).toContain(`<lastmod>${lastmod}</lastmod>`)
})

test('custom XML namespaces', () => {
expect(xml).toContain('<sitemapindex xmlns="https://example.com/schemas/sitemap/0.9">')
})
Expand Down

0 comments on commit 56d586b

Please sign in to comment.