Replies: 2 comments 3 replies
-
Oooh, cool ideas!! Here's another option: Option 3: extend getStaticPropsexport async function getStaticProps(context) {
return {
props: {someThingElse: ''},
useQuery: [getInventory, {where: {id: context.params.productId}}, {staleTime: 5 * 1000 * 60}]
}
}
function Page({queryData, queryExtras, someThingElse}) {
return (
<div>
Inventory: {queryData.count}
<button onClick={queryExtras.refetch}>Refresh</button>
</div>
)
}
`` |
Beta Was this translation helpful? Give feedback.
2 replies
-
Option 4: Build this (server-site prefetching and query cache hydration) into blitz!I was unaware that there's an official react-query + next way to do this! (Since react-query 2.13.0). https://react-query.tanstack.com/docs/guides/ssr#integrating-with-nextjs // pages/posts.jsx
import { QueryCache } from 'react-query'
import { dehydrate } from 'react-query/hydration'
export async function getStaticProps() {
const queryCache = new QueryCache()
await queryCache.prefetchQuery('posts', getPosts)
return {
props: {
dehydratedState: dehydrate(queryCache),
},
}
}
function Posts() {
// This useQuery could just as well happen in some deeper child to
// the "Posts"-page, data will be available immediately either way
const { data } = useQuery('posts', getPosts)
// This query was not prefetched on the server and will not start
// fetching until on the client, both patterns are fine to mix
const { data: otherData } = useQuery('posts-2', getPosts)
// ...
} Seems like the way to go! Plus some additional sugar over how react query is used in getStaticProps. Perhaps just (1) a flag in |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I often have pages I want to pre-render statically (using
getStaticProps
), but once they load, there are conditions when I want to reload that data (usinguseQuery
).I see at least two possibilities.
Option 1: getStaticPropsQuery
This would be a new export from pages that replaces
getStaticProps
, and—instead of generating the props directly—returns the args foruseQuery
based on the route params. These can then be used as the static props and place the page within a wrapper that dynamically updates them (just wrapping the page inside a useQuery with the returned args).Option 2: prop-less pages
An alternative would be to have pages with no props, and use a hook for page data. This takes us further from next, but is less magical.
Pages would be allowed to have
getStaticPaths
but nogetStaticProps
. Instead,useQuery
runs on server side.Thoughts?
For reference, here's the twitter thread where this was initially discussed: https://twitter.com/edelwax/status/1331735357700894727/photo/1
Beta Was this translation helpful? Give feedback.
All reactions