javascript - Nextjs | Why getInitialProps is not generating one page per result (SEO)?
问题描述
I've built a project using Next.js. There are 2 pages on this website but when I run a sitemap generator it only crawls one, the index one. I think that if these crawlers don't detect other pages, the search engines wouldn't either.
Next.js version: 10.0.2
React version: 17.0.1
Here is the pages directory's structure:
pages/
├── index.js
├── courses
│ ├── [course].js
As you can see, I'm generating every course dynamically using getInitialPros
.
Curso.getInitialProps = async (ctx) =>{
const {query} = ctx;
const response = await fetch('https://www.example.com/data.json');
const coursesList = await response.json();
let courseIndex = coursesList.map(function (e) {return e.path;}).indexOf(query.course);
let courseObj = coursesList[courseIndex];
return({courseObj: courseObj})
}
And here is the component that renders the course that match the URL:
const Curso = ({courseObj })=> {
const router = useRouter();
return (
<h2> {courseObj.title} </h2>
...
}
The website works fine, I can access every course using the list of links (as I'm working with Next.js, I'm using <Link />
from 'next/link'
to navigate in the website) on the index page or directly from the navigation bar. I've tried to solve this using swr but I got the same result.
UPDATE
When I run build in the directory this is what I get
After the build has been created, the page folder contains the following files:
pages/
├── index.js
├── courses
│ ├── [course].js
解决方案
推荐阅读
- apache-kafka - 为什么重新创建具有相同 groupId 的消费者组后偏移量不重置为 0
- html - 如何为 HTML/CSS 使用“带有 BEM 的原子设计”方法?
- java - Drools 7 是否必须在单独的 JVM 中运行,即持有规则的 KIE 服务器?
- css - 实现 Bootstrap 5 多级下拉垂直导航栏的干净方法是什么?
- html - HTML5 中的表单“action”属性究竟是如何将数据发送到实时网站的后端的?
- go - 更新 opentelemetry prometheus 导出器中的标签
- mysql - 由于访问限制,LOAD DATA LOCAL INFILE 文件请求被拒绝
- rust - Rust:有没有办法使用 map 来缩短这个 if/else 代码?
- python - Think Python(如何像计算机科学家一样思考)——练习 8.4 Duckling 问题
- winapi - win32 listview内部构建滚动条所有者绘制