Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nuxt-community/sitemap-module
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.2.0
Choose a base ref
...
head repository: nuxt-community/sitemap-module
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.2.1
Choose a head ref
  • 5 commits
  • 10 files changed
  • 1 contributor

Commits on Apr 23, 2020

  1. fix: lastmod on sitemapindex

    fix #112
    NicoPennec committed Apr 23, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    zhukovgreen zhukovgreen
    Copy the full SHA
    56d586b View commit details
  2. Copy the full SHA
    d82ee3e View commit details
  3. Copy the full SHA
    9b4773c View commit details
  4. chore: update deps

    NicoPennec committed Apr 23, 2020
    Copy the full SHA
    3963db0 View commit details
  5. chore(release): 2.2.1

    NicoPennec committed Apr 23, 2020
    Copy the full SHA
    f4cabac View commit details
Showing with 683 additions and 563 deletions.
  1. +9 −0 CHANGELOG.md
  2. +26 −1 README.md
  3. +1 −1 lib/builder.js
  4. +16 −7 lib/generator.js
  5. +1 −1 lib/logger.js
  6. +5 −4 lib/middleware.js
  7. +11 −3 lib/options.js
  8. +1 −1 package.json
  9. +9 −6 test/module.test.js
  10. +604 −539 yarn.lock
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [2.2.1](https://github.com/nuxt-community/sitemap-module/compare/v2.2.0...v2.2.1) (2020-04-23)


### Bug Fixes

* lastmod on sitemapindex ([56d586b](https://github.com/nuxt-community/sitemap-module/commit/56d586b2ecaac977aca44103377d5c73f2f2dbc0)), closes [#112](https://github.com/nuxt-community/sitemap-module/issues/112)
* logs without additional ([9b4773c](https://github.com/nuxt-community/sitemap-module/commit/9b4773c7766422029593905853f4ddbd7ef17220))
* missing warning on generate mode ([d82ee3e](https://github.com/nuxt-community/sitemap-module/commit/d82ee3ea7b2ebcb8203a964b67f57d41f3a2c797))

## [2.2.0](https://github.com/nuxt-community/sitemap-module/compare/v2.1.0...v2.2.0) (2020-04-05)


27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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`
@@ -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: [...]
}
}
```
2 changes: 1 addition & 1 deletion lib/builder.js
Original file line number Diff line number Diff line change
@@ -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
23 changes: 16 additions & 7 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -15,14 +15,21 @@ 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) {
/* istanbul ignore if */
if (depth > 1) {
// see https://webmasters.stackexchange.com/questions/18243/can-a-sitemap-index-contain-other-sitemap-indexes
logger.warn("A sitemap index file can't list other sitemap index files, but only sitemap files")
}

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)
}
}

@@ -32,10 +39,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 = {}
@@ -64,8 +72,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)

@@ -86,7 +95,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))
)
}

2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ const consola = require('consola')
function warn(message, options = null) {
consola.warn({
message: `[sitemap-module] ${message}`,
additional: JSON.stringify(options, null, 2),
additional: options ? JSON.stringify(options, null, 2) : null,
})
}

9 changes: 5 additions & 4 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ const { excludeRoutes } = require('./routes')
* @param {number} depth
*/
function registerSitemaps(options, globalCache, nuxtInstance, depth = 0) {
/* istanbul ignore if */
if (depth > 1) {
// see https://webmasters.stackexchange.com/questions/18243/can-a-sitemap-index-contain-other-sitemap-indexes
/* istanbul ignore next */
logger.warn("A sitemap index file can't list other sitemap index files, but only sitemap files")
}

@@ -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)
}
}

@@ -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 = {}
14 changes: 11 additions & 3 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -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:
@@ -25,6 +26,7 @@ function setDefaultSitemapOptions(options, nuxtInstance) {
xmlNs: undefined,
xslUrl: undefined,
trailingSlash: false,
lastmod: undefined,
defaults: {},
}

@@ -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
@@ -59,6 +66,7 @@ function setDefaultSitemapIndexOptions(options) {
path: '/sitemapindex.xml',
hostname: undefined,
sitemaps: [],
lastmod: undefined,
gzip: false,
xmlNs: undefined,
xslUrl: undefined,
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nuxtjs/sitemap",
"version": "2.2.0",
"version": "2.2.1",
"description": "Automatically generate or serve dynamic sitemap.xml for Nuxt.js projects",
"keywords": [
"nuxt",
15 changes: 9 additions & 6 deletions test/module.test.js
Original file line number Diff line number Diff line change
@@ -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/',
@@ -418,7 +421,6 @@ describe('sitemapindex - advanced configuration', () => {
},
],
gzip: true,
lastmod,
xmlNs: 'xmlns="https://example.com/schemas/sitemap/0.9"',
xslUrl: 'sitemapindex.xsl',
},
@@ -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">')
})
Loading