[Cache Components] Is Next.js's bfCache a back/forward cache? #85857
Replies: 2 comments 4 replies
-
|
Yeah, you’re right — what Next.js calls “bfCache” isn’t the same as the browser’s actual back/forward cache. Browser bfCache is tied to history entries, so when you go back/forward, it restores the exact page state for that specific entry. That’s why your MPA example resets and replays state depending on navigation. What Next.js is doing with That’s why in your SPA example:
So yeah, it’s closer to a component-level cache keyed by route, not a true history-based cache. I don’t think it’s meant to replicate browser bfCache exactly — more like a performance optimization to avoid re-rendering and preserve state during SPA navigation. Not sure if there are plans to align it with real bfCache behavior, but that would probably require deeper integration with the browser history API rather than just React caching. |
Beta Was this translation helpful? Give feedback.
-
|
Opting out is now available. #93633 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The docs says:
Internally, this feature seems to be called bfCache. However, I believe the cache behavior differs from the browser's bfCache.
Here is the demo repository:
https://github.com/koichik/bfcache-demo
pnpm dev(orpnpm buildandpnpm start).http://localhost:3000/.Browser's bfCache
This is the back/forward cache behavior.
Next.js's bfCache
This behavior differs from the browser's bfCache.
The bfCache should be associated with history entries. In other words, the cache key should be the position of the entry in the history (e.g.
window.navigation.currentEntry.key). However, Next.js's bfCache seems to just use the URL as a key. Thus, I don't think this can be called bfCache.Do you have a plan to implement behavior similar to the browser's (i.e. true) bfCache?
Beta Was this translation helpful? Give feedback.
All reactions