#export const dynamic

1 messages · Page 1 of 1 (latest)

mortal yacht
#

Will putting export const dynamic = "force-dynamic"; in a server component make that component dynamic only or do i need to put it in the page which is calling the component

untold glenBOT
#

Hi @mortal yacht. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The export const dynamic = 'force-dynamic' statement is used in the context of a layout or page file, not a component. When you use this statement in a layout or page file, it forces dynamic rendering and uncached data fetching of that layout or page by disabling all caching of fetch requests and always revalidating. This means that the layout or page will be rendered and data will be fetched on every incoming request to the server.

If you want to dynamically import a component, you can use the next/dynamic function. However, it's important to note that if you dynamically import a Server Component, only the Client Components that are children of the Server Component will be lazy-loaded - not the Server Component itself.

Here's an example of how to dynamically import a component:

import dynamic from 'next/dynamic'
 
// Server Component:
const ServerComponent = dynamic(() => import('../components/ServerComponent'))
 
export default function ServerComponentExample() {
 return (
 <div>
 <ServerComponent />
 </div>
 )
}

In this example, ServerComponent is imported dynamically, which means it will be lazy-loaded when it's needed.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: