Next.js Discord

Discord Forum

How can I have a "default" route as well as a dynamic route show the same data

Unanswered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
TBH, the title wording it probably not the best... But basically I want a way to have /college-football/rankings/[division] to show the "latest" data we have while also allowing users to go to /college-football/rankings/[division]/[year]/[week]. Does this mean I need both a page.tsx in the [division] directory and the [week] directory and duplicate the code? I guess I could use ?year=2024&week=0 but I try to avoid searchParams because I ran into issues in the past with generateMetadata and it not being aware if the searchParams.

Here is the scenario:

It's currently week 8 of the 2024 college football season and the user goes to /college-football/rankings/fcs, they should be able to see the rankings for that current week. However, lets say now they want to see the previous week's rankings so they select "Week 7" from the dropdown. They should be taken to /college-football/rankings/fcs/2024/7 or I guess if generateMetadata can handle searchParams (i.e. they can be used in the metadata returned), then I guess ?year=2024&week=7.

4 Replies

Sun bear
Yes you are right you have to create page.tsx in both folders. But you can create a component like

<Rankings week={week} year={year} />

and week and year can be optional so that if empty you show the latest results. So i would say you dont have any repetitive code
But just for my feeling I would go with searchParams in this case. It is just filtering of results. I dont see the need to create different metadata here. But if you want to keep it separate I would go with 2 page.tsx with one component like mentioned above
Nile CrocodileOP
Gotcha, yea I was looking at replicating something like https://www.espn.com/college-football/rankings/_/poll/20/week/9/year/2023/seasontype/2 which the metadata does change. My guess is it's because it's not necessarily filtering but showing a completely different ranking.
Sun bear
If it makes sense you could of course do it. I think its also possible to do something like

results/[...params]

And then read it dynamically bit never did it so far