fix: trigger navigation guards when keep-alive component is reactivated for different route#2604
fix: trigger navigation guards when keep-alive component is reactivated for different route#2604posva merged 3 commits intovuejs:mainfrom
Conversation
…ted for different route
✅ Deploy Preview for vue-router canceled.
|
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughRefactors guard registration to use a ComputedRef for the active route record so guards can be re-registered when a KeepAlive-wrapped component is reactivated for a different route. Adds tests for onBeforeRouteLeave and onBeforeRouteUpdate covering shared KeepAlive components and tracks activation/deactivation and guard invocation/registration. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Router as Router
participant KeepAlive as KeepAlive
participant Component as Component
participant GuardRegistry as Guard Registry
participant RouteRecord as Route Record
Client->>Router: navigate(to Route A)
Router->>KeepAlive: render/update <rgba(100,150,240,0.5)>
KeepAlive->>Component: mount / onActivated
Component->>GuardRegistry: registerGuard(computedActiveRecordRef)
GuardRegistry->>RouteRecord: resolve currentRecord from ref
GuardRegistry->>RouteRecord: attach guard to currentRecord
Client->>Router: navigate(to Route B, same kept component)
Router->>KeepAlive: update (component kept alive) <rgba(180,120,200,0.5)>
KeepAlive->>Component: onDeactivated -> onActivated
Component->>GuardRegistry: resolve currentRecord from ref
GuardRegistry->>RouteRecord: detect new record
GuardRegistry->>RouteRecord: remove guard from old, attach to new
Client->>Router: route params/query update
Router->>GuardRegistry: invoke guards for currentRecord
GuardRegistry-->>Router: guard result
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2604 +/- ##
==========================================
+ Coverage 91.27% 91.54% +0.27%
==========================================
Files 46 46
Lines 4181 4188 +7
Branches 1114 1116 +2
==========================================
+ Hits 3816 3834 +18
+ Misses 358 347 -11
Partials 7 7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@packages/router/__tests__/guards/onBeforeRouteLeave.spec.ts`:
- Around line 77-79: Update the Step 2 comment to accurately describe the
navigation: change the text that currently says navigating to `/a/123` to
reflect that the test actually calls `await router.push('/other')` (e.g., "Step
2: Navigate to /other - SharedComponent is deactivated (kept alive); Leave guard
is called when leaving /a"). This ensures the comment next to the `await
router.push('/other')` line matches the action being performed.
In `@packages/router/__tests__/guards/onBeforeRouteUpdate.spec.ts`:
- Around line 120-123: Update the inline comment that currently reads "// BUG:
The guard was registered with /a's record, not /b's record // So when /b
updates, the guard is not called" to clearly state this is the pre-fix behavior
and contrasts with the test assertion; reference the router.push('/b?page=2')
step and onBeforeRouteUpdate guard so the comment reads that before the fix the
guard was registered to /a (so updating /b did not trigger it), whereas the test
now asserts the guard is called after the fix.
|
@posva |
fixes #2601
Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.