Skip to content

Commit

Permalink
feat(i18n): add x-default
Browse files Browse the repository at this point in the history
  • Loading branch information
JB AUBREE committed May 22, 2024
1 parent 45f8264 commit 5652418
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ export function getFormattedSitemap(options: ResolvedOptions, routes: string[])
priority: getOptionByRoute(options.priority, route) ?? defaultOptions.priority,
lastmod: getOptionByRoute(options.lastmod, route) ?? defaultOptions.lastmod,
}
if (options.i18n)
return Object.assign(formattedSitemap, { link: options.i18n.languages.map(str => ({ lang: str, url: str === options.i18n?.defaultLanguage ? url : `${url}/${str}` })) })
if (options.i18n) {
const languages = options.i18n.languages.map(str => ({
lang: str,
url: str === options.i18n?.defaultLanguage ? url : `${url}/${str}`,
}))
return Object.assign(formattedSitemap, {
links: options.i18n.defaultLanguage ? [...languages, { lang: 'x-default', url }] : languages,
})
}

return formattedSitemap
})
Expand Down
8 changes: 6 additions & 2 deletions test/sitemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('sitemap', () => {
{
"changefreq": "daily",
"lastmod": Any<Date>,
"link": [
"links": [
{
"lang": "fr",
"url": "http://localhost/route/fr",
Expand All @@ -143,7 +143,7 @@ describe('sitemap', () => {
{
"changefreq": "daily",
"lastmod": Any<Date>,
"link": [
"links": [
{
"lang": "fr",
"url": "http://localhost/route",
Expand All @@ -152,6 +152,10 @@ describe('sitemap', () => {
"lang": "en",
"url": "http://localhost/route/en",
},
{
"lang": "x-default",
"url": "http://localhost/route",
},
],
"priority": 1,
"url": "http://localhost/route",
Expand Down

0 comments on commit 5652418

Please sign in to comment.