Vue3 Fundamentals · Built-in Components

Wait for content with Suspense

Waiting for async content with Suspense

Wait for content with Suspense

A component can need data before it can render. While it waits, the page shows nothing, and the tree behind it is held up. <Suspense> gives that wait a place to live.

<Suspense> renders its default slot once every component inside is ready. Until then it shows the #fallback slot:

<template>
  <Suspense>
    <AsyncChild />
    <template #fallback>
      <p>Loading...</p>
    </template>
  </Suspense>
</template>

A component becomes "async" when its setup returns a promise — usually because it awaits a fetch. <Suspense> waits for that promise, keeps the fallback on screen, then swaps in the real content.

The file already defines an AsyncChild whose setup awaits a tick before it can render. Wrap it in <Suspense> with a #fallback slot. The Test checks the fallback is showing, waits for the promise to settle, and checks the content replaced it. The starter renders the child with no boundary, so the fallback never appears and the Test fails.

Challenge

The async child has nowhere to wait. Give it a boundary: export an App component from App.vue that wraps the child in <Suspense> with a #fallback slot.

The Test checks the fallback shows first, then that the loaded content replaces it. The starter renders the child with no <Suspense>, so the fallback never appears and the Test fails.

App.vue
<script setup>
import { h } from 'vue';

const AsyncChild = {
  async setup() {
    await Promise.resolve();
    return () => h('p', { 'data-test': 'content' }, 'Loaded');
  },
};
</script>

<template>
  <AsyncChild />
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.