Next.js Discord

Discord Forum

how to make a route as dynamic not static?

Unanswered
Gazami crab posted this in #help-forum
Open in Discord
Gazami crabOP
hello guys I have a path called "/dashboard/domestic-packages" where I am fetching data mongo db database using mongoose, so now when I build the app it gets build as Static page so issue is when I update anything on db the updated data dont show at that static page, I have to rebuild the entire app or purge the cache from vercel. I want to make the static page as Dynamic how should I do that. below is my page code of dashboard/domestic-package.

import DeleteDialogue from "@/app/(admin)/_components/DeleteDialogue"
import CreatePackage from "@/components/forms/CreatePackage"
import { Button } from "@/components/ui/button"
import { connectToMongoDB } from "@/lib/db"
import DestinationPackage from "@/lib/schema/PackageSchema"
import { Eye, Plus, Trash } from "lucide-react"

connectToMongoDB()


export default async function Page({ params }: { params: Promise<{ slug: string }> }) {

    const slug = (await params).slug
    const decodedSlug = decodeURIComponent(slug)
    const data = await DestinationPackage.find({ destination: decodedSlug })




    return (
        <div>
            {/* data */}
        </div>
    )
}

13 Replies

Sloth bear
@Gazami crab are you sure that you want to extract slug param inside /dashboard/domestic-package/page.tsx?
@Gazami crab yes sir
Sloth bear
slug will be always undefined, isn't it? I don't think its what you want, maybe this part doesn't care if you put undefined value
const decodedSlug = decodeURIComponent(slug)
const data = await DestinationPackage.find({ destination: decodedSlug })



you created
/dashboard/domestic-package/[slug]/page.tsx

where you can extract that param and that route is a dynamic route
@Sloth bear slug will be always undefined, isn't it? I don't think its what you want, maybe this part doesn't care if you put undefined value const decodedSlug = decodeURIComponent(slug) const data = await DestinationPackage.find({ destination: decodedSlug }) you created /dashboard/domestic-package/[slug]/page.tsx where you can extract that param and that route is a dynamic route
Gazami crabOP
no no sir that route is working fine and even every route is working fine. it just that (/dashboard/domestic-package) is being build as static dont know for what, and the code I put above is the the code of /dashboard/domestic-package/page.tsx path only. which fetch data
and it does fetch data, but its being static when i try to builld
what I want the the route /dashboard/domestic-package should be dynamic and not static as it shows in the build logs
@Gazami crab what I want the the route /dashboard/domestic-package should be dynamic and not static as it shows in the build logs
Sloth bear
sorry, I don't understand why you're trying to access slug param there and how it can be possible
Gazami crabOP
Oh sorry sir
I pasted the wrong code
There is no slug
export default async function DomesticPackagePage() {

    const data = await Destination.find({ type: 'Domestic' }).select('destination destinationImage tag description _id').lean() as any
    const structureData = data.map((destination: any) => {
        return {
            destination: destination.destination,
            destinationImage: destination.destinationImage,
            description: destination.description,
            tag: destination.tag,
            _id: destination._id.toString()
        }
    })

return (

Data

)
}

Here is the page code sir
Sloth bear
export const dynamic = "force-dynamic";


just add this line to the page.tsx file, doesn't it work for you?
you mentioned this option in your original post
@Sloth bear export const dynamic = "force-dynamic"; just add this line to the page.tsx file, doesn't it work for you? you mentioned this option in your original post
Gazami crabOP
sir sir love you. I did add that but in wrong page and file. thank you so much