fetchPages
The fetchPages function is useful when you want to retrieve all your pages from outside the React context (where you could use the usePagesPublic hook instead).
In particular, this comes in handy to retrieve all the pages during the build process of a static website. Indeed, this is the method used in our Gatsby and Next.js starter projects.
Signature (simplified)
const fetchPages = async (
apiKey: string,
options: {...}
): Promise<types.PageFromList[] | types.PagesFromListWithPagination>
Property | Definition |
---|---|
apiKey | Api Key of your React Bricks app (a string). |
options | Optional object to filter the pages (see below). |
Options
The options object has the following shape
{
type?: string
types?: string[]
tag?: string
language?: string
usePagination?: boolean
page?: number
pageSize?: number
sort?: string
filterBy?: { [key: string]: any}
}
Property | Definition |
---|---|
type | Optional string to return only the pages with the specified page type. |
types | Optional array of strings to return only the pages with one of the specified page types. |
tag | Optional string to return only the pages with the specified tag. |
language | Optional string to return only the pages with the specified language. |
usePagination | If true, it will consider the page and pageSize parameters and it will return paginated results |
page | The page number, in case of pagination |
pageSize | The page size, in case of pagination |
sort | Sort parameter: currently it accepts only createdAt ,-createdAt ,publishedAt ,-publishedAt |
filterBy | Used to filter by a custom field. The object should have a key for each field and the value that it should have (or an array of possible values). The queries on the fields are in logical AND. For example: { category: 'shoes', subcategory: ['snickers', 'running'] } . At the moment you cannot filter using a complex type (like an object) for the value, so really now it should be string or string[] } |
Return value
fetchPages
returns a promise which resolves to.
- When
usePagination
isfalse
, an array of type PageFromList - When
usePagination
istrue
, an object of type PagesFromListWithPagination
To retrieve the content of each page, you can use the fetchPage function.
Usage example
fetchPages('API_KEY', { type: 'blogPost', tag: 'react' }).then((data) => {
console.log(data)
})