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

Unexpected behavior when a path parameter value matches the path parameter name #33703

Closed
klarstrup opened this issue Jan 26, 2022 · 6 comments
Closed
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@klarstrup
Copy link

klarstrup commented Jan 26, 2022

What version of Next.js are you using?

12.0.8-canary.6

What version of Node.js are you using?

whatever vercel.com is on

What browser are you using?

Chrome 97

What operating system are you using?

Windows

How are you deploying your application?

Vercel

Describe the Bug

For a URL like https://vercel.com/changelog/[item] to a page /changelog/[item].tsx, useRouter().query returns an empty object {}.

Expected Behavior

For a URL like https://vercel.com/changelog/[item] to a page /changelog/[item].tsx, one would expect useRouter().query to return { item: "[item]" }

To Reproduce

Request a path parameter's name as its value: https://vercel.com/changelog/[item]

@klarstrup klarstrup added the bug Issue was opened via the bug report template. label Jan 26, 2022
@klarstrup klarstrup changed the title Unexpected behavior when a path parameter value matches the path parameter Unexpected behavior when a path parameter value matches the path parameter name Jan 26, 2022
@klarstrup
Copy link
Author

https://vercel.com/support/articles/[slug] has also crashed on me

@balazsorban44 balazsorban44 added Navigation Related to Next.js linking (e.g., <Link>) and navigation. kind: bug and removed bug Issue was opened via the bug report template. labels Jan 26, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented Jan 26, 2022

Thanks, I could reproduce this locally with the following:

  1. create page pages/[slug].js:
import { useRouter } from "next/router"

export default function Page() {
  const router = useRouter()
  return <pre>{JSON.stringify(router.query, null, 2)}</pre>
}
  1. Visit http://localhost:3000/[slug]
  2. Observe error in development
    image

Just to investigate further, do you have a valid use case for this? 🤔

@klarstrup
Copy link
Author

Just to investigate further, do you have a valid use case for this? 🤔

Not in particular, but errors from this behavior popped up in my production logs for access to a similar URL (may have come from bad internal URL building)

I decided to raise this issue since it's a case of unfortunate interpolation/escaping similar to the one I addressed in #31523

@klarstrup
Copy link
Author

The main thing here is that it feels like a breach of API contract that there is a scenario where useRouter().query for a page /[param] will not have a string param property

I don't want to have to check for that in all of my dynamic routes :(

@balazsorban44
Copy link
Member

I actually found this is a duplicate of #23824, so I'm going to close this issue in favor of the one that was reported earlier. Thank you for the investigation anyway, I'll backlink to this issue as well.

timneutkens added a commit to timneutkens/next.js that referenced this issue Jan 28, 2022
Add tests for the cases described in vercel#33703 and vercel#23824 as well as a few more.
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

2 participants