Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Fix cache-less queries in SSR #770

Merged
merged 3 commits into from
Jul 5, 2019
Merged

Fix cache-less queries in SSR #770

merged 3 commits into from
Jul 5, 2019

Conversation

lemonmade
Copy link
Member

There are two types of fetchPolicies that ignore the cache: network-only and no-cache. We aren't currently handling either of them well: AFAICT, in both cases queries lead to infinite looping while trying to resolve during SSR, because the client will make a fresh query every time. This PR is a stab at addressing them that I think makes sense. In the case of network-only, we can transform it to be cache-first in SSR, so that it will save to cache and re-use for all subsequent server renders. no-cache has a promise about not putting stuff in the cache, so for those, I've updated the code to consider them equivalent to skipped queries.

cc/ @rox163 while I was playing with this locally, it reduced the number of render loops needed for products pages to just 3 (one for the first set of queries, one for the queries rendered by the product show page, and one last one where it finds no more queries to resolve).

@@ -38,12 +38,16 @@ export default function useQuery<
} = options;
const client = useApolloClient(overrideClient);

if (typeof window === 'undefined' && skip) {
if ((typeof window === 'undefined' && skip) || fetchPolicy === 'no-cache') {
Copy link
Contributor

@michenly michenly Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this early return actually breaks react hook rule. Is there anyway not to do this anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rules of hooks don't matter for server rendering. They only exist so that a single component, when updated, calls hooks in the same order, but in server renders, the entire component is thrown out each time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that make sense to me, so this is really a dev experience thing to for consumer of this package to understand why they would be getting this warning.

@lemonmade lemonmade force-pushed the fix-network-only-ssr branch from bf591d9 to db6378b Compare July 4, 2019 21:05
Copy link
Member

@GoodForOneFare GoodForOneFare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 really appreciate the extra tests 🙏

@lemonmade lemonmade merged commit 49c3cdd into master Jul 5, 2019
@lemonmade lemonmade deleted the fix-network-only-ssr branch July 5, 2019 13:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants