Skip to content

How to use <Suspense> only on first load #1554

Closed Answered by ol1s
ol1s asked this question in Q&A
Discussion options

You must be logged in to vote

This is the solution I came to in the end:

async setup() {

  const booted = ref(false)
  const loading = ref(false)
  const data = ref({})

  onMounted(() => {
    if (booted.value) {
      //this fires for all subsequent routes and
      //handles loading state within the component
      loading.value = true
      data.value = await fetchData(...)
      loading.value = false
    } else {
      booted.value = true
    }
  })

  if (!booted.value) {
    //this fires on inital boot only
    //and gets resolved by <Suspense>
    data.value = await fetchData(...)
  }

  return {
    data
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ol1s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant