redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.31k stars 995 forks source link

[Bug?]: Orphaned Apollo Client on `RedwoodApolloProvider` re-render #11602

Open justinadkins opened 1 month ago

justinadkins commented 1 month ago

What's not working?

With how the provider is currently constructed, a parent re-render will orphan an apollo client instance and create a new one. I believe this causes a memory leak and cache orphaning. I discovered it because of client pollution visible when using Apollo Client's dev tools.

This is due to the lack of memoization where the client is instantiated here.

How do we reproduce the bug?

Force a re-render of a parent of the RedwoodApolloProvider, for example App. Using the Apollo Client devtools, observe a new client instantiation each render.

Screenshot 2024-09-21 at 10 16 48 AM

What's your environment? (If it applies)

No response

Are you interested in working on this?

Josh-Walker-GM commented 1 month ago

Hey @justinadkins 👋

Thanks for the really clearly documented issue! I see you are interested in helping out with it, awesome! Are you hoping to create a PR with a fix? If so I'd be happy to help out in any way you might need.

justinadkins commented 1 month ago

Hey @Josh-Walker-GM! Yes, I'm happy to submit a PR. I think the fix is pretty straight forward.

callingmedic911 commented 4 weeks ago

@justinadkins Are you thinking of using WeakRef to hold the the instance?

justinadkins commented 3 weeks ago

@callingmedic911 Just seeing this now, I hadn't considered using WeakRef. I submitted a PR #11699 if you'd like to take a look. I'm jus using a local variable to keep track of the current instance.