【问题标题】:"Invalid URL" error for markdown file using Nuxt content module使用 Nuxt 内容模块的降价文件的“无效 URL”错误
【发布时间】:2020-11-30 00:54:56
【问题描述】:

我正在使用内置的content 模块在 Nuxt 站点上工作,并且在本地运行,一切正常。但是,当我尝试通过运行nuxt generate 来构建站点时,我在第一个标记为Invalid URL 的降价文件上收到了一个致命错误。我的markdown文件在/content/posts目录下,这个名字的结构是my-post-markdown-file,命名为my-post-markdown-file.md。我已经从目录中删除了问题文件,以查看它是否只是那个特定文件,但下一个文件的过程也会出错。我正在关注 Debbie O'Brien 的 this post 使用内容模块,所以看起来我已经正确创建了东西。我还有什么遗漏的吗?

更新:我将范围缩小到站点地图生成。在this post 之后,我定义了以下插件来生成我的路线:

export default async () => {
  const { $content } = require("@nuxt/content");
  const files = await $content({ deep: true }).only(["path"]).fetch();

  return files.map((file) => (file.path === "/index" ? "/" : file.path));
};

然后在我的nuxt.config.js 中,我将站点地图定义为

sitemap: {
    path: '/sitemap.xml',
    hostname: process.env.BASE_URL,
    gzip: true,
    routes() {
      return getRoutes();
    }
  }

我的内容目录结构是

content
   |  \
   |   posts
   |      \
   |       my-post-markdown-file1.md
   |       my-post-markdown-file2.md
   |       my-post-markdown-file3.md  
  about.md

当我运行 npm generate 时,它会以这个错误结束

 TypeError [ERR_INVALID_URL]: Invalid URL: about

【问题讨论】:

    标签: javascript vue.js markdown nuxt.js


    【解决方案1】:

    使用nuxt.config.js 中的此配置,我能够使用内容模块为我的 Nuxt 站点生成站点地图:

    sitemap: {
        hostname: 'https://my-website-url.com',
        gzip: true,
        routes: async () => {
          let routes = []
          const { $content } = require('@nuxt/content')
          let posts = await $content('posts').fetch()
          for (const post of posts) {
            routes.push(`blog/${post.slug}`)
          }
          return routes
        },
      },
    

    我使用本地构建以及部署到 Netlify 对此进行了测试。

    我看到的一个可能不同的配置是您返回的路由可能不是有效的端点。路由的返回应该是一组有效的相对 URL。您是否将您的博客文章托管为“https://url.com/{post}”?

    以上代码基于Nuxt Content integrations page

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 2021-04-30
      • 2017-12-18
      • 2022-10-16
      • 2018-11-27
      • 2019-06-29
      • 2022-01-13
      • 2014-11-07
      相关资源
      最近更新 更多