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.3.2
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.4.0
Choose a head ref
  • 4 commits
  • 8 files changed
  • 2 contributors

Commits on Jun 25, 2020

  1. fix: generate an <url> for each i18n language available

    The "sitemap.i18n.defaultLocale" option is no longer needed.
    
    fix #140
    NicoPennec committed Jun 25, 2020

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    b6d79c8 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    f0365d2 View commit details
  3. chore: update deps

    NicoPennec committed Jun 25, 2020

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    fd58e85 View commit details
  4. chore(release): 2.4.0

    NicoPennec committed Jun 25, 2020

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    cf0b6a0 View commit details
Showing with 1,286 additions and 695 deletions.
  1. +12 −0 CHANGELOG.md
  2. +26 −2 README.md
  3. +29 −31 lib/builder.js
  4. +2 −0 lib/module.js
  5. +4 −5 lib/options.js
  6. +1 −1 package.json
  7. +30 −22 test/module.test.js
  8. +1,182 −634 yarn.lock
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,18 @@

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.4.0](https://github.com/nuxt-community/sitemap-module/compare/v2.3.2...v2.4.0) (2020-06-25)


### Features

* add hooks on sitemap generation ([f0365d2](https://github.com/nuxt-community/sitemap-module/commit/f0365d233d9881b613d346dfed6aef951139385d))


### Bug Fixes

* generate an <url> for each i18n language available ([b6d79c8](https://github.com/nuxt-community/sitemap-module/commit/b6d79c890a1be0cf9919fe7b1042e8b90e19ac6f)), closes [#140](https://github.com/nuxt-community/sitemap-module/issues/140)

### [2.3.2](https://github.com/nuxt-community/sitemap-module/compare/v2.3.1...v2.3.2) (2020-06-15)


28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
- [Sitemap Options](#sitemap-options)
- [Sitemap Index Options](#sitemap-index-options)
- [Routes Declaration](#routes-declaration)
- [Hooks](#hooks)

## Installation

@@ -373,10 +374,9 @@ Example:
sitemap: {
hostname: 'https://example.com',
// shortcut notation (basic)
i18n: 'en',
i18n: true,
// nuxt-i18n notation (advanced)
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'fr'],
routesNameSeparator: '___'
}
@@ -391,6 +391,18 @@ Example:
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/es/"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>
</url>
<url>
<loc>https://example.com/es/</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/es/"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>
</url>
<url>
<loc>https://example.com/fr/</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/es/"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>
</url>
```

### `defaults` (optional) - object
@@ -580,6 +592,18 @@ const axios = require('axios')
}
```


## Hooks

Hooks are listeners to Nuxt events. [Learn more](https://nuxtjs.org/api/configuration-hooks)

You can register hooks on certain life cycle events.

| Hook | Arguments | When |
|---|---|---|
| sitemap:generate:before | (nuxt, sitemapOptions) | Hook on before site generation |
| sitemap:generate:done | (nuxt) | Hook on sitemap generation finished |

## License

[MIT License](./LICENSE)
60 changes: 29 additions & 31 deletions lib/builder.js
Original file line number Diff line number Diff line change
@@ -44,52 +44,50 @@ function createSitemap(options, routes, base = null, req = null) {
})
}

// Group each route with its alternative languages
// Add alternate i18n routes
if (options.i18n) {
const { defaultLocale, locales, routesNameSeparator } = options.i18n
const { locales, routesNameSeparator } = options.i18n

// Set alternate routes for each page
const i18nRoutes = routes.reduce((i18nRoutes, route, index) => {
routes.reduce((i18nRoutes, route) => {
if (!route.name) {
// Route without alternate link
i18nRoutes[`#${index}`] = route
return i18nRoutes
}

let [page, lang, isDefault] = route.name.split(routesNameSeparator) // eslint-disable-line prefer-const
const [page, lang, isDefault = false] = route.name.split(routesNameSeparator)

// Get i18n route, or init it
const i18nRoute = i18nRoutes[page] || { ...route }

if (lang) {
// Set main link
if (isDefault) {
lang = 'x-default'
}
if (lang === defaultLocale) {
i18nRoute.url = route.url
}
if (!lang) {
return i18nRoutes
}

// Set alternate links
if (!i18nRoute.links) {
i18nRoute.links = []
// Init alternate route
const link = {
lang,
url: join('.', route.url),
}
if (isDefault) {
link.lang = 'x-default'
} else {
const locale = locales.find(({ code }) => code === lang)
if (locale && locale.iso) {
link.lang = locale.iso
}
}

const locale = locales.find(({ code }) => code === lang) || { iso: lang }
i18nRoute.links.push({
lang: locale.iso,
url: join('.', route.url),
})
} else {
// No alternate link found
i18nRoute.url = route.url
// Group alternate routes by page and sorted by lang
if (!i18nRoutes[page]) {
i18nRoutes[page] = []
}
const langs = i18nRoutes[page].map(({ lang }) => lang)
langs.push(link.lang)
const index = langs.sort().indexOf(link.lang)
i18nRoutes[page].splice(index, 0, link)

// Set alternate routes
route.links = i18nRoutes[page]

i18nRoutes[page] = i18nRoute
return i18nRoutes
}, {})

routes = Object.values(i18nRoutes)
}

// Enable the custom filter function for each declared route
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -39,8 +39,10 @@ module.exports = async function module(moduleOptions) {

// On "generate" mode, generate static files for each sitemap or sitemapindex
nuxtInstance.nuxt.hook('generate:done', async () => {
await nuxtInstance.nuxt.callHook('sitemap:generate:before', nuxtInstance, options)
logger.info('Generating sitemaps')
await Promise.all(options.map((options) => generateSitemaps(options, globalCache, nuxtInstance)))
await nuxtInstance.nuxt.callHook('sitemap:generate:done', nuxtInstance)
})

// On "ssr" mode, register middlewares for each sitemap or sitemapindex
9 changes: 4 additions & 5 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ function setDefaultSitemapOptions(options, nuxtInstance, isLinkedToSitemapIndex
const defaults = {
path: '/sitemap.xml',
hostname:
// TODO: remove support of "build.publicPath" on release 3.0
nuxtInstance.options.build.publicPath !== DEFAULT_NUXT_PUBLIC_PATH
? nuxtInstance.options.build.publicPath
: undefined,
@@ -48,16 +49,14 @@ function setDefaultSitemapOptions(options, nuxtInstance, isLinkedToSitemapIndex
)
}

// Shortcut notation
/* istanbul ignore if */
if (typeof sitemapOptions.i18n === 'string') {
sitemapOptions.i18n = {
defaultLocale: sitemapOptions.i18n,
}
// TODO: remove support of "string" as shortcut notation on release 3.0
sitemapOptions.i18n = true
}

// Set default i18n options
sitemapOptions.i18n = {
defaultLocale: '',
locales: [],
routesNameSeparator: '___',
...sitemapOptions.i18n,
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.3.2",
"version": "2.4.0",
"description": "Automatically generate or serve dynamic sitemap.xml for Nuxt.js projects",
"keywords": [
"nuxt",
52 changes: 30 additions & 22 deletions test/module.test.js
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ describe('sitemap - advanced configuration', () => {
const sitemapConfig = {
hostname: 'https://example.com',
trailingSlash: true,
i18n: 'en',
i18n: true,
routes: ['foo', { url: 'bar' }],
}

@@ -386,14 +386,18 @@ describe('sitemap - advanced configuration', () => {
sitemap: sitemapConfig,
})

const links = [
'<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/"/>',
'<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>',
].join('')

const xml = await get('/sitemap.xml')
expect(xml).not.toContain('<loc>https://example.com/</loc>')
expect(xml).toContain('<loc>https://example.com/en/</loc>')
expect(xml).not.toContain('<loc>https://example.com/fr/</loc>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>')
expect(xml).toContain(`<url><loc>https://example.com/en/</loc>${links}</url>`)
expect(xml).toContain(`<url><loc>https://example.com/fr/</loc>${links}</url>`)
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/en/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/fr/"/>')
})

test('strategy "prefix_except_default"', async () => {
@@ -407,14 +411,18 @@ describe('sitemap - advanced configuration', () => {
sitemap: sitemapConfig,
})

const links = [
'<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>',
'<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>',
].join('')

const xml = await get('/sitemap.xml')
expect(xml).toContain('<loc>https://example.com/</loc>')
expect(xml).not.toContain('<loc>https://example.com/en/</loc>')
expect(xml).not.toContain('<loc>https://example.com/fr/</loc>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>')
expect(xml).toContain(`<url><loc>https://example.com/</loc>${links}</url>`)
expect(xml).toContain(`<url><loc>https://example.com/fr/</loc>${links}</url>`)
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/en/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/fr/"/>')
})

test('strategy "prefix_and_default"', async () => {
@@ -427,20 +435,21 @@ describe('sitemap - advanced configuration', () => {
},
sitemap: {
...sitemapConfig,
i18n: {
defaultLocale: 'x-default',
},
},
})

const links = [
'<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/"/>',
'<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>',
'<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/"/>',
].join('')

const xml = await get('/sitemap.xml')
expect(xml).toContain('<loc>https://example.com/</loc>')
expect(xml).not.toContain('<loc>https://example.com/en/</loc>')
expect(xml).not.toContain('<loc>https://example.com/fr/</loc>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/"/>')
expect(xml).toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/"/>')
expect(xml).toContain(`<url><loc>https://example.com/</loc>${links}</url>`)
expect(xml).toContain(`<url><loc>https://example.com/fr/</loc>${links}</url>`)
expect(xml).toContain(`<url><loc>https://example.com/en/</loc>${links}</url>`)
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/en/"/>')
expect(xml).not.toContain('<xhtml:link rel="alternate" hreflang="x-default" href="https://example.com/fr/"/>')
})

test('locales with iso values', async () => {
@@ -458,7 +467,6 @@ describe('sitemap - advanced configuration', () => {
sitemap: {
...sitemapConfig,
i18n: {
defaultLocale: 'en',
locales,
},
},
Loading