useContent
Learn how to fetch content using composables.
This composable fetches the content of a Slot with full server-side rendering support. It uses Nuxt's useAsyncData under the hood, so the content is fetched on the server during SSR and hydrated on the client.
Signature
This composable has the following signature:
function useContent<T extends SlotId>( id: T, options?: UseContentOptions,): AsyncData<FetchResponse<T>>;Example
Here is an example of how to use this composable:
<script setup>const {data} = await useContent('home-hero');</script>
<template> <div v-if="data"> <strong>{{ data.content.title }}</strong> <p>{{ data.content.subtitle }}</p> <a :href="data.content.button.link">{{ data.content.button.label }}</a> </div></template>Parameters
- idstring
The ID of the slot to fetch.
You can specify the version of the slot by passing a versioned ID in the form id@version. For example, passing home-hero@1 will fetch the content for the home-hero slot in version 1. Not specifying a version number is the same as passing home-hero@latest, which will load the content for the latest version.
Best practiceAlways specify a version to ensure the front end receives content with the expected structure despite future schema changes.
For more information, see Slot versioning.
- options(optional)object
The fetch options.
- fallback(optional)JSON
A fallback value to use in case of an error.
Auto-providedIf you are using the Croct CLI, you do not need to set a fallback unless you want to use a different one.
If not specified, the error ref will contain the error and data will remain null.
- preferredLocale(optional)string
The locale code to fetch the content.
The code consists of a two-part string that specifies the language and, optionally, the country. For example, en represents English, en-us stands for English (United States), and pt-br for Portuguese (Brazil). It is case-insensitive and supports both hyphens and underscores as separators to accommodate the different conventions used by browsers, libraries, and other systems.
If no content is available in the preferred locale, the default locale content is returned instead.
Default:default locale- timeout(optional)number
The maximum fetch time in milliseconds.
Once reached, the SDK will abort the request and reject the promise with a timeout error.
- attributes(optional)object
The map of attributes to inject in the evaluation context.
The attributes can be referenced in audience conditions using the context variable. For example, suppose you pass the following attributes:
{cities: ["New York", "San Francisco"]}You can then reference them in queries like:
context's cities include location's cityNameFor more information, see Context variables.
The following restrictions apply to the attributes:
- Up to 30 entries and 5 levels deep
- Keys can be either numbers or non-empty strings with a maximum length of 50 characters
- Values can be null, numbers, booleans, strings (up to 50 characters), or nested maps
- Nested maps follow the same constraints for keys and values
- fallback(optional)
Return
The return is a Nuxt AsyncData object with the following properties:
- dataRef<FetchResponse|null>
A ref containing the fetch response, or null while loading.
The response includes a content property with the slot content.
- pendingRef<boolean>
A ref indicating whether the content is currently being fetched.
- errorRef<Error|null>
A ref containing the error if the fetch failed, or null otherwise.