@threlte/extras

<Suspense>

The component <Suspense> allows you to orchestrate the loading of resources inside (nested) child components. The implementation roughly follows a subset of the concept established by the React Suspense API and has certain limitations.

The idea is to allow a parent component to make decisions based on the loading state of child components. The parent component can then decide to show a loading indicator or a fallback component while the child component is loading.

Usage

In a child component

Let’s have a look at a simple component that loads a model with the hook useGltf.

Model.svelte
<script>
  import { T } from '@threlte/core'
  import { useGltf } from '@threlte/extras'

  const gltf = useGltf('model.gltf')
</script>

{#await gltf then { scene }}
  <T is={scene}>
{/await}

We can make that component suspense-ready by using the hook useSuspense and passing the promise returned by useGltf to it.

Model.svelte
<script>
  import { T } from '@threlte/core'
  import { useGltf, useSuspense } from '@threlte/extras'

  const suspend = useSuspense()
  const gltf = suspend(useGltf('model.gltf'))
</script>

{#await gltf then { scene }}
  <T is={scene}>
{/await}

Suspense-ready

Suspense-ready means it has the ability to hit a <Suspense> boundary if there is any. If there’s no <Suspense> boundary, the component will behave as usual.

In a parent component

Now we implement the component “Model.svelte” in a parent component:

Parent.svelte
<script>
  import Model from './Model.svelte'
</script>

<Model />

To let the parent component decide what to do while the model component is loading, we wrap the child component in a <Suspense> component and show a fallback component while the child component is loading.

Parent.svelte
<script>
  import Model from './Model.svelte'
  import Fallback from './Fallback.svelte'
  import { Suspense } from '@threlte/extras'
</script>

<Suspense>
  <Model />

  {#snippet fallback()}
    <Fallback />
  {/snippet}
</Suspense>

Error Boundary

In contrast to the React Suspense API, the <Suspense> component also acts as an error boundary for async requests made in child components wrapped in suspend.

The "error" snippet will be mounted as soon as an error is thrown in a child component wrapped in suspend and the snippet prop errors can be used to display a meaningful error message.

UX considerations

The <Suspense> component is a great tool to improve the user experience of your application. However, it is important to consider the following points:

  1. The <Suspense> component will only ever show one of the available slots at any time:
  • The snippet "error" will be shown as soon as an error is thrown.
  • The snippet "fallback" will be shown as long as a child component is suspended.
  • The default slot will be shown as long as no child component is suspended and no error has been thrown.
  1. Umounting a component that suspends rendering through awaiting a resource or throwing an error will discard its suspend state.
  2. Directly citing the React Suspense API:

    Don’t put a Suspense boundary around every component. Suspense boundaries should not be more granular than the loading sequence that you want the user to experience.

  3. A <Suspense> component can be re-suspended if a child component invokes suspend again. The property final on the component <Suspense> can be used to prevent this behavior.

Limitations

  1. It’s important to keep in mind that while the contents of the default slot are not visible, they are still mounted and the component is otherwise fully functional. This means that any side effects (for instance useTask hooks) that are invoked in a suspense-ready child component will be executed immediately.
Model.svelte
<script>
  import { T, useTask } from '@threlte/core'
  import { useGltf, useSuspense } from '@threlte/extras'

  const suspend = useSuspense()
  const gltf = suspend(useGltf('model.gltf'))

  useTask(() => {
    // This will be executed immediately
  })
</script>

{#await gltf then { scene }}
  <T is={scene}>
{/await}
  1. Also, in contrast to the React Suspense API, the <Suspense> component does not support showing a stale version of a child component while it is loading.

Component Signature

Props

name
type
required
default
description

final
boolean
no
false
If final is set to true, components cannot re-suspend the suspended state.

Events

name
payload
description

load
void
Fires when all child components wrapped in `suspend` have finished loading.

suspend
void
Fires when a child component suspends.

error
Error
Fires when an error is thrown in a child component wrapped in `suspend`.