【发布时间】:2022-12-16 08:57:10
【问题描述】:
我有一个带有博客的 next.js 网站。该博客有一个主页,其中包含从我的后端获取的所有博客文章的链接。这是页面的组件:
import { APIURL } from './../../constants'
import services from './../../components/services'
import React from 'react'
import Link from 'next/link'
class Blog extends React.Component {
constructor(props) {
super(props);
this.state = {
blog_posts: []
}
}
componentDidMount = async () => {
const response = await services.get_v2(APIURL.GET_BLOG_POSTS, {params: {}})
if (response) {
const blog_posts = []
for (const post in response) {
const blog_post = response[post]
const blog_image = blog_post['image'] ? blog_post['image'] : '/no-image.png'
const title_without_spaces = blog_post['title'].replace(new RegExp("\\ ","g"),'-')
blog_posts.push(
<Link key={'blog-post-' + post}
href={{
pathname: '/blog/[title]',
query: { title: title_without_spaces },
}}>
<a className="blog-card">
<div className="blog-title">{blog_post['title']}</div>
<div className="blog-created">{blog_post['created_at']}</div>
</a>
</Link>
)
}
this.setState({blog_posts: blog_posts})
}
}
render () {
return (
<div className="home">
<div className="home-container">
<h1 className='blog-index-title'>
Blog
</h1>
<div className="blog-index">
{this.state.blog_posts}
</div>
</div>
</div>
)
}
}
export default Blog
我希望每个博客的个人博文都能被谷歌抓取和索引,因为我使用的是 next.js 链接组件。然而事实并非如此。这个博客主页也得到了正确的索引,所以我不确定我现在做错了什么。我需要做什么才能让谷歌抓取这些单独的博客文章链接并将其编入索引?有什么我可以添加到站点地图以索引博客中的所有页面的东西吗?例如:
<url>
<loc>https://www.example.io/blog/*</loc>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
【问题讨论】:
-
爬虫看不到链接,因为您只是在客户端呈现它们(
componentDidMount仅在浏览器上运行)。改为使用getStaticProps/getServerSideProps获取服务器上的数据。参见nextjs.org/docs/basic-features/pages#pre-rendering。
标签: reactjs next.js google-search-console