harlan-zw / nuxt-seo

The complete SEO solution for Nuxt.
https://nuxtseo.com
949 stars 56 forks source link

Override robots settings from within a component #221

Open rudolfbyker opened 2 months ago

rudolfbyker commented 2 months ago

Clear and concise description of the problem

Sometimes we would like to modify the robots settings for a specific page or a specific URL, based on some logic that is only available while rendering the app. Use cases:

Suggested solution

Some kind of composable, e.g. useRobotsConfig(…) which will override both the meta tag and the x-robots-tag header for the current request. This should work in any component, not just the top-level page component, since the ErrorAlert might be shown a bit deeper in the component tree. If useRobotsConfig is never called, then use the routeRules as usual. If useRobotsConfig is called more than once, I'm not sure what should happen...

Alternative

I'm currently using this, but it means that my x-robots-tag header is in conflict with my <meta name="robots"> tag:

useHead({
  meta: [
    {
      // This only affects the robots meta tag. It does not affect the `x-robots-tag` header.
      name: "robots",
      content: "noindex, nofollow",
    },
  ],
});

Additional context

No response

harlan-zw commented 2 months ago

Thank you for the detailed write-up, this makes perfect sense. I'll try and implement when I have a chance.