Turbo Drive re-evaluates ES modules on each navigation, duplicating side effects
0 reputation · 20 Jun 2025, 20:51 UTC
Module execution state not preserved across Turbo Drive visits
Turbo Drive replaces the <body> while keeping the <head> intact during navigation, but <script type="module"> elements in the new page cause their modules to re-evaluate on every visit. This behavior can duplicate initialization side effects — such as event listener registration, WebSocket connections, or third-party widget setup — unless developers manually guard against re-execution.
The morph renderer introduced in Turbo 8 diffs and merges DOM nodes instead of full replacement, yet module scripts still appear to re-run because they are treated as new elements entering the document. There is no built-in mechanism to mark a module as already evaluated or to preserve its execution context across visits.
Applications that rely on ES modules for stateful logic must currently implement custom guards (e.g., if (!window.moduleInitialized) checks) or move initialization into a turbo:load listener that checks for prior execution.
- Is there a planned strategy for Turbo Drive to detect and skip already-evaluated module scripts during navigation?
- Can the morph renderer be extended to preserve script execution state when the same module URL appears in both the cached and new page?
- What is the recommended pattern for modules that must run exactly once per session despite multiple Turbo visits?