Skip to content

Commit

Permalink
refactor: change soft navigation concept to fallback failed
Browse files Browse the repository at this point in the history
  • Loading branch information
serkodev committed Apr 14, 2024
1 parent 91936f3 commit 38a3451
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/runtime/components/PlusParallelPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const props = defineProps<{
// Unique name of the parallel router
name: string
// Disable rendering during soft navigation
// Disable rendering if fallback fail
autoHide?: boolean
// Name of the router view to use
Expand All @@ -30,7 +30,7 @@ const route = computed(() => router.value?.currentRoute.value)

<template>
<RouterView
v-if="router && (!props.autoHide || !router.inSoftNavigation.value)"
v-if="router && (!props.autoHide || !router.isFallbackFailed.value)"
:name="routerViewName"
:route="route"
v-bind="$attrs"
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/parallel-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { type Ref, ref } from '#imports'

export interface ParallelRouter extends Router {
name?: string
inSoftNavigation: Ref<boolean>
isFallbackFailed: Ref<boolean>
hasPath: (path: string) => boolean
tryPush: (path: string, notFoundPath?: ParallelPageOptions['notFoundPath']) => ReturnType<Router['push']> | undefined
sync: (notFoundPath?: ParallelPageOptions['notFoundPath']) => ReturnType<Router['push']> | undefined
Expand Down Expand Up @@ -86,7 +86,7 @@ async function createParallelRouter(name: string, routes: RouteRecord[], router:
mode: 'sync',
defaultPath: '/~default',
notFoundPath: '/~not-found',
disableSoftNavigation: false,
disableFallback: false,
} satisfies ParallelPageOptions)

const parallelRouter = createRouter({
Expand All @@ -106,18 +106,18 @@ async function createParallelRouter(name: string, routes: RouteRecord[], router:
return parallelRouter.resolve(path)?.name !== ParallelRouteNotFoundSymbol
}

const inSoftNavigation = ref(false)
const isFallbackFailed = ref(false)

// try to push the path, if not found, try to push the not found path
function tryPush(path: string, notFoundPath: ParallelPageOptions['notFoundPath'] = options.notFoundPath) {
function pushWithFallback(path: string, ...fallbacks: (string | undefined)[]) {
for (const _path of [path, ...fallbacks])
if (_path !== undefined && (options.disableSoftNavigation || hasPath(_path))) {
if (_path !== undefined && (options.disableFallback || hasPath(_path))) {
return parallelRouter.push(_path).then(() => {
inSoftNavigation.value = false
isFallbackFailed.value = false
})
}
inSoftNavigation.value = true
isFallbackFailed.value = true
}
return pushWithFallback(path, notFoundPath || undefined)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ async function createParallelRouter(name: string, routes: RouteRecord[], router:
return {
...parallelRouter,
name,
inSoftNavigation,
isFallbackFailed,
hasPath,
tryPush,
sync,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ParallelPageOptions {
notFoundPath: string | false

// default: false
disableSoftNavigation: boolean
disableFallback: boolean
}

export interface PagesPlusOptions {
Expand Down

0 comments on commit 38a3451

Please sign in to comment.