Skip to content

When to use fetchSync vs useQuery #1112

Answered by jplhomer
davidhousedev asked this question in Help
Discussion options

You must be logged in to vote
  • fetchSync is a "sync version of fetch." If you are making an HTTP call to an endpoint, you should use fetchSync. 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 simple fetch call. Hydrogen cannot automatically infer the keys to be used in the cache like it can with fetchSync, so you need to provide the keys every time.

Note that this might change someday, as we are…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@jplhomer
Comment options

@davidhousedev
Comment options

@davidhousedev
Comment options

@frandiox
Comment options

@davidhousedev
Comment options

Answer selected by davidhousedev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants