Change routes, based on locale detected in base method (way to localize paths) #32
Replies: 2 comments 3 replies
-
If I understood correctly, you want to localize the slug, right? As in AliasesAn idea would be using route aliases for the slug part, and the Using route children
Adding routes dynamicallyAs you mentioned, using import viteSSR from 'vite-ssr'
import { addPagePropsGetterToRoutes } from 'vite-ssr/vue/utils'
viteSSR(App, { routes }, ({ router, initialRoute }) => {
// main hook
const localizedRoutes = getYourLocalizedRoutes({ initialRoute }) // initialRoute should contain useful information
addPagePropsGetterToRoutes(localizedRoutes)
localizedRoutes.forEach(router.addRoute)
}) External libsThere's also -- Hope something here helps! |
Beta Was this translation helpful? Give feedback.
-
I tried adding the routes dynamically like presented in your solution, but I end up with the same result as my first tries: The SSR version loads well and the source for the localized (slug) route is full and working as expected. I can event see my page for a few milliseconds until Vue kicks in. As Vue initiate in after page load, the page disappears and become empty:
I wonder if it is related to my Vue skills, because I do it that way (using your SSR boilerplate project) and not exactly as your example up there, I might be updating my routes at the bad place?
Thanks again for your answer, I really appreciate! 🙌 |
Beta Was this translation helpful? Give feedback.
-
Hi,
Not sure it's the right place but I'm taking a chance here since it may generate ideas for ways to implement this (in a smart way that I'm tring to) in the future.
I'm trying to give to ViteSSR different routes based on localization.
First idea was to try to set the routes (with router.addroute()) inside of the base method inside ViteSSR export, but it's too late to edit the routes, it absolutely has to happen before.
Then I tried editing the routes in the
async(ctx) => {}
, and it's working but for the SSR part only, because on hydration, the page disappears. I assume it's because SSR and Vue hydration don't have the same routes to work with.I'm still learning Vue and I'm sorry if some things seems obvious. I'm using the boilerplate by the author of this package to help me getting started: https://github.com/frandiox/vitesse-ssr-template
Here is what I got to:
setLocalizedRoutes() adds the localized routes to the router (and works, because router.getRoutes() gives me the good localized routes juste under).
I tried setting up before ViteSSR export, but router.currentRoute is always set at / and no way to check the language from URL then, and it makes sense it's not set yet.
Would there be a way to get routes based on language to work in that context? I don't mind losing the instant translation on language switching.
Any help would be welcome, thank you so so much.
Beta Was this translation helpful? Give feedback.
All reactions