Skip to content

Commit

Permalink
docs: add advanced async data example
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Jan 11, 2022
1 parent 11402ee commit 4a633a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/pages/3.usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ const total = await count('restaurants')

> Check out the Strapi v3 [Count entries](https://docs-v3.strapi.io/developer-docs/latest/developer-resources/content-api/content-api.html#count-entries) REST API endpoint.
<alert type="info">

All examples above are demonstrated with http calls in script setup. However, to handle SSR properly you may want to use [useAsyncData](/advanced#async-data).

</alert>

## `useStrapiGraphQL`

This composable is an alias of `useStrapiClient` that sets the `url` to `/graphql` and `method` to `POST`. You can use this method to send an authenticated GraphQL query to your API.
Expand Down
19 changes: 19 additions & 0 deletions docs/pages/4.advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ title: Advanced
description: 'Real-life advanced usages of the strapi module.'
---

## Async data

To take full advantage of server-side rendering, you can use Nuxt [useAsyncData](https://v3.nuxtjs.org/docs/usage/data-fetching#useasyncdata) composable:

```vue
<script setup lang="ts">
import type { Restaurant } from '~/types'
import type { Strapi4Response } from '@nuxtjs/strapi'
const route = useRoute()
const { findOne } = useStrapi4()
const { data, pending, refresh, error } = await useAsyncData(
'restaurant',
() => findOne<Strapi4Response<Restaurant>>('restaurants', route.params.id)
)
</script>
```

## Auth middleware

You can protect your authenticated routes by creating a custom plugin in your project, here is an example:
Expand Down

0 comments on commit 4a633a0

Please sign in to comment.