v3 - SSR - Queries render with loading: true even when data exists (Example repo provided)
See original GitHub issueIntended outcome: Using apollo getDataFromTree I expect the html produced by renderToString to not be in loading state.
Actual outcome:
The query loading is true
even when data is populated. So the component will hit the
if (loading) return <div>Loading</div>
How to reproduce the issue:
Here is a repo. You can see on the server renderToString() the loading value is true
even though data is populated with the query result. You can also see window.__APOLLO_STATE__
is correctly populated with the result.
https://github.com/bretthadley/apollo-issue-demo
Versions
System:
OS: macOS 10.15.5
Binaries:
Node: 14.3.0 - /usr/local/bin/node
Yarn: 1.17.3 - ~/.yarn/bin/yarn
npm: 6.14.5 - /usr/local/bin/npm
Browsers:
Chrome: 83.0.4103.116
Firefox: 68.0.2
Safari: 13.1.1
npmPackages:
@apollo/client: ^3.0.2 => 3.0.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Server-side rendering - Apollo GraphQL Docs
Server-side rendering (SSR) is a performance optimization for modern web apps. It enables you to render your app's initial state to raw HTML...
Read more >GraphQL Code Libraries, Tools and Services
A JavaScript library for application development using cloud services, which supports GraphQL backend and React components for working with GraphQL data.
Read more >Apollo Client Cache Rehydration in Next.js - Medium
Cache rehydration provides SSR/SSG/ISR benefits (a fully rendered HTML document on the initial page load, great SEO, etc.). The data is still available...
Read more >react-query - npm
Hooks for managing, caching and syncing asynchronous and remote data in React. Latest version: 3.39.2, last published: 4 months ago. Start using react-query...
Read more >Execute react-apollo-hooks useQuery Only the First Time a ...
useState(false) const { loading, data } = useQuery(QUERY, ... data exists if (!loading && !!data) { setSkip(true) } }, [data, loading]) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Looks like maapteh beat me to it (https://github.com/apollographql/apollo-client/issues/6651). I confirm that maapteh’s issue is the same as the one I referred to.
There appear to be two related issues here, one server side and one client side. The server-side issue is
renderToString
not working; for our project (LessWrong), this made the SSR html blank. Using the return value fromgetDataFromTree
works, and looks like the right solution, so this is mostly a documentation issue; the migration guide should say thatrenderToString
is no longer suitable. Streaming may still be an issue, but we weren’t doing that before.But there’s a second issue, which is client side. The first call to
useQuery
for a given query, withssr: true
, returns the correct data, but withloading: true
. The cache is correctly pre-populated and no network request is sent, and it immediately does an update/rerender withloading:false
, but all the extra rerendering has a noticeable performance imact.