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
How to use with universal/isomorphic apps (NextJS) #32
Comments
Hi @anselmdk — Unfortunately, If you're using server-side rendering to prefetch the Apollo queries needed for the page (per the guide here), you won't be able to use The reason for this is that, at least right now, it's not possible to merge two separate copies of the store. With server-side rendering, you're essentially filling a store on the server, and then persisting it to the page and restoring it in the browser. In general, the value of In Apollo Client 3.0, we'll think about a better way to coordinate these two technologies. I hope this helps! |
Hi @jamesreggio, thanks for the answer. This was a little bit my fear. |
@anselmdk you probably figured out this workaround already, but faced with a similar situation, I restore all my client side state in the |
@majelbstoat I am facing the same issue. Can you provide a snippet of your workaround implementation? |
@mshahov: static async getInitialProps(ctx: InitialContext<any>): Promise<WithPageProps> {
const { req, res, asPath, pathname, query } = ctx
let token
if (req) {
const { session } = req
if (session) {
token = session.token
}
}
const apollo = createApolloClient(null, token)
if (req) {
const { session, env } = req
// Write initial values for local client state
// TODO(jamie) Figure out how to not have to specify these here.
apollo.writeQuery({
query: SESSION,
data: { session },
})
apollo.writeQuery({
query: ENV,
data: { env },
})
apollo.writeQuery({
query: SAVE_STATUS,
data: { saveStatus: 'WAITING' },
})
}
// This seems strange, but we are reading this on both client and server.
const result: session | null = apollo.readQuery({
query: SESSION,
})
const session = result ? result.session : null
// ... rest of the stuff in getInitialProps, quite a lot like the with-apollo example in nextjs. It's kinda ugly, but does the job. I'm kind of unwilling to expand the use of client/apollo-link-state stuff until this gets better though. |
I'm trying to figure out a lib to perform a more granular cache persisting system. My tests so far show that SSR and client-side persistence could live well together. https://github.com/TallerWebSolutions/apollo-cache-instorage |
Can apollo-cache-persist consider doing what TallerWebSolutions/apollo-cache-instorage is doing:
SSR helps but when web users come back to the site or open another tab, it would be ideal to not refetch all the data that has already been fetched. |
Any updates on how to do this? |
@anselmdk can you share what was your solution to deal with this issue. I'm doing something similar to what you were doing, just trying to use the apollo-cache-persists only for client side data, so when I refresh the browser I can still see the data without having to store it on the server. |
Guys, sorry for not hearing your mentions. We went away from apollo-cache-persist because of the mentioned limitations, and are now persisting our sessions on the server. |
Did you find any solutions for this ? |
Is support going to be implemented any time soon? There's another package called |
@wilmervanheerde My fellow (Dutchie) i presume. Thank you for showing me @wora/apollo-offline. But if im not mistaken its not running with AC3. If you have any idea how to persist AC# with local state in an SSR app we could really use some advice. IF you can help we will gladly opensource a boilerplate so others can implement it in nextjs as well. |
ROFL :) How about disabling persist if window is undefined? |
My bad typo... Still though, what is the best way to persist cache in an SSR application @wtrocki ? We are working with a team of super enthusiast but very junior devs to build a streaming platform for fashion students. And persisting cache is a good way is one of our issues. We are willing to opensource the boilerplate for this so others can do the same. Would you be willing to help us out? |
Any progress on this? We failed to implement this with Next.js Project. |
Yes I have made a working example. Important sidenote it only caches the apollo cache not the reactive variables.
|
What is this line do ? |
It imports the cache from a file its specified in. |
@sanderkooger I tried your example but it's not working with Apollo 3. Do you have any example repo? |
@krish-dev , no not yet, I am looking for a solution though, If you find one before me let me know please |
Anyone found solution? |
So the Apollo cache can't be persisted with Nextjs? Back to Redux it is... |
@JavaJamie we actually managed to get it working! @onair-lena could you share how its fixed for us ? |
@onair-lena, can you please share with us how you have used it? |
I think I've managed to add apollo-cache-persist based on with-apollo example from Next.js repo - vercel/next.js#29718 |
@adrianos10 Do you know how much time it will take before the author accepts your PR ? |
@besSejrani No idea, I will ping them. But this is just an example based on existing with-apollo example, so you can check if it works for you. |
I've been playing around a little getting this to work with Nextjs and
localStorage
, specifically with the with-apollo example, with no luck.The gist seems to be that the example initialises apollo twice. First on the server, and subsequently on the client, with the initial state set on the server. On the server, however
localStorage
is not available, hence I initialized on the client only, which seems to be the issue (localStorage
is written to, but not rehydrated from):The question is, will this potentially never work in an universal app like NextJS, and what would be the alternatives? The README mentions that redux-persist-cookie-storage (which seems to cater for universal apps) could be used interchangeably, but I've had no luck using that either.
The text was updated successfully, but these errors were encountered: