Skip to content

RouterSuspense ​

RouterSuspense provides loading and error states for async content and automatically detects errors from props getters and awaited useLoader calls. To catch errors unrelated to routing, use the catch prop.

vue
<template>
  <RouterSuspense>
    <RouterView />

    <template #fallback>
      <p>Loading…</p>
    </template>

    <template #error="{ error }">
      <p>Unable to load this page: {{ error }}</p>
    </template>
  </RouterSuspense>
</template>

Props ​

PropRequiredTypeDefaultDescription
loadersfalsebooleantrueIncludes awaited useLoader errors
propsfalsebooleantrueIncludes route props getter errors
catchfalseboolean or (error: unknown) => booleanfalseIncludes all component errors when true, or only errors matching the predicate
vue
<script setup lang="ts">
import { ExpectedError } from './errors'

const catchError = (error: unknown) => error instanceof ExpectedError
</script>

<template>
  <RouterSuspense :catch="catchError">
    <RouterView />
  </RouterSuspense>
</template>

Recovery ​

Call router.refresh() to restart the current route and render the content again, including when the URL has not changed.

vue
<script setup lang="ts">
import { useRouter } from '@kitbag/router'

const router = useRouter()
</script>

<template>
  <RouterSuspense>
    <RouterView />

    <template #error>
      <p>Unable to load this page.</p>
      <button @click="router.refresh()">Try again</button>
    </template>
  </RouterSuspense>
</template>