Implement getStaticPaths
First, let’s set up the files:
- Create a file called
[id].js
inside thepages/posts
directory. - Also, remove
first-post.js
inside thepages/posts
directory — we’ll no longer use this.
Then, open pages/posts/[id].js
in your editor and paste the following code. We’ll fill in ...
later:
Then, open lib/posts.js
and add the following getAllPostIds
function at the bottom. It will return the list of file names (excluding .md
) in the posts
directory:
Important: The returned list is not just an array of strings — it must be an array of objects that look like the comment above. Each object must have the params
key and contain an object with the id
key (because we’re using [id]
in the file name). Otherwise, getStaticPaths
will fail.
Finally, we'll import the getAllPostIds
function and use it inside getStaticPaths
. Open pages/posts/[id].js
and copy the following code above the exported Post
component:
paths
contains the array of known paths returned bygetAllPostIds()
, which include the params defined bypages/posts/[id].js
. Learn more in thepaths
key documentation- Ignore
fallback: false
for now — we’ll explain that later.
We’re almost done — but we still need to implement getStaticProps
. Let’s do that on the next page!