[cache components]: Client component's state during route navigation #85502
-
|
In Next.js 16, when cacheComponents is enabled, the client component's state is preserved during route navigation. However, when cacheComponents is disabled, the state is lost. What causes this behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
|
When Cache Components is enabled, we also wrap Activity components around routes. I just merged the docs update w/ this behavior. Be aware that there's a known issue with Portals. Tracked upstream here: facebook/react#35000 |
Beta Was this translation helpful? Give feedback.
-
|
We're experiencing this Portal issue with shadcn/ui Sidebar (which uses Radix Dialog) in a route group layout scenario: Scenario:
Main UI Bug:
This happens because:
Current Workaround: useEffect(() => {
return () => {
// Wait 500ms for React's async cleanup attempt
setTimeout(() => {
// Manually remove leftover Portal DOM
const sidebarElements = document.querySelectorAll('[data-slot="sidebar"]');
sidebarElements.forEach(element => {
const portalContainer = findPortalContainer(element);
if (portalContainer?.parentNode && document.body.contains(portalContainer)) {
portalContainer.parentNode.removeChild(portalContainer);
}
});
}, 500);
};
}, []);Questions:
@icyJoseph Thanks for the clarification on Cache Components behavior! 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
Is there currently no way to opt out of this? Are there plans to add the option to opt out? |
Beta Was this translation helpful? Give feedback.
When Cache Components is enabled, we also wrap Activity components around routes. I just merged the docs update w/ this behavior. Be aware that there's a known issue with Portals. Tracked upstream here: facebook/react#35000