#Force dynamic on page or layout

1 messages · Page 1 of 1 (latest)

winged cliff
#

If I do force dynamic on a page, does the layout holding it not be cached?

calm grailBOT
#

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

#

When you set force-dynamic on a page, it doesn't automatically make the parent layout dynamic. The layout will still follow its own caching behavior based on how it's configured.

According to the NextJS documentation, route segment configuration options like dynamic: 'force-dynamic' apply specifically to the segment where they're defined. This means:

  1. If you set force-dynamic on a page, only that page will be dynamically rendered on each request
  2. The parent layout will maintain its own caching behavior (static by default)

This can sometimes lead to unexpected behavior where your dynamic page is getting fresh data on each request, but the layout containing it is still using cached data.

There's an interesting issue related to this behavior described in the GitHub repository. In one case, a user found that when navigating between static and dynamic segments, the layout component from the static segment would receive updated data when refreshing the dynamic segment, despite being marked as force-static GitHub Issue #46715.

If you need both the page and layout to be dynamic, you should set force-dynamic on both the page and the layout separately.

Keep in mind that with browser back/forward navigation, even with force-dynamic, NextJS has special caching behavior to prevent layout shift and preserve scroll position, as mentioned in GitHub Issue #75697.

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