When to use fetchSync vs useQuery #1112
-
It seems like It seems that the biggest difference between the two is that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Note that this might change someday, as we are revisiting how data fetching works in Server Components. For now, hopefully that guidance is helpful! |
Beta Was this translation helpful? Give feedback.
fetchSync
is a "sync version of fetch." If you are making an HTTP call to an endpoint, you should usefetchSync
. This is because Hydrogen can easily optimize this for its preload cache. It also means you don't have to write an async function or provide a custom set of cache keys.useQuery
is a way to run an async function in a Suspense-friendly way. You should use it if you need to use a third-party JavaScript library that requires async/await or Promises — aka something that isn't a simplefetch
call. Hydrogen cannot automatically infer the keys to be used in the cache like it can withfetchSync
, so you need to provide the keys every time.Note that this might change someday, as we are…