[ ] ๐ Documentation (updates to the documentation, readme or JSdoc annotations)
[ ] ๐ Bug fix (a non-breaking change that fixes an issue)
[ ] ๐ Enhancement (improving an existing functionality like performance)
[x] โจ New feature (a non-breaking change that adds functionality)
[ ] ๐งน Chore (updates to the build process or auxiliary tools and libraries)
[ ] โ ๏ธ Breaking change (fix or feature that would cause existing functionality to change)
๐ Description
Resolves #2798
This adds a component SwitchLocalePathLink which is not much different from passing switchLocalePath to NuxtLink except the resulting link is wrapped in comments <!--nuxt-i18n-slp-[locale]-->...<!--/nuxt-i18n-slp-->.
While setI18nParams currently works, it actually doesn't render the correct links during SSR depending on when and where it is used (as a warning describes in the docs), I have added an experimental feature that aims to resolve this.
The following functionality is enabled with the experimental.switchLocalePathLinkSSR property:
The setI18nParams is changed to use a useState ref instead of the nuxtI18n meta property on the route, this would normally cause a hydration mismatch (lang switcher rendered before setI18nParams is called, while the client will have the correct data at hydration) which is why we couldn't use this before.
Using the wrapping comments to find the links with their locale in the rendered DOM at the end of SSR, the URL is replaced with an up to date localized path returned from switchLocalePath.
The implementation feels hacky to me but it seems to work fine, if there is a better way to do this please let me know!
๐ Linked issue
2798
โ Type of change
๐ Description
Resolves #2798
This adds a component
SwitchLocalePathLink
which is not much different from passingswitchLocalePath
toNuxtLink
except the resulting link is wrapped in comments<!--nuxt-i18n-slp-[locale]-->...<!--/nuxt-i18n-slp-->
.While
setI18nParams
currently works, it actually doesn't render the correct links during SSR depending on when and where it is used (as a warning describes in the docs), I have added an experimental feature that aims to resolve this.The following functionality is enabled with the
experimental.switchLocalePathLinkSSR
property:The
setI18nParams
is changed to use auseState
ref instead of thenuxtI18n
meta property on the route, this would normally cause a hydration mismatch (lang switcher rendered beforesetI18nParams
is called, while the client will have the correct data at hydration) which is why we couldn't use this before.Using the wrapping comments to find the links with their locale in the rendered DOM at the end of SSR, the URL is replaced with an up to date localized path returned from
switchLocalePath
.The implementation feels hacky to me but it seems to work fine, if there is a better way to do this please let me know!
๐ Checklist