Next.js Discord

Discord Forum

How to prerender as static HTML a /page using next 14

Unanswered
Goldstripe sardinella posted this in #help-forum
Open in Discord
Goldstripe sardinellaOP
Hi,

I would like to generate my page /flights at build time but its being server-rendered on demand at the moment.

Here is my code :

import FlightListCard from '@/components/FlightListCard'
import { getDictionary } from '@/lib/dictionaries'
import PageWrapper from '@/components/page-wrapper'
import { getAllFlightsList } from '@/app/actions/flights'

export default async function Flights() {
    const allFlights = await getAllFlightsList()
    const dict = await getDictionary()

    return (
        <PageWrapper>
            <div className='container'>
                <ul className='grid md:grid-cols-2 xl:grid-cols-3 gap-4'>
                    {allFlights.map((flightList) => (
                        <li key={flightList.slug}>
                            <FlightListCard flightList={flightList} dict={dict.flightList} />
                        </li>
                    ))}
                </ul>
            </div>
        </PageWrapper>
    )
}


For my pages with /flights/[slug] I am using generateStaticParams but since /flights doesn't have any params since its only supposed to show the list of flights, I dont how to do it. :

export async function generateStaticParams() {
    const allFlightsList = await getAllFlightsList()
    return allFlightsList.map((flightList) => ({
        slug: flightList.slug,
    }))
}


Thanks !

6 Replies

@Goldstripe sardinella It is as simple as exporting const dynamic in that page/layout to make it 'auto' | 'force-dynamic' | 'error' | 'force-static'. For the full documentation and usage of it, check out https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic It is so helpful and I recommend you to check the others too
How do you know it's dynamic? Does it show as so in the build logs?
Goldstripe sardinellaOP
Yes !
Yeah export const dynamic = "error" to see why it’s being dynamically rendered
Goldstripe sardinellaOP
Wonderful thanks! Couldn't find this in documentation
Goldstripe sardinellaOP
thanks @useless @joulev