nuxt-modules / sitemap

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.
https://nuxtseo.com/sitemap
327 stars 29 forks source link

Localized Sitemap Chunking Issue with i18n Integration #236

Closed V1laZ closed 3 months ago

V1laZ commented 8 months ago

Describe the bug

When generating the sitemap with the i18n module and fetched dynamic URLs, it divides the sitemap into locale chunks. However, it fails to automatically further divide these chunks when they are too large, even with the sitemaps: true setting enabled.

To reproduce

  1. package.json with the following dependencies
    "@nuxtjs/i18n": "^8.0.0",
    "@nuxtjs/sitemap": "^5.1.0",
  2. nuxt.config.ts with the following settings

    i18n: {
    vueI18n: './i18n.config.ts',
    defaultLocale: 'cs',
    strategy: 'prefix',
    langDir: 'locales/',
    lazy: true,
    locales: [
      {
        code: 'cs',
        iso: 'cs-CZ',
        name: 'Čeština',
        file: 'cs.json',
      },
      {
        code: 'en-us',
        iso: 'en-US',
        name: 'English (US)',
        file: 'en_us.json',
      },
      ...
    ]
    },
    
    sitemap: {
    sitemaps: true,
    sources: [
     `${process.env.BACKEND_URL}/api/services/sitemap/`,  // sends JSON with relative paths without locales
    ]
    },
  3. Open /sitemap.xml
  4. The sitemap is chunked by locales, but within each locale chunk, there are, for example, 13000 URLs even with sitemaps: true. Locale chunks Inside the locale chunk

Expected behavior

Either locale chunks should contain multiple chunked sitemap files if they are too large, or the sitemap should be chunked as a whole by the specified chunk size with locale paths.

Notes

I'm not sure if this is a problem with my settings, but I've tried multiple combinations and none worked. It either doesn't generate localized URLs, even when making my own endpoint and specifying _i18nTransform: true to every URL, or it chunks them by locales as described above.

harlan-zw commented 7 months ago

This is not currently supported, it will require some work to get it to behave properly but is planned for some point.

13k URLs should not be an issue for Google.

gianLuigiL commented 7 months ago

I have the same issue with 94k urls

gianLuigiL commented 7 months ago

I'm curious if there's a way to detach form localizing the sitemap as at least it would not be grouped but i can construct the urls myself and have the plugin just chunk the result

V1laZ commented 6 months ago

I've made a temporary workaround until native support becomes available. I manually generate locale alternatives for each route and allow the module to handle the chunking process.

// nuxt.config.ts

sitemap: {
  autoI18n: false,
  sitemaps: true,
  sources: [
    '/api/sitemap-urls/'
  ]
}
// server/api/sitemap-urls.ts

interface SitemapUrl {
  loc: string
  lastmod: string
}

export default defineCachedEventHandler(async (event) => {
  const config = useRuntimeConfig(event);
  const response = await $fetch<SitemapUrl[]>(`${config.public.BACKEND_URL}/api/services/sitemap/`);

  // Add languages here in ISO 639-1 format and optionally ISO 3166-1 Alpha-2 format
  const locales = ['cs', 'sk', 'en-US', 'en-GB', 'es', 'de', 'fr', 'pl', 'it'];

  return response.map(p => asSitemapUrl({
    loc: `/${locales[0]}${p.loc}`,
    lastmod: p.lastmod,
    alternatives: locales.map(lang => ({
      hreflang: lang,
      href: `/${lang}${p.loc}`
    })),
  }));
}, { maxAge: 60 * 60 * 24 }) // cache for 24 hours