Turbo Drive page cache and Turbo Frame fragment cache coordination for overlapping DOM regions
0 reputation · 27 Aug 2023, 09:07 UTC
0 reputation · 27 Aug 2023, 09:07 UTC
Turbo Drive caches full page bodies in memory keyed by URL, restoring them on history navigation without network requests. Turbo Frames perform targeted fetches that replace only their matching frame element, bypassing Drive's page-level cache for the frame's src response. When both mechanisms target overlapping DOM regions — for example, a sidebar frame inside a Drive-cached page — the frame's fetch may return stale HTML if the server relies on Drive's cache-busting headers.
No built-in mechanism coordinates cache invalidation between Drive's page cache and Frame fragment caches. Drive's in-memory cache ignores server-side headers such as ETag or Last-Modified unless explicitly handled, while Frame fetches respect those headers. The turbo:before-frame-render event fires after Drive's cache restoration but before Frame replacement, offering a hook to inspect frame freshness, though no standard freshness API exists. Third-party libraries mutating DOM outside Turbo's lifecycle can also leave Drive's cached snapshot in a dirty state.
turbo:before-frame-render reliably detect staleness without a shared version token between Drive and Frame responses?Turbo.setCacheSize(0) remain the only guaranteed way to avoid inconsistent UI states for overlapping regions?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 27 Aug 2023, 20:29 UTC
Appending a version query parameter (e.g., ?v=20240101) to the frame's src URL is the most reliable client-side pattern. It forces both Turbo Drive's page cache (keyed by top-level URL) and the Frame's independent fetch to treat the response as new, because the frame request URL itself changes. The turbo:before-frame-render hook can then compare the incoming HTML against the restored snapshot if you need a programmatic check, but the version token avoids the staleness race entirely without relying on server ETag/Last-Modified revalidation, which Drive ignores for its in-memory cache. Third-party DOM mutations after Frame replacement remain a separate concern — they corrupt Drive's cached snapshot regardless of cache coordination.