NextJs getStaticPaths not rendering the appropiate page, shows 404 error

2.8k views Asked by At

Okay this may be a very stupid question but I have never used 'getStaticPaths' before but for this project I really need too!

I have used the code of NextJs's documentation on 'getStaticPaths' and while the code compiles correctly when I head to what should be a route 'http://localhost:3000/1' it returns 404 not found? My custom API is built using Strapi. When using Postman it returns the GET request with a json response as below.

Why is this not working? I am extremely confused and some help would be extremely appreciated!!

Also is their a way to change the route to '/Math' instead of '/1'?

Again thanks in advance!


        "id": 1,
        "name": "Math",
        "published_at": "2021-10-08T04:32:51.871Z",
        "created_at": "2021-10-08T04:32:50.777Z",
        "updated_at": "2021-10-08T04:32:51.883Z",
        "papers": [
            {
                "id": 1,
                "name": "Maths Standard 2020 Past Papers 1",
                "description": "# 2020 Maths Standard Past Papers 1\n\nOriginal Source: [Source](https://educationstandards.nsw.edu.au/wps/portal/nesa/resource-finder/hsc-exam-papers/2020/mathematics-standard-2020-hsc-exam-pack)",
                "pdf_gcs_link": "https://storage.cloud.google.com/matsites/2020-hsc-mathematics-standard-1.pdf",
                "published_at": "2021-10-08T04:39:01.111Z",
                "created_at": "2021-10-08T04:38:49.013Z",
                "updated_at": "2021-10-08T05:21:32.293Z",
                "pdf": [
                    {
                        "id": 1,
                        "name": "2020-hsc-mathematics-standard-1(1).pdf",
                        "alternativeText": "",
                        "caption": "",
                        "width": null,
                        "height": null,
                        "formats": null,
                        "hash": "2020_hsc_mathematics_standard_1_1_1e764b3f71",
                        "ext": ".pdf",
                        "mime": "application/pdf",
                        "size": 612.42,
                        "url": "/uploads/2020_hsc_mathematics_standard_1_1_1e764b3f71.pdf",
                        "previewUrl": null,
                        "provider": "local",
                        "provider_metadata": null,
                        "created_at": "2021-10-08T04:37:35.185Z",
                        "updated_at": "2021-10-08T04:37:35.192Z"
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "name": "English",
        "published_at": "2021-10-08T04:32:59.966Z",
        "created_at": "2021-10-08T04:32:58.804Z",
        "updated_at": "2021-10-08T04:32:59.974Z",
        "papers": []
    }
]
// pages/posts/[id].js

function Category({ categories }) {
    <>
    <h1>{categories.name}</h1>
    </>
  }
  
  export async function getStaticPaths() {
    const res = await fetch('http://localhost:1337/Categories')
    console.log(res);
    const categories = await res.json()
  
    const paths = categories.map((category) => ({
      params: { id: category.id },
    }))

    return { paths, fallback: false }
  }
  

  export async function getStaticProps({ params }) {
    // params contains the post `id`.
    // If the route is like /posts/1, then params.id is 1
    const res = await fetch(`http://localhost:1337/Categories${params.id}`)
    const categories = await res.json()
  
    // Pass post data to the page via props
    return { props: { categories } }
  }
  
  export default Category
  

// categories response
Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'http://localhost:1337/Categories',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}
1

There are 1 answers

5
Ishan Bassi On

There is a typo in fetch call in getStaticProps , there should be a forward slash after Categories :

const res = await fetch(`http://localhost:1337/Categories/${params.id}`)

You can change the route by replacing category.id with category.name in getStaticPaths:

params: { id: category.name},

But then you have to modify your fetch call in getStaticProps and the api endpoint. A better approach would be to use next.js rewrites