keep-alive and beforeRouteLeave: pending request cancellation interoperability
0 reputation · 14 Sept 2021, 20:48 UTC
0 reputation · 14 Sept 2021, 20:48 UTC
Vue 3's keep-alive component and the beforeRouteLeave navigation guard each manage lifecycle events that influence pending async operations. When a route transition occurs, beforeRouteLeave can cancel in-flight requests, but keep-alive's cached state may rehydrate the component after navigation, potentially re-triggering the same async call. The reactivity system does not inherently distinguish between a legitimate retry and a duplicate write caused by lifecycle overlap. This creates an interoperability question: how to coordinate cancellation and caching so that a retry initiated after navigation does not produce duplicate network requests. Version considerations include Vue 3's composition API onBeforeUnmount behavior and axios interceptor flag persistence across keep-alive activated hooks.
How does keep-alive's activated hook interact with beforeRouteLeave's cancellation window when a retry is pending? Can a retry suppression flag set in an axios interceptor survive keep-alive unmount and remount without race conditions? What pattern ensures a single async write on component re-entry after navigation guard cancellation?
The beforeRouteLeave guard runs before the component instance is unmounted, while keep-alive's activated hook fires after the cached instance is remounted. If a request is still in flight when the guard cancels it, the subsequent activated call can re‑trigger the same request unless the cancellation state is persisted outside the component instance.
beforeRouteLeave executes before Vue destroys the component; any cleanup placed there (e.g., calling abort() on an AbortController) happens while the instance still exists.keep-alive preserves the existing instance; activated re‑hydrates it without re‑running beforeRouteEnter. Local refs or interceptor flags that live on the instance are lost when the instance is temporarily removed from the DOM.activated resets the flag and re‑issues the request.The duplication occurs because the cancellation signal (or suppression flag) is scoped to the component instance, which keep‑alive discards temporarily. When the instance is remounted, the flag is re‑initialized to its default state, allowing the pending request to be retried.
AbortController) or a suppression flag in a global store such as Pinia or a simple singleton module.beforeRouteLeave, call abort() on the shared controller and set the suppression flag to true.activated, before launching the request, check the shared controller’s signal.aborted flag (or the suppression flag). If abort is true or the flag is set, skip the request or wait for the abort to settle.false in the store so a future navigation can allow a new request.Add console logs in beforeRouteLeave, deactivated, and activated to confirm the order: leave → deactivated → (keep‑alive cache) → activated. Then verify that calling abort() on the shared controller prevents the request logged in activated. Finally, watch the store’s suppression flag to ensure it persists across the cycle and resets only after the request finishes.
If you are already using a global store for the cancellation state, confirm whether the store is re‑initialized on page reload; otherwise the pattern above is sufficient. If not, please specify whether you can introduce a Pinia/singleton store, as this determines if the shared controller approach is viable.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.