Does a Turbo Frame with src refetch after back-forward cache restoration?
0 reputation · 14 Mar 2023, 11:03 UTC
0 reputation · 14 Mar 2023, 11:03 UTC
A Turbo Frame declared with a src attribute normally defers its GET request until the frame is connected, then replaces its body with the matching <turbo-frame> from the response. When a user leaves and returns through the browser’s back-forward cache, Turbo Drive may restore a page snapshot instead of reloading the document.
It is unclear whether a previously lazy-loaded frame reissues its src request during that restoration, or whether Turbo treats the existing frame body as final. The behavior may depend on the installed Turbo major version, because frame-load and history event sequencing differs between Turbo 7 and Turbo 8.
Does Turbo re-fetch a src frame when the page is restored from the back-forward cache? Which events reliably indicate that a lazy frame has reloaded after pageshow or popstate? Is there a version-specific contract between Turbo 7 and Turbo 8 for this behavior?
36775 reputation · 14 Mar 2023, 16:03 UTC
No. Turbo does not automatically re-issue a src frame GET when the document is restored from the browser’s back-forward cache. A previously loaded frame keeps its restored DOM content, and Turbo treats that existing frame body as final for the restored snapshot.
When the browser places a page in back-forward cache, it freezes the whole document, including Turbo Frame elements and their loaded inner HTML. On restoration, the browser reinstates that frozen snapshot instead of re-running the frame’s initial connection logic. Because a src fetch is tied to frame connection on a fresh load—not to pageshow restoration—the frame does not re-request its URL unless your code forces it.
pageshow with event.persisted === true is the standard browser signal that the page was restored from back-forward cache rather than freshly loaded.turbo:load and turbo:frame-load fire on initial navigation and frame connection, but their behavior on back-forward cache restoration is version-sensitive and is not a guaranteed re-fetch signal.popstate alone is not reliable for detecting back-forward cache restoration; use pageshow and check event.persisted.package.json or window.Turbo in DevTools.<turbo-frame src=\"...\">, allow the frame to load, navigate away, then press Back.src appears after restoration.pageshow, log event.persisted, and compare it against any turbo:frame-load firing on the restored frame.window.addEventListener('pageshow', (event) => {
console.log('pageshow persisted:', event.persisted);
});
If fresh content is required after restoration, manually reset the frame—for example, by removing and re-appending it or toggling src—inside the pageshow handler when event.persisted is true. The exact reset approach that works reliably depends on your Turbo major version, so verify it against your installed version before shipping.
This assumes Turbo Drive is enabled and the frame was connected before the user navigated away. The exact turbo:frame-load sequencing on back-forward cache restoration in Turbo 7 versus Turbo 8 is not confirmed from general knowledge and should be treated as requiring review.
Are you using Turbo 7 or Turbo 8? The answer changes which event sequence and frame-reset approach to recommend.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.