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

Link and router query should automatically replace dynamic route parts #14959

Closed
jstcki opened this issue Jul 8, 2020 · 2 comments · Fixed by #16774
Closed

Link and router query should automatically replace dynamic route parts #14959

jstcki opened this issue Jul 8, 2020 · 2 comments · Fixed by #16774
Assignees
Milestone

Comments

@jstcki
Copy link
Contributor

jstcki commented Jul 8, 2020

Feature request

It would be great if the Link component was smart enough to replace dynamic route parts in pathname automatically based on the query when using the url-object form of href:

<Link href={{ pathname: "/posts/[id]", query: { id: 1, anotherParam: 2 } }}><a>...</a></Link>

Currently this renders <a href="/posts/[id]?id=1&anotherParam=2">...</a> which is functional but odd (also it breaks getServerSideProps et al params). So in order to make this work, I have to replace the dynamic parts in pathname with matching query params and stringify a search to construct an as prop manually.

The same issue concerns router.push and router.replace.

Another example to illustrate the issue where it's quite tedious to construct the as prop would be to replace one or more query params of the current (unknown) route to link to another locale:

const LinkCurrentRouteToLocale = ({ locale, ...rest }) => {
  const { pathname, query } = useRouter();
  // pathname being /[locale]/foo or similar
  return <Link href={{ pathname, query: { ...query, locale } }} {...rest} />
}

Describe the solution you'd like

If this was handled out of the box, using { pathname: "/posts/[id]", query: { id: 1, anotherParam: 2 } } would create the URL /posts/1?anotherParam=2 and the link would be <a href="/posts/1?anotherParam=2">...</a>.

I think adding this behavior would be a non-breaking change without any new API. It would be a big improvement over the current behavior. I also don't think that it would be at odds with a more sophisticated approach described in #8207

Describe alternatives you've considered

An alternative could be to explicitly add a params prop to the url object form, e.g.

<Link href={{ pathname: "/posts/[id]", params: { id: 1 }, query: { anotherParam: 2 } }}><a>...</a></Link>

But I don't see the benefit of doing this.

P.S.: I already posted this suggestion in #8207 (comment) but as that RFC had a much bigger scope it probably wasn't the right place to discuss this.

@timneutkens
Copy link
Member

timneutkens commented Jul 8, 2020

Making your suggestion work vs #8207 is the same amount of effort and amount of changes so I'd like to focus on #8207 instead. I've done a lot of work to prepare for that change recently.

@Timer Timer added the kind: bug label Sep 2, 2020
@kodiakhq kodiakhq bot closed this as completed in #16774 Sep 2, 2020
kodiakhq bot pushed a commit that referenced this issue Sep 2, 2020
This corrects/makes sure interpolating dynamic route values for `href` works correctly. This provides an alternative approach to building the `href` value with `next/link` so that you don't need to worry about encoding the params manually. 

Closes: #13473
Closes: #14959
Closes: #16771
@Timer Timer added this to the iteration 8 milestone Sep 2, 2020
HitoriSensei pushed a commit to HitoriSensei/next.js that referenced this issue Sep 26, 2020
This corrects/makes sure interpolating dynamic route values for `href` works correctly. This provides an alternative approach to building the `href` value with `next/link` so that you don't need to worry about encoding the params manually. 

Closes: vercel#13473
Closes: vercel#14959
Closes: vercel#16771
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants