jbaubree / vite-plugin-pages-sitemap

vite-plugin-pages based sitemap generator
MIT License
62 stars 2 forks source link

Multi Lingual website #465

Open xiki808 opened 1 month ago

xiki808 commented 1 month ago

Can this plugin generate sitemap for a multiple lingual website? If so, how?

jbaubree commented 1 month ago

Hi @xiki808 this is not possible for now. I will work on it as soon as possible. If you need this feature quickly, feel free too PR with an i18n config. You can find some inspiration here => sitemap-ts

jbaubree commented 1 month ago

@xiki808 you can now use v1.7.1 using i18n config!

xiki808 commented 1 month ago

@jbaubree thanks for your quick response! I'm trying it out but not working well in my situation. I am placing this configuration:

onRoutesGenerated: (routes) => {
          generateSitemap({
            routes: routes,
            i18n: { defaultLanguage: 'en', languages: ['en', 'it'] }
          })
        },

and each URL is looking like this:

        <loc>http://localhost/:locale/test/</loc>
        <lastmod>2024-05-27T09:44:35.405Z</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

As you might notice, I'm using a parameter for language and I would think that it's a common practice. Would it be possible to add this to the configuration to be replaced by the language?

Also would be nice to also have the ability to include the region for hreflangs in XML, maybe by being able to pass a key => value to the language config. Key what can go in the URL and Value what will go for hreflang

<url>
        <loc>http://localhost/KEY/test/</loc>
        <lastmod>2024-05-27T09:44:35.405Z</lastmod>
                <xhtml:link rel='alternate' hreflang='VALUE' href='http://localhost/KEY/test/' />
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
xiki808 commented 1 month ago

Actually I'm not sure how I managed to build the first few times as I'm always getting the following error now:

TypeError: [vite-plugin-pages] Could not load virtual:vite-plugin-pages/generated-pages?id=~pages (imported by node_modules/@horizon/frontend/src/lib/router.js): Invalid URL

I guess it's some conflict as I'm actually importing import routes from '~pages' inside a custom NPM package that I have which provides basic components and initiates things such as the router itself

jbaubree commented 1 month ago

Well, i think you can use dynamicRoutes to load your sitemap.

onRoutesGenerated: async (routes) => {
    const languages = ['en', 'it']
    const dynamicRoutes = languages.map(lang => `/${lang}/test`)
    generateSitemap({ routes: [...routes, ...dynamicRoutes] })
},

I think your TypeError is not depending on this plugin but directly vite-plugin-pages. You can maybe add "skipLibCheck": true in your tsconfig.json or add --skipLibCheck in your tsc command.

xiki808 commented 1 month ago

With the dynamic routes approach each variant will create a separate URL entry in the sitemap, instead of an alternate xhtml:link for the same URL

As for the TypeError, indeed that's what the error is pointing to, but if I just comment out the i18n config line the projects builds and I also get all the routes.

jbaubree commented 1 month ago

Yes sure, feel free to PR a solution for your situation. I will add this to my roadmap but will not get any time for now to develop this feature asap.