Search code examples
javascriptnuxt.jsrobots.txtnuxt3.js

Nuxt3 Robots.txt - @nuxtjs/robots not generating robots.txt file


I'm building a Nuxt 3 project. I need my build to generate a robots.txt file, just like this package states it does -> https://github.com/nuxt-community/robots-module

After running "nuxt build" and/or "nuxt generate", the robots.txt does not appear in the output or public folders as I'd expect.

I'm definitely missing something and likely being an idiot here.. Does anyone know what I'm missing? Here's my code:

package.json

  "dependencies": {
    ...
    "@nuxtjs/robots": "^2.5.0", 
    }

nuxt.config.ts

 target: "static",
  runtimeConfig: {
    NUXT_STORYBLOK_PRODUCTION_KEY: process.env.NUXT_STORYBLOK_PRODUCTION_KEY,
    public: {
      CDN: process.env.CDN,
      NUXT_STORYBLOK_PREVIEW_KEY: process.env.NUXT_STORYBLOK_PREVIEW_KEY,
      NUXT_DOMAIN_NAME: process.env.NUXT_DOMAIN_NAME,
    },
  },
  modules: [
    ...
    "@nuxtjs/robots",
  ],
  robots: {
    UserAgent: "*",
    Disallow: "",
  },
}

Solution

  • You are using an incompatible version of nuxt robots for your project. Ensure you are using version ^3.0.0. Any versions below this are not compatible with nuxt 3.

    npm install @nuxtjs/[email protected]  
    

    Then ensure your nuxt.config.ts looks similar to below. As John Overstreet mentioned above the simple method appears flawed.

    I created a config folder in the root directory of my project and placed my robots.txt file within it:

    export default {
      modules: [
        ['@nuxtjs/robots', { configPath: "~/config/robots.config" }]
      ]
    }
    

    You will also need to delete the robots.txt file from your public folder

    Build/Generate your project and go to '/robots.txt'

    The file is generated dynamically upon request of the above route.

    With mine and John's help above I hope this helps resolve your issues :)