Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental feature switchLocalePathLinkSSR with <SwitchLocalePathLink> component #2838

Merged

Conversation

BobbieGoede
Copy link
Collaborator

@BobbieGoede BobbieGoede commented Mar 14, 2024

πŸ”— Linked issue

❓ Type of change

  • πŸ“– 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)
  • ✨ 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!

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

@BobbieGoede BobbieGoede force-pushed the feat/switch-locale-path-link-ssr branch from 30cc704 to bdd24fd Compare March 14, 2024 10:40
Copy link

nuxt-studio bot commented Mar 14, 2024

βœ… Live Preview ready!

Name Edit Preview Latest Commit
i18n Edit on Studio β†—οΈŽ View Live Preview 2508a27

@BobbieGoede BobbieGoede self-assigned this Mar 14, 2024
@BobbieGoede BobbieGoede changed the title [WIP] feat: experiment switchLocalePathLink SSR component feat: experimental switchLocalePathLinkSSR with <SwitchLocalePathLink> component Mar 14, 2024
@BobbieGoede BobbieGoede marked this pull request as ready for review March 14, 2024 12:37
@BobbieGoede BobbieGoede changed the title feat: experimental switchLocalePathLinkSSR with <SwitchLocalePathLink> component feat: experimental feature switchLocalePathLinkSSR with <SwitchLocalePathLink> component Mar 14, 2024
@BobbieGoede BobbieGoede added the v8 label Mar 14, 2024
Copy link
Collaborator

@kazupon kazupon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good for me.

@kazupon kazupon merged commit df92c6a into nuxt-modules:main Mar 14, 2024
7 checks passed
DarthGigi pushed a commit to DarthGigi/i18n that referenced this pull request Apr 16, 2024
…alePathLink>` component (nuxt-modules#2838)

* feat: `switchLocalePath` ssr

* feat: experimental `switchLocalePathLink` SSR component

* refactor: use constant for `SwitchLocalePathLinkSSR` component retrieval

* test: assert `switchLocalePathLinkSSR` functionality

* docs: add documentation for `switchLocalePathLink` component and config

* refactor: locale in wrapper comment to reduce regex matching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useLocaleHead prerenders properly. useSwitchLocalePath does not
2 participants