Skip to main content

Function: fetchHookData()

fetchHookData<T, P, R>(fetchStrategy, ctx, options): Promise<{ additionalCacheObjects: any; data: FetchResponse<R>; hostOrSlug: undefined | string; isMainQuery: boolean; key: string; }>

Defined in: packages/next/src/data/server/fetchHookData.ts:191

A function that implements data fetching on the server. This should be used in getServerSideProps or getStaticProps.

Data fetching will be performed by the specified strategy and URL params will be automatically extracted from `context

Usage​

export async function getServerSideProps(context) {
try {
const usePostsHook = await fetchHookData(usePosts.fetcher(),context);

return addHookData([usePostsHook], {});
} catch (e) {
return handleError(e, context);
}
}´

Type Parameters​

T​

T = unknown

P​

P extends EndpointParams = EndpointParams

R​

R = T

Parameters​

fetchStrategy​

AbstractFetchStrategy<T, P, R>

The fetch strategy to use. Typically this is exposed by the hook e.g: usePosts.fetcher()

ctx​

The Next.js context, either the one from getServerSideProps or getStaticProps

GetServerSidePropsContext<any, PreviewData> | GetStaticPropsContext<any, PreviewData>

options​

FetchHookDataOptions<P, T> = {}

See FetchHookDataOptions

Returns​

Promise<{ additionalCacheObjects: any; data: FetchResponse<R>; hostOrSlug: undefined | string; isMainQuery: boolean; key: string; }>

An object with a key of data and a value of the fetched data.