web3modal: [bug] NextJS Hydration error causes web3modal to no longer open
Link to minimal reproducible example
Steps to reproduce:
- Go to https://web3modal-w3m-account-button-already-used-moaribp1i-astaria-xyz.vercel.app
- Connect a wallet
- Click the “Other” link at the top to go to https://web3modal-w3m-account-button-already-used-moaribp1i-astaria-xyz.vercel.app/other
- Refresh the page
- Try clicking “Open Connect Modal” button again. Nothing happens.
Summary
Thanks again for releasing 3.1.0. Our core issue is more clear now. It seems the web3modal stops working if there is a hydration error.
You can see the hydration error if you run the app locally. https://legacy.reactjs.org/docs/error-decoder.html/?invariant=418 and https://legacy.reactjs.org/docs/error-decoder.html/?invariant=423 will be shown in the console on the preview.
Every other part of our still works so this seems unique to web3modal. Though perhaps other apps are failing more gracefully? Not sure.
List of related npm package versions
Latest versions:
"@web3modal/wagmi": "^3.1.0",
"next": "13.5.4",
"react": "^18",
"react-dom": "^18",
"viem": "^1.15.1",
"wagmi": "^1.4.3"
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Comments: 25 (12 by maintainers)
I fixed it by wrapping my connect wallet button in this component
<NonSSRWrapper></NonSSRWrapper>so my component is looking like this
this is
NonSSRWrappercomponent looks likeGreat to hear! I’ll investigate once v4 is stable as the upgrade is quite large with viem, wagmi, and web3modal all having breaking changes that are related.
We’re very happy with App router. Our pages are now significantly smaller in size as App router handles the bundle much better and the App router experience is nice.
We can’t take advantage of the server on most pages as most of our data comes from the blockchain and wagmi can’t handle that currently, but App router is still worth the effort imo.
Thanks for sharing we’ll look into this.
So far I found that the hydration error blocks/removes when trying to create and append a child element to the body, this is what Web3Modal does and why it fails (I assume) during this error. We’ll investigate it.
Aside from the issue, I’m curious, how do you like the app router so far compared to pages?
My main concern is that runtime errors are difficult to fully eliminate. We can’t use any static tools like typescript, eslint, etc.
With some errors (at least the hydration one) the app fails gracefully and still works (except
web3modal). Others the whole app fails/falls back to the NextJS error page.Some other situations that I’ve encountered and would be be wary of:
web3modaldid not open. I solved this on our app by removing theuseSearchParamsfrom certain pages and thenweb3modalworked on those pages. I tried to create a reproduction of this issue this morning, but was unable to get the pages to deopt into client-side rendering in my reproduction repo. If that occursweb3modaldoesn’t work. I’m not sure why since client should be ideal. I’ll try to create a reproduction of this.<div>in<p>cannot be statically checked in all cases so it could slip through, causingweb3modalto not work unless every page is manually tested. I know hydration issues fromwagmiaren’t your concern and that’s fair, but there are other types that could slip through.wagmiresponses, but there may be cases where a runtime error occurs. I’m not sure if it’d fail gracefully or not.web3modalwould handle them.Cases that I’m not concerned about:
throw new Errorfor example. The whole app seems to break/fallback to an error page soweb3modalisn’t a concern here.