question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v3 - SSR - Queries render with loading: true even when data exists (Example repo provided)

See original GitHub issue

Intended 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:open
  • Created 3 years ago
  • Reactions:14
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jimrandomhcommented, Jul 20, 2020

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.

2reactions
jimrandomhcommented, Jul 19, 2020

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 from getDataFromTree works, and looks like the right solution, so this is mostly a documentation issue; the migration guide should say that renderToString 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, with ssr: true, returns the correct data, but with loading: true. The cache is correctly pre-populated and no network request is sent, and it immediately does an update/rerender with loading:false, but all the extra rerendering has a noticeable performance imact.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found