Skip to content

Commit 2e8af8f

Browse files
committed
chore: remove navigation success and error listeners
1 parent 97e3796 commit 2e8af8f

File tree

1 file changed

+51
-68
lines changed
  • packages/router/src/navigation-api

1 file changed

+51
-68
lines changed

packages/router/src/navigation-api/index.ts

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
7979
)
8080

8181
let pendingLocation: RouteLocation | undefined
82-
let navigationInfo: NavigationInformation | undefined
8382
let lastSuccessfulLocation: RouteLocationNormalizedLoaded =
8483
START_LOCATION_NORMALIZED
8584

@@ -129,7 +128,8 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
129128

130129
async function resolveNavigationGuards(
131130
to: RouteLocationNormalized,
132-
from: RouteLocationNormalizedLoaded
131+
from: RouteLocationNormalizedLoaded,
132+
navigationInfo?: NavigationInformation
133133
): Promise<void> {
134134
const [leavingRecords, updatingRecords, enteringRecords] =
135135
extractChangingRecords(to, from)
@@ -595,31 +595,6 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
595595
async function handleNavigate(event: NavigateEvent) {
596596
if (!event.canIntercept) return
597597

598-
if (event.navigationType === 'traverse') {
599-
const fromIndex = window.navigation.currentEntry?.index ?? -1
600-
const toIndex = event.destination.index
601-
const delta = fromIndex === -1 ? 0 : toIndex - fromIndex
602-
603-
navigationInfo = {
604-
type: NavigationType.pop, // 'traverse' maps to 'pop' in vue-router's terminology.
605-
direction:
606-
delta > 0 ? NavigationDirection.forward : NavigationDirection.back,
607-
delta,
608-
}
609-
} else if (
610-
event.navigationType === 'push' ||
611-
event.navigationType === 'replace'
612-
) {
613-
navigationInfo = {
614-
type:
615-
event.navigationType === 'push'
616-
? NavigationType.push
617-
: NavigationType.pop,
618-
direction: NavigationDirection.unknown, // No specific direction for push/replace.
619-
delta: event.navigationType === 'push' ? 1 : 0,
620-
}
621-
}
622-
623598
event.intercept({
624599
async handler() {
625600
const destination = new URL(event.destination.url)
@@ -628,7 +603,53 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
628603
const to = resolve(pathWithSearchAndHash) as RouteLocationNormalized
629604
const from = currentRoute.value
630605
pendingLocation = to
631-
await resolveNavigationGuards(to, from)
606+
607+
let navigationInfo: NavigationInformation | undefined
608+
if (event.navigationType === 'traverse') {
609+
const fromIndex = window.navigation.currentEntry?.index ?? -1
610+
const toIndex = event.destination.index
611+
const delta = fromIndex === -1 ? 0 : toIndex - fromIndex
612+
613+
navigationInfo = {
614+
type: NavigationType.pop, // 'traverse' maps to 'pop' in vue-router's terminology.
615+
direction:
616+
delta > 0
617+
? NavigationDirection.forward
618+
: NavigationDirection.back,
619+
delta,
620+
}
621+
} else if (
622+
event.navigationType === 'push' ||
623+
event.navigationType === 'replace'
624+
) {
625+
navigationInfo = {
626+
type:
627+
event.navigationType === 'push'
628+
? NavigationType.push
629+
: NavigationType.pop,
630+
direction: NavigationDirection.unknown, // No specific direction for push/replace.
631+
delta: event.navigationType === 'push' ? 1 : 0,
632+
}
633+
}
634+
635+
try {
636+
await resolveNavigationGuards(to, from, navigationInfo)
637+
finalizeNavigation(to, from)
638+
} catch (error) {
639+
const failure = error as NavigationFailure
640+
finalizeNavigation(to, from, failure)
641+
642+
if (
643+
isNavigationFailure(failure, ErrorTypes.NAVIGATION_GUARD_REDIRECT)
644+
) {
645+
navigate((failure as NavigationRedirectError).to, { replace: true })
646+
} else if (
647+
!isNavigationFailure(failure, ErrorTypes.NAVIGATION_CANCELLED)
648+
) {
649+
triggerError(failure, to, from)
650+
}
651+
throw failure
652+
}
632653
},
633654
})
634655
}
@@ -649,7 +670,7 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
649670
const fromIndex = event.from.index
650671
const toIndex = window.navigation.currentEntry!.index
651672
const delta = toIndex - fromIndex
652-
navigationInfo = {
673+
const navigationInfo: NavigationInformation = {
653674
type: NavigationType.pop,
654675
direction:
655676
delta > 0 ? NavigationDirection.forward : NavigationDirection.back,
@@ -660,7 +681,7 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
660681

661682
try {
662683
// then browser has been done the navigation, we just run the guards
663-
await resolveNavigationGuards(to, from)
684+
await resolveNavigationGuards(to, from, navigationInfo)
664685
finalizeNavigation(to, from)
665686
} catch (error) {
666687
const failure = error as NavigationFailure
@@ -677,56 +698,18 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
677698
}
678699
}
679700

680-
function handleNavigateSuccess(event: Event) {
681-
navigationInfo = undefined
682-
if (pendingLocation) {
683-
finalizeNavigation(
684-
resolve(pendingLocation) as RouteLocationNormalized,
685-
currentRoute.value
686-
)
687-
pendingLocation = undefined
688-
}
689-
}
690-
691-
function handleNavigateError(event: ErrorEvent) {
692-
navigationInfo = undefined
693-
694-
const failure = event.error as NavigationFailure
695-
696-
if (pendingLocation) {
697-
const to = pendingLocation as RouteLocationNormalized
698-
const from = currentRoute.value
699-
pendingLocation = undefined
700-
701-
finalizeNavigation(to, from, failure)
702-
703-
if (isNavigationFailure(failure, ErrorTypes.NAVIGATION_GUARD_REDIRECT)) {
704-
navigate((failure as NavigationRedirectError).to, { replace: true })
705-
} else {
706-
triggerError(failure, to, from)
707-
}
708-
}
709-
}
710-
711701
window.navigation.addEventListener('navigate', handleNavigate)
712702
window.navigation.addEventListener(
713703
'currententrychange',
714704
handleCurrentEntryChange
715705
)
716-
window.navigation.addEventListener('navigatesuccess', handleNavigateSuccess)
717-
window.navigation.addEventListener('navigateerror', handleNavigateError)
718706

719707
function destroy() {
720708
window.navigation.removeEventListener('navigate', handleNavigate)
721709
window.navigation.removeEventListener(
722710
'currententrychange',
723711
handleCurrentEntryChange
724712
)
725-
window.navigation.removeEventListener(
726-
'navigatesuccess',
727-
handleNavigateSuccess
728-
)
729-
window.navigation.removeEventListener('navigateerror', handleNavigateError)
730713
}
731714

732715
const history: RouterHistory = {

0 commit comments

Comments
 (0)