Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hydratation bug with first render #90

Open
adrienpsl opened this issue Nov 30, 2020 · 9 comments
Open

Hydratation bug with first render #90

adrienpsl opened this issue Nov 30, 2020 · 9 comments

Comments

@adrienpsl
Copy link

adrienpsl commented Nov 30, 2020

Hello,

I have followed the instructions in the readme but my page when it loads does the same query as the ssr page.
However there is the NEXT_DATA with the right data.
My cache is not hydrated, is it possible to have the source code of the demo so that I understand what I did wrong?

also:

In a page contained by WithApollo

export default WithApollo( { ssr : true } )( Home );

I have as props:

(2) [{…}, {…}]
0: {apolloState: {…}, apolloClient: null}
1: {}

The apolloState has the right data, how can I pass them to Apollo client ?

Thanks! :).

@adrienpsl
Copy link
Author

I found a solution, the store did not get the the store at the first render, and has it is save in globalApolloClient, remaining empty.

here the file that works with my modification :).

https://gist.github.com/adrienpsl/b99a6d0ae10803ea433bed544ce38142

Have a nice day ! :

@aboveyunhai
Copy link

aboveyunhai commented Dec 8, 2020

@adrienpsl Your modification actually solved my hydration issue with some text content inconsistence issue between server and client side. I was able to do some semi dynamic persistent layout with next.js and apollo.
Maybe you should reopen it and rise the attention to the package maintainer ?

@adrienpsl adrienpsl changed the title Demo source code Hydratation bug with first render Dec 8, 2020
@adrienpsl
Copy link
Author

Hi @aboveyunhai :),
I'm glad I that was able to help you !

You right if we are 2 I reopen this issu.

@adrienpsl adrienpsl reopened this Dec 8, 2020
@adamsoffer
Copy link
Owner

Here's the demo source code https://github.com/adamsoffer/next-apollo-example/tree/f4ea2ffd05e18af15589051ffcbd688d6b8019a1

(visible by visiting https://next-with-apollo.vercel.app/_src)

@aboveyunhai
Copy link

@adamsoffer just a weird question to elaborate the demo.
What happen if you do something like

export default withApollo({ ssr: false })(App) inside your _app.js.

and then still do export default withApollo({ ssr: true })(Home) inside the index.js

@adamsoffer
Copy link
Owner

Not sure. The page config might override it.

@aboveyunhai
Copy link

aboveyunhai commented Dec 9, 2020

@adamsoffer , that's the behavior I would expect, but it seems like it will cause some inconsistency on SSR (For example , SSR on page), it's hard to produce the issue.

https://gist.github.com/adrienpsl/b99a6d0ae10803ea433bed544ce38142#file-next-apollo-tsx-L99 (only fewer lines inside if statement) added by @adrienpsl somehow solved it. Probably you can take a look, there might be a bug from the current package. I'm still not quite understanding everything behinds Apollo state on initialization.

@adamsoffer
Copy link
Owner

@aboveyunhai feel free to submit a PR if that fixes something not working. If persistent layout is what you're after check this out https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/

@aboveyunhai
Copy link

@adrienpsl just want to further enhance your implementation in case if you hadn't meet the problem.
if (globalApolloClient && initialState && !hasSetInitialStore) will be executed when initalState = {} because if ({}) is evaluated true but we don't want to restore the empty state obviously.
So the true implementation is to do a true check on empty object such as

if (globalApolloClient && !isObjectEmpty(initialState) && !hasSetInitialStore)

export function isObjectEmpty(obj: Object) {
    for(var i in obj) return false;
    return true;
}

It seems like the problem only occurs on the really first render and and then you switch pages from SSR -> SSR -> go back to previous page SSR. The empty state kicks in and cause the unwanted store. SSR to CSR back and forth wouldn't create that empty state; And it only appears on the first time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants