Home > front end >  Can not add query params to the nextjs dynamic route
Can not add query params to the nextjs dynamic route

Time:02-17

I have file named. [slug].js.

what I am trying to do is that I want to add query parameter to this dynamic route, this is the code.

   await router.replace({
          pathname: `${router.pathname}`,
          query: { coupon },
        },
        undefined,
        { shallow: true });

This works fine every static page but on dynamic page it gives me this error:

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

any suggestions please?

CodePudding user response:

Use router.asPath to get current page address :

router.replace({
          pathname: `${router.asPath}`,
          query: { coupon },
        },
        undefined,
        { shallow: true });
  • Related