Dynamic route
Unanswered
Gazami crab posted this in #help-forum
Gazami crabOP
Hey! I'm used to use nextjs but this time I don't understand what's wrong. I use a template from tailwind UI (spotlight, if you want to know).
I want to create dynamic route, based on two parameters, [company] and [step].jsx. Here is my structure file:
src/
directory/
[company]
[step].jsx
page.jsx
Inside the directory page, there is a page. It's a database. Once users click on one company inside the databse it redirects to page /directory/[company]/[step].jsx
My databse is in Contentful. So I connected everything.
But now that I want to test my dynamic page/route, it doesn't work. And I don't nuderstand what's wrong
Here is my code, with hardcoded path ( I go to this page http://localhost:3000/directory/test-company/1), but I have a 404:
What's wrong?
I want to create dynamic route, based on two parameters, [company] and [step].jsx. Here is my structure file:
src/
directory/
[company]
[step].jsx
page.jsx
Inside the directory page, there is a page. It's a database. Once users click on one company inside the databse it redirects to page /directory/[company]/[step].jsx
My databse is in Contentful. So I connected everything.
But now that I want to test my dynamic page/route, it doesn't work. And I don't nuderstand what's wrong
Here is my code, with hardcoded path ( I go to this page http://localhost:3000/directory/test-company/1), but I have a 404:
import { fetchCompaniesWithSteps, fetchOnboardingStepData } from '@/lib/contentful/retrieveData';
export async function getStaticPaths() {
// Fetch all companies and their onboarding steps from Contentful
const companies = await fetchCompaniesWithSteps(); // Implement this based on your Contentful setup
return {
paths: [
{ params: { company: 'test-company', step: '1' } },
],
fallback: false,
};
}
export async function getStaticProps({ params }) {
const { company } = params;
// Fetch the specific onboarding step data for the given company and step number from Contentful
const onboardingStepData = await fetchOnboardingStepData(company); // Implement based on your Contentful setup
return {
props: {
onboardingStep: {
title: "Test Title",
description: "This is a test description.",
},
},
};
}
export default function OnboardingStep({ onboardingStep }) {
// Render your onboarding step using onboardingStep data
return (
<div>
{/* {console.log(onboardingStep)} */}
<h1>oui</h1>
<p>oui</p>
{/* Display the step image, etc. */}
</div>
);
}What's wrong?
10 Replies
Your directory structure must be like this:
src/
app/
[company]
[step]
page.jsx
page.jsxA page.jsx is must to make a page routable in app directory in nextjs
This directory structure will create two routes:
1) http://localhost:3000/[company]
2) http://localhost:3000/[company]/[step]
1) http://localhost:3000/[company]
2) http://localhost:3000/[company]/[step]
Remove the page.jsx inside company if you dont want the first path
Gazami crabOP
thanks a lot! Now I receive this erroor:
"getStaticProps" is not supported in app/
Do you know why?
"getStaticProps" is not supported in app/
Do you know why?
Gazami crabOP
Ok i'm so outdated, I understood that know getStaticProps is deprecated! Am I correct?
getStaticProps works in the pages router.
I seem to have overlooked that function. Instead of the "app" directory you will need to use "pages" directory.
Also please learn about the differences between the two and how to use routing and dynamic routes with those specific routers.
1) [App Router](https://nextjs.org/docs/app)
2) [Pages Router](https://nextjs.org/docs/pages)
I seem to have overlooked that function. Instead of the "app" directory you will need to use "pages" directory.
Also please learn about the differences between the two and how to use routing and dynamic routes with those specific routers.
1) [App Router](https://nextjs.org/docs/app)
2) [Pages Router](https://nextjs.org/docs/pages)
Gazami crabOP
Amazing thanks a lot, I wasn't aware of that!
Gazami crabOP
Should I also use generateStaticParams() instead of getStaticPath?
@Gazami crab Should I also use generateStaticParams() instead of getStaticPath?
1) If its not non-dynamic route, you can fetch directly in your component and as long as you arent using a dynamic function like for example cookies(), next will automatically fetch at build-time and create a static page.
2) If you want to build a couple dynamic route pages statically(like you are doing here), then you can simply use:
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#generating-static-params
2) If you want to build a couple dynamic route pages statically(like you are doing here), then you can simply use:
generateStaticParams()https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#generating-static-params