next-i18next: Next dynamic routing is incompatible with next-i18next's Link
Describe the bug
I tried to use dynamic routing from Next.js with next-i18next. I imported Link
from NextI18Next instead of next/link
and it causes the error saying:
Your
<Link>'s
asvalue is incompatible with the
href value. This is invalid.
What I tried:
- Add
as
property to Link as well as remove it
Does any one have the same issue?
Occurs in next-i18next version
next-i18next version: 0.50.0
Steps to reproduce
I set up a minimal repository here: https://github.com/lazidoca/dynamic-routing-next-i18next-app
Expected behaviour
You must provide a clear and concise description of what you expected to happen.
OS (please complete the following information)
- Device: Windows 10
- Browser: Chrome 75.0.3770.142
- Node: v10.16.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 43
- Comments: 49 (13 by maintainers)
I can confirm that it is working now in next v9.2.1. @eduludi you need to supply query params to your href.
e.g.
or
This is a huge deal breaker and should be stated in the first lines of the readme. š¤¦āā
Can anyone suggest any workarounds here ?
This might be a bit hacky, but itās a pretty simple workaround and works for me.
Have your folder structure like this:
Write your code in
Page.js
and in[id].js
, the following:Then, use i18nās Link component just like this:
Client-side navigation & SSR š
Like @vimutti77, it also seems to work as expected in my project:
I also needed to replicate the same linking behaviour with
Router.push()
elsewhere ā usingRouter
fromNextI18NextInstance
ā which I got to work like so:Anyone want to roll those changes into
next-i18next
, or should we assume users are happy to handle that complexity?@isaachinman for sure I will. Anyway I think a warning or pin this issue is not a bad call. When I see an active project like this I would expect it to support and integrate with all latest features of the main lib. š¤·āā
Anyway good work. I will try to find a workaround and update here if I succeed š
@RafaPolit This problem occurs when users:
localeSubpaths
Essentially, the two are mutually exclusive due to a limitation on the NextJs side of things. Please do feel free to have a look at the implementation here and see if you can come up with a creative fix.
In general, if people want to see this implemented, the most helpful thing to do is make some noise over in the NextJs repo.
Calling i18n.changeLanguage(foo) in dynamic page also not working properly. The language is changed but the url is not.
Hi there, I have just released
next-i18next@8.0.0
, which uses the internationalised routing built into NextJs v10+. This new version ofnext-i18next
is quite different, thus making this PR no longer relevant. Thanks!@oswaldoacauan Feel free to relay your frustrations to the NextJs team. Not sure if itās a ādeal breakerā, as there are no other prebuilt options that Iām aware of. There are workarounds posted above.
@pixelass Since you seem to be having a look around, just be aware that weāve got utils like
subpath-from-lng
andsubpath-is-required
that should handle most of the heavy lifting for you.zeit/next.js/pull/9837
Iāve just used now and it works perfect.
I just upgraded to:
And refactored my next-18next implementation from removing the additions to the custom server to use rewrites in next.config.js, and all of my links start working without the hard reloading!!
We have the same setup and you can use this lib but you will not be able to use the he localeSubpath option and you will also need to write your own language switcher component that is aware of the new slug for the new language.
On Mon 27. Jul 2020 at 17:00, Gregor Adams notifications@github.com wrote:
@pixelass We would want to extend the existing component, and we would need unit tests and most likely e2e tests as well.
@vimutti77 That might be a separate issue. Any client side calls to
changeLanguage
should trigger this callback. If you think youāre experiencing a bug there, please do open a separate issue.The
Link
component thatnext-i18next
exports does not callchange
or do anything imperatively. It simply returns the defaultLink
component fromnext/link
, modifying thehref
andas
props where necessary.I believe this is related to https://github.com/zeit/next.js/issues/7488.
Hereās some JSX that causes the error, within a
next-i18next
project:With
localeSubpaths
set to"all"
, and a current language of"en"
, weād expect thisLink
to eventually lead to/en/post/second
, while displaying the page located atpages/post/[id]
. Instead, we get the error:However, this doesnāt appear to be specifically related to
next-i18next
. If you importLink
directly fromnext/link
and change your JSX to:You will see the exact same error.
Not sure what to say, except that we might need to drop
localeSubpath
support altogether, as it has always been a complicated and opinionated thing to support, and there isnāt a clear way forward.