0

I've been following the tutorial on the site to build out a small application. I am stuck with a catch-all route or a nested route based on the subdirectory structure.

I have all my files as markdown in the ./posts directory and in ./pages directory I would like to have subdirectories (categories) that some posts live in.

pages/content/category-1/post1
pages/content/category-2/post2
etc

My site is only able to serve content at pages/content/[id].js, if I put [...id].js as a way to catch-all routes, I get this error:

Error: A required parameter (id) was not provided as an array in getStaticPaths for /category/[...id]

If I place [id].js in every subdirectory ./pages/content/category-1/[id].js it works but that is pretty inefficient if I have a ton of category directories.

I am following the tutorial and my [...id].js looks like this:

import Layout from '../../components/layout'
import { getAllPostIds, getPostData } from '../../lib/posts'
import utilStyles from '../../styles/utils.module.css'
import Head from 'next/head'

export default function Post({ postData }) {
  return (
    <Layout>
      <Head>
        <title>{postData.title}</title>
      </Head>
      <article>
        <h1 className={utilStyles.headingXl}>{postData.title}</h1>
        <div className={utilStyles.lightText}>
        </div>
        <div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
      </article>
    </Layout>
  )
}

export async function getStaticPaths() {
  // Return a list of possible value for id
  const paths = getAllPostIds()
  return {
    paths,
    fallback: false
  }  
}

export async function getStaticProps({ params }) {
  // Fetch necessary data for the blog post using params.id
  const postData = await getPostData(params.id)
  return {
    props: {
      postData
    }
  }
}
3
  • Did you try pages/content/[categoryId]/[id].js. Maybe here you can find an answer: stackoverflow.com/questions/57648690/… Commented Oct 1, 2020 at 23:00
  • Thanks, very similar. I made a directory [categoryId] and place [id].js inside but now when I visit /content/category-name/post I get this error "Error: A required parameter (categoryId) was not provided as a string in getStaticPaths for /content/[categoryId]/[id]" Commented Oct 1, 2020 at 23:48
  • categoryId should be a string as suggested by the error, for some reason it is not. You need to convert it to string. Commented Oct 2, 2020 at 12:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.