13

I am getting this error in Next.js:

Error: The provided 'href' (/subject/[subject]) value is missing query values (subject) to be interpolated properly. Read more: https://err.sh/vercel/next.js/href-interpolation-failed`.

I have a dynamic page set up as /subject/[subject].tsx. Now in my navigation I have:

<Link href={'/subject/${subject}'} passHref><a>{subject}</a></Link>

It works fine when I access the page with different paths but when I am pressing on a button on the page it throws the error above which I imagine is because the component rerenders. If you go to the page in the error it says: Note: this error will only show when the next/link component is clicked not when only rendered.

I have tried to look for a solution and I tried doing:

<Link href={{pathname: '/subject/[subject]', query: {subject: subject}}}></Link>

but nothing changed. I read the docs and it seems that the as prop is only an optional decorator that is not used anymore so I fail to see how that can help me.

2
  • Could it be a syntax issue? <Link href={`/subject/${subject}`} passHref><a>{subject}</a></Link> works for me (string with back-ticks rather than single quotes). Commented Dec 27, 2020 at 21:00
  • It's not I used single quotes because of the code formatting on this site, in the code I do use backticks. Commented Dec 28, 2020 at 16:49

6 Answers 6

6

I got the same issue when trying to redirect user to locale. I did it in useEffect. After investigate I discovered that on first render router.query is empty, so it's missing required field id. I fix it by using router.isReady

export const useUserLanguageRoute = () => {
  const router = useRouter()

  useEffect(() => {
    const {
      locales = [],
      locale,
      asPath,
      defaultLocale,
      pathname,
      query,
      isReady // here it is
    } = router

    const browserLanguage = window.navigator.language.slice(0, 2)

    const shouldChangeLocale =
      isReady && // and here I use it
      locale !== browserLanguage
      && locale === defaultLocale
      && locales.includes(browserLanguage)

    if (shouldChangeLocale) {
      router.push(
        {
          pathname,
          query,
        },
        asPath,
        { locale: browserLanguage }
      )
    }
  }, [router])
}

Sign up to request clarification or add additional context in comments.

Comments

4

Another possible solution could be redirect using the router.push function:

const myRedirectFunction = function () {

    if (typeof window !== 'undefined') {
        router.push({
            pathname: router.pathname,
            query: {...router.query, myqueryparam: 'myvalue'},
        })
    }

}


return (
  <>
    <button onClick={() => {myRedirectFunction()}}> Continue </button>
  </>

)

It is important to include ...router.query because there is where the [dynamic] current value is included, so we need to keep it.

Reference: https://github.com/vercel/next.js/blob/master/errors/href-interpolation-failed.md

2 Comments

wow I had the same mysterious error (reference docs totally unhelpful). But the magic '...router.query` fix WFM as well. Still somewhat mystified as to why it's needed as I'm pushing to a different page which doesn't need (or want) the query-string params I have on the current page. Thanks though!
Seems a bug in nextjs AFAICT - tried latest version 10.0.9 and still has same issue
0

You better do a null || undefined check in prior.

3 Comments

<Link href={subject ? `/subject/${subject}` : "/"} key={subject} passHref> I tried this and it doesn't seem to help me. Is that what you meant? I am getting the subject data client side.
I also have a /book/[pid].tsx page and when I got to it and press any buttons it shows the same error: Error: The provided `href` (/book/[pid]) value is missing query values (pid) to be interpolated properly. Read more: https://err.sh/vercel/next.js/href-interpolation-failed
in case of the dynamic routing: ` <Link href={{ pathname: '/book/[pid]', query: {pid: somevalue }, }} > <a>Valid link</a> </Link> `
0

@Bentasy, Like you rightly pointed out, any re-render of the page is expecting you to pass in the dynamic route params again. The way I avoided page re-rendering with subsequent clicks on the page was to replace Link tag on the page with a label tag. I was using the Next Link tag to navigate between the tabs on this image which generated this same error. So I replaced the Link tags with label tags and the error was solved. The tabs on the page.

Comments

0

I was having the same problem, this is how I solved it.

While pushing something to the router, we need to give the old values ​​in it.

In that:

const router = useRouter()

router.push({
   ...router,
   pathname: '/your-pathname'
})

Comments

0

try add this function, it work for me:

export async function getServerSideProps(context) {
return {
props: {},
};
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.