Next.js Discord

Discord Forum

Is it possible to generate a URL route with 2 variables at the same level?

Answered
jpaask posted this in #help-forum
Open in Discord
Doubt about route creation.

I want to create the following url:
/best-[category]-of-[monthName]

But I can't find how to do it.

I am not looking for this:
/best/[category]/of/[monthName]

I have thought about using a /[slug] and create a kind of "route-checker" that reads the slug and detecting composition it shows me one thing or another but I find it very unusable:box_cat_hide:
Answered by American Crow
Can't do it out of the box with with just folder structure.
As you mentioned you'd have to do a /[slug] and something like
const dynamicParams = false

export function generateStaticParams() {
  const categories = ['love','hate','crime']
  const monthNames = ['jan', 'feb', 'march']
 
  // some logic to combine them
  // ...
  const categoriesMonths = [
    { 
      category: 'love'
      month: 'jan'
    },
    ...
   ]
 
  return categoriesAndMonths.map(({category,month}) => ({
    slug: `best-${category}-${month}`,
  }))
}
View full answer

2 Replies

American Crow
Can't do it out of the box with with just folder structure.
As you mentioned you'd have to do a /[slug] and something like
const dynamicParams = false

export function generateStaticParams() {
  const categories = ['love','hate','crime']
  const monthNames = ['jan', 'feb', 'march']
 
  // some logic to combine them
  // ...
  const categoriesMonths = [
    { 
      category: 'love'
      month: 'jan'
    },
    ...
   ]
 
  return categoriesAndMonths.map(({category,month}) => ({
    slug: `best-${category}-${month}`,
  }))
}
Answer
Thanks @American Crow