solidjs / solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
https://solidjs.com
MIT License
32.17k stars 918 forks source link

[Solid Query] Hydration Mismatch error #2132

Open PeterDraex opened 5 months ago

PeterDraex commented 5 months ago

Describe the bug

Getting an error:

Error: Hydration Mismatch. Unable to find DOM nodes for hydration key: s00-0-0-0-0-0-0-0-0-1-3

Your Example Website or App

https://stackblitz.com/edit/github-pqy8j7-jhayd9?file=src%2Fcomponents%2FPage.tsx

Steps to Reproduce the Bug or Issue

  1. Open console
  2. See Hydration Mismatch Error

Expected behavior

Blank page is displayed, with no errors in console.

Screenshots or Videos

No response

Platform

Windows, Chrome 123

Additional context

No response

elite174 commented 5 months ago

This was solved here: https://discord.com/channels/722131463138705510/1229910798135066716/1230151418569298052

PeterDraex commented 5 months ago

This was solved here: https://discord.com/channels/722131463138705510/1229910798135066716/1230151418569298052

Yeah, a workaround of deferStream: true was suggested on Discord (thank you). Another available workaround is using <Show> instead of <Switch>.

The issue seems to be that as soon as Switch matches a case it renders and flushes the data. So in this case when pending case is matched it renders that and then yields.

indeyets commented 5 months ago

it works a bit different with solid-query 5.29:

  1. seems to reach success-state
  2. but still shows Hydration Mismatch in console
indeyets commented 5 months ago

attn @ardeora

ryansolid commented 5 months ago

It's pretty hard for me to debug Solid Query stuff. Like I need to put time aside to get into the code base. It might be easiest to report bugs against them and then they make reproductions with pure createResource so I can better see what is going on without tracing through that code.

ardeora commented 5 months ago

It's pretty hard for me to debug Solid Query stuff. Like I need to put time aside to get into the code base. It might be easiest to report bugs against them and then they make reproductions with pure createResource so I can better see what is going on without tracing through that code.

Apologies @ryansolid Here is an example with just using createResource. https://stackblitz.com/edit/github-pqy8j7-n87wnf?file=src%2Fcomponents%2FPage.tsx

I think the Switch component (during SSR) eagerly evaluates and flushes the data when any case matches? In this case you would always see the pending string in the HTML after hydration

ryansolid commented 3 months ago

Sorry took me a while to look at this. But in the createResource scenario it is acting how I'd expect. The problem is that checking loading doesn't suspend.. the server doesn't wait. And in the client it is available and we get the mismatch unless the timing works out just right. You need to read the data to suspend.

If I remove the first condition or change the first switch condition to the following it works.

<Match when={!data() && data.loading}>pending</Match>

The reason reading loading doesn't suspend is it is the mechanism we use to purposefully opt into tearing. But tearing during hydration won't work.

However making that fix in the Solid Query example I think still doesn't work. It doesn't error during hydration but it also doesn't show anything. So we will need to narrow this down further.