Gatsby 修改盖茨比内容博客文章的URL
我最近将“满足”与我的盖茨比网站整合在一起,一切都很完美。 我还有最后一个问题是关于我的博客文章页面的slug/url 目前,这些文件的URL类似于http://localhost:8000/blog/another-博客文章,但我希望他们是http://localhost:8000/blog/year/another-根据发布日期创建年份的博客文章 在我的gatsby-node.js文件中,我有以下内容Gatsby 修改盖茨比内容博客文章的URL,gatsby,contentful,Gatsby,Contentful,我最近将“满足”与我的盖茨比网站整合在一起,一切都很完美。 我还有最后一个问题是关于我的博客文章页面的slug/url 目前,这些文件的URL类似于http://localhost:8000/blog/another-博客文章,但我希望他们是http://localhost:8000/blog/year/another-根据发布日期创建年份的博客文章 在我的gatsby-node.js文件中,我有以下内容 if (posts.length > 0) { posts.forEach(
if (posts.length > 0) {
posts.forEach((post, index) => {
const previousPostId = post.previous?.id
const nextPostId = post.next?.id
const postYear = new Date(post.node.publishDate).getFullYear();
createPage({
path: `/blog/${post.node.slug}`,
component: blogPost,
context: {
id: post.id,
previousPostId,
nextPostId,
slug: post.node.slug
},
})
})
}
在那个例子中,post.node.slug
是“另一篇博客文章”
我已经创建了postwear
变量,该变量正确地给出了它们发布的年份。我想用
我不知道如何让URL实际包含年份
我确实尝试将
路径
、slug
和更改为=
以包含${postwear}
,但这导致了404。我设法解决了我自己的问题,不是100%确定这是否是最好的方法,但似乎效果很好。我将路径更新为
path:/blog/${postYear}/${post.node.slug}
,然后用
const postUrl = `/blog/${post.postYear}/${post.slug}`
<Link className="button" aria-label={`Read ${title}`} to={postUrl} itemProp="url">
const postrl=`/blog/${post.postyar}/${post.slug}`
经过多年的测试,我似乎正在做我所期望的事情