I currently have the following React Query implementation:
const { data, isSuccess, isLoading } = useQuery(['myQuery', myParams], async () => {
return myAjaxCall(myParams);
}, {
cacheTime: 0
});
And I pass the results returned into a custom component:
<Results foundRecords={data}/>
I am in the process of passing the initial search results into my parent page component so that search engine crawlers are able to see the results on initial page load (search engine bots like Google’s web crawler cannot see the initial search results if the requests are made client-side – ie. with useQuery()
).
In this scenario, what’s the best way for me to integrate the initial search value I pass into my component (via NextJS’ getServerSideProps()
) with a useQuery()
implementation?
Off of the top of my head, it would look something like:
export async function getServerSideProps(context){
...
return {
initialData: ....
}
}
export default function MyPage({initialData}){
const { data, isSuccess, isLoading } = useQuery(['myQuery', myParams], async () => {
return myAjaxCall(myParams);
}, {
cacheTime: 0
});
return (
<Results foundRecords={initialData || data}/>
)
}
2
Answers
In order to get the result for the Google crawlers you need to use Metadata where in your title and descriptions are provided also you need to submit your domain name in Google console
The docs recommend to put the data into
initialData
of useQuery. Then, you can just continue to usedata
returned fromuseQuery
: