feat: experimental feature switchLocalePathLinkSSR
with <SwitchLocalePathLink>
component
#2838
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🔗 Linked issue
❓ 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