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

fix: better support for shallow route changes #1064

Merged
merged 3 commits into from
Sep 13, 2021

Conversation

skrivanos
Copy link
Contributor

@skrivanos skrivanos commented Mar 14, 2021

@vercel
Copy link

vercel bot commented Mar 14, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/isaachinman/next-i18next/3bChh7HvMWM93jXnVYUbidpHSgRL
✅ Preview: https://next-i18next-git-fork-skrivanos-shallow-rout-2dcf5b-isaachinman.vercel.app

@skrivanos
Copy link
Contributor Author

Reckon examples/simple/pages/index.ts should be changed to use shallow routing for the change locale button?

Copy link
Contributor

@isaachinman isaachinman left a comment

Choose a reason for hiding this comment

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

In general, these type of changes make me a little nervous, as it seems like even though it might fix shallow routing, it may also cause other issues.

Would something like this work as well? I'm not sure I'm keen on memoising the entire client.

Let me know your thoughts!

examples/simple/package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
src/appWithTranslation.tsx Outdated Show resolved Hide resolved
@skrivanos
Copy link
Contributor Author

In general, these type of changes make me a little nervous, as it seems like even though it might fix shallow routing, it may also cause other issues.

Would something like this work as well? I'm not sure I'm keen on memoising the entire client.

Let me know your thoughts!

I can understand that. But no, that fix alone would not help (and it's also baked into this PR). One of the problems is that creating a new instance causes two re-renders of memoized components on every shallow route change, leading to extremely poor performance.

@isaachinman
Copy link
Contributor

Sorry for a delayed response. Happy to keep this PR moving and get it merged in.

@skrivanos
Copy link
Contributor Author

skrivanos commented Mar 23, 2021

@isaachinman Noticed a slight hiccup when using this branch in our real application: switching the locale with a shallow route change will not load resources for the new language, because serverSideTranslations aren't re-run (expected). However, this cannot be fixed by including the HTTP backend either because it will not fetch missing resources if the resources config option is provided to i18next: https://www.i18next.com/how-to/add-or-load-translations#load-using-a-backend-plugin

The question is whether this should be solved in the context of this PR or not. Perhaps the scope of the PR should simply be changed (i.e. change commit message and rename it) to only cover #1059 rather than #1023 as well.

Edit:
Maybe partialBundledLanguages could solve this..?

@isaachinman
Copy link
Contributor

@skrivanos Do you have an example use case for switching the locale and wanting shallow routing at the same time? That seems like somewhat of an edge case to me.

@skrivanos
Copy link
Contributor Author

@skrivanos Do you have an example use case for switching the locale and wanting shallow routing at the same time? That seems like somewhat of an edge case to me.

@isaachinman No, I don't and I agree that it's an edge case. Although it might be considered unexpected that it doesn't work, but could perhaps just mention in the README.

@isaachinman
Copy link
Contributor

Cool, then I agree it should be handled in a separate PR (if at all).

@isaachinman
Copy link
Contributor

In general, we should be able to cease passing initialLocale via props, here, right?

@skrivanos
Copy link
Contributor Author

In general, we should be able to cease passing initialLocale via props, here, right?

Yes, I believe so. I can update the PR to remove it, just very short on time lately.

@isaachinman
Copy link
Contributor

Cool, no worries – I know the feeling.

@DanPurdy
Copy link

DanPurdy commented May 6, 2021

Hey both, just to confirm that initialLocale is safe to remove from the props returned by serverSideTranslations. Ive changed it in my PR too so once we're good to merge this I'll rebase and pull apart mine.

@skrivanos skrivanos changed the title fix: support shallow route changes fix: better support shallow route changes May 10, 2021
@skrivanos skrivanos changed the title fix: better support shallow route changes fix: better support for shallow route changes May 10, 2021
@skrivanos
Copy link
Contributor Author

@isaachinman @DanPurdy finally had time to updated the MR now - I opted to exclude #1023 since this does not solve it completely.

@isaachinman
Copy link
Contributor

@skrivanos Can you rebase against latest? This PR is my top priority and would love to get it in ASAP.

* Memoize the i18n client and do not re-create it unless the route or
  locale has changed.

* Let Next's router locale take precedence over initialLocale.

[fix i18next#1059]
@skrivanos
Copy link
Contributor Author

@skrivanos Can you rebase against latest? This PR is my top priority and would love to get it in ASAP.

@isaachinman Done!

@isaachinman
Copy link
Contributor

@skrivanos @DanPurdy This PR has caused #1484. Any objections to leaving the memoisation, but removing the router.locale check?

@skrivanos
Copy link
Contributor Author

@skrivanos @DanPurdy This PR has caused #1484. Any objections to leaving the memoisation, but removing the router.locale check?

@isaachinman Won't have time to dive into this again soon unfortunately since I'm a bit overcrowded with work at the moment. I can't quite remember why I changed it to use router.locale but there has to be a reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-renders of memoized components on shallow route changes
3 participants