简体   繁体   English

Nuxt3 Robots.txt - @nuxtjs/robots 不生成 robots.txt 文件

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

I'm building a Nuxt 3 project.我正在构建一个 Nuxt 3 项目。 I need my build to generate a robots.txt file, just like this package states it does -> https://github.com/nuxt-community/robots-module我需要我的构建来生成一个 robots.txt 文件,就像这个 package 声明的那样 -> 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.运行“nuxt build”和/或“nuxt generate”后,robots.txt 没有出现在 output 或公共文件夹中,如我所料。

I'm definitely missing something and likely being an idiot here.. Does anyone know what I'm missing?我肯定错过了一些东西并且可能在这里是个白痴..有谁知道我错过了什么? Here's my code:这是我的代码:

package.json package.json

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

nuxt.config.ts 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: "",
  },
}

I just successfully installed this module on Nuxt 3, and I think there are a couple of things to note here.我刚刚在 Nuxt 3 上成功安装了这个模块,我认为这里有几点需要注意。

The first is that I've never managed to get the module options for any module to work in Nuxt 3 the way you have shown (top-level options, according to the module docs):首先是我从来没有设法让任何模块的模块选项以您显示的方式在 Nuxt 3 中工作(顶级选项,根据模块文档):

  modules: [
    ...
    "@nuxtjs/robots",
  ],
  robots: {
    UserAgent: "*",
    Disallow: "",
  }

Have you tried the other options?您是否尝试过其他选项? You can also try using the Robots Config file, or pass the options when declaring the module (from the README.md for the repo):您还可以尝试使用Robots Config文件,或在声明模块时传递选项(来自 repo 的 README.md):

export default {
  modules: [
    // Simple usage
    '@nuxtjs/robots',

    // With options
    ['@nuxtjs/robots', { /* module options */ }]
  ]
}

The other thing is that I also do not see a generated robots.txt file anywhere after running the build or dev, but if I go to '/robots.txt' in the dev or build preview, I can see the output working as intended.另一件事是,在运行构建或开发后,我也没有在任何地方看到生成的 robots.txt 文件,但是如果我在开发或构建预览中将 go 转到“/robots.txt”,我可以看到 output 按预期工作. Have you tried visiting the path?您是否尝试过访问该路径?

It looks like something the server generates when the route is visited rather than a static file generated on build by default.它看起来像是访问路由时服务器生成的内容,而不是默认情况下在构建时生成的 static 文件。 I think you can change that in the options, but it's not something I need so I haven't dug into it.我想你可以在选项中改变它,但这不是我需要的,所以我没有深入研究它。

Hopefully, that helps somewhat!希望这能有所帮助!

You are using an incompatible version of nuxt robots for your project.您正在为您的项目使用不兼容版本的 nuxt 机器人。 Ensure you are using version ^3.0.0.确保您使用的是版本 ^3.0.0。 Any versions below this are not compatible with nuxt 3.低于此版本的任何版本都与 nuxt 3 不兼容。

npm install @nuxtjs/robots@3.0.0  

Then ensure your nuxt.config.ts looks similar to below.然后确保您的 nuxt.config.ts 看起来类似于下面。 As John Overstreet mentioned above the simple method appears flawed.正如 John Overstreet 上面提到的,这个简单的方法似乎有缺陷。

I created a config folder in the root directory of my project and placed my robots.txt file within it:我在项目的根目录中创建了一个配置文件夹,并将我的 robots.txt 文件放在其中:

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

You will also need to delete the robots.txt file from your public folder您还需要从公共文件夹中删除 robots.txt 文件

Build/Generate your project and go to '/robots.txt'构建/生成您的项目和 go 到“/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:)在我和约翰的帮助下,我希望这有助于解决您的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM