我正在使用内置的内容模块开发Nuxt网站,并在本地运行,一切正常。然而,当我试图通过运行nuxt generate
来构建站点时,我在第一个markdown文件上遇到了一个致命错误,该文件显示Invalid URL
。我的降价文件在/content/posts
目录中,这个文件的名称是结构化的my-post-markdown-file
,命名为my-post-markdown-file.md
。我已经从目录中删除了问题文件,看看它是否只是那个特定的文件,但下一个文件的处理也会出错。我关注Debbie O'Brien的这篇文章,因为我使用了内容模块,所以看起来我已经正确地创建了东西。我还缺少什么吗?
更新:我将其缩小到站点地图生成。在这篇文章之后,我定义了以下插件来生成我的路线:
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
在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集成页面。