Skip to content

Commit

Permalink
fix(i18n): broken dedupe of loc and alternatives (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw authored Aug 30, 2024
1 parent 92d9610 commit 2b16423
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare module 'nitropack' {
}

export function includesSitemapRoot(sitemapName: string, routes: string[]) {
return routes.includes(`/sitemap.xml`) || routes.includes(`/${sitemapName}`) || routes.includes('/sitemap_index.xml')
return routes.includes(`/__sitemap__/`) || routes.includes(`/sitemap.xml`) || routes.includes(`/${sitemapName}`) || routes.includes('/sitemap_index.xml')
}

export function isNuxtGenerate(nuxt: Nuxt = useNuxt()) {
Expand Down Expand Up @@ -66,7 +66,7 @@ export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeCo
nitro.hooks.hook('prerender:generate', async (route) => {
const html = route.contents
// extract alternatives from the html
if (!route.fileName?.endsWith('.html') || !html)
if (!route.fileName?.endsWith('.html') || !html || ['/200.html', '/404.html'].includes(route.route))
return

// maybe the user already provided a _sitemap on the route
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/nitro/sitemap/builder/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function resolveSitemapEntries(sitemap: SitemapDefinition, sources: Sitem
return false
e._locale = locale
e._index = i
e._key = `${e._sitemap || ''}${e._path?.pathname || '/'}${e._path.search}`
withoutPrefixPaths[pathWithoutPrefix] = withoutPrefixPaths[pathWithoutPrefix] || []
// need to make sure the locale doesn't already exist
if (!withoutPrefixPaths[pathWithoutPrefix].some(e => e._locale.code === locale.code))
Expand Down Expand Up @@ -122,7 +123,7 @@ export function resolveSitemapEntries(sitemap: SitemapDefinition, sources: Sitem
_sitemap,
...e,
_index: undefined,
_key: `${_sitemap || ''}${loc}`,
_key: `${_sitemap || ''}${loc || '/'}${e._path.search}`,
_locale: l,
loc,
alternatives: [{ code: 'x-default', _hreflang: 'x-default' }, ...autoI18n.locales].map((locale) => {
Expand Down Expand Up @@ -163,6 +164,7 @@ export function resolveSitemapEntries(sitemap: SitemapDefinition, sources: Sitem
}
if (isI18nMapped) {
e._sitemap = e._sitemap || e._locale._sitemap
e._key = `${e._sitemap || ''}${e.loc || '/'}${e._path.search}`
}
if (e._index)
_urls[e._index] = e
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/nitro/sitemap/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export async function createSitemap(event: H3Event, definition: SitemapDefinitio

const maybeSort = (urls: ResolvedSitemapUrl[]) => runtimeConfig.sortEntries ? sortSitemapUrls(urls) : urls
// final urls
const urls = maybeSort(mergeOnKey(resolvedCtx.urls.map(e => normaliseEntry(e, definition.defaults, resolvers)), '_key'))
const normalizedPreDedupe = resolvedCtx.urls.map(e => normaliseEntry(e, definition.defaults, resolvers))
console.log(normalizedPreDedupe)
const urls = maybeSort(mergeOnKey(normalizedPreDedupe, '_key').map(e => normaliseEntry(e, definition.defaults, resolvers)))
const sitemap = urlsToXml(urls, resolvers, runtimeConfig)

const ctx = { sitemap, sitemapName }
Expand Down
6 changes: 3 additions & 3 deletions test/unit/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('i18n', () => {
{
"_abs": false,
"_index": 0,
"_key": "/en/dynamic/foo",
"_key": "en-US/en/dynamic/foo",
"_locale": {
"_hreflang": "en-US",
"_sitemap": "en-US",
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('i18n', () => {
{
"_abs": false,
"_index": 1,
"_key": "/fr/dynamic/foo",
"_key": "fr-FR/fr/dynamic/foo",
"_locale": {
"_hreflang": "fr-FR",
"_sitemap": "fr-FR",
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('i18n', () => {
{
"_abs": false,
"_index": 3,
"_key": "english-url",
"_key": "en-USenglish-url",
"_locale": {
"_hreflang": "en-US",
"_sitemap": "en-US",
Expand Down

0 comments on commit 2b16423

Please sign in to comment.