首页 > 解决方案 > 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 enter image description here

After the build has been created, the page folder contains the following files:

pages/
├── index.js
├── courses
│   ├── [course].js

标签: javascriptseonext.js

解决方案


推荐阅读