'NextJS - Add <Head> Data on query parameters

All my pages are working with getStaticProps. I want to make "If there's and q parameter on URL, then add 'noindex' to ". But as you can understand, that doesnt work on 'prerendered' pages.

But I have to find a way.

I need to add 'noindex' to all my 'shop?q=blabla' pages.

On _app.js I did like that:

function MyApp({ Component, pageProps }) {
  const router = useRouter()
  ...
  
  return (
    <Fragment>
      <Head>
      ...
        {router.query.q ? <meta name="robots" content="noindex"></meta> : '' } 
      ...

But not working on getStaticProps pages. Is there any advice about it?

How can I automatically add 'noindex' to all of the 'q' parametered pages?



Solution 1:[1]

You'd need to create two different pages. One for the basic non query routing and the other for dynamic routing and use getStaticPaths for the latter.

nextjs documentation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kanuos