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

[Bug?]: createResource() throws "Hydration Mismatch" errors on page load #1424

Closed
2 tasks done
FrenchDilettante opened this issue Mar 28, 2024 · 1 comment
Closed
2 tasks done
Labels
bug Something isn't working

Comments

@FrenchDilettante
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

createResource fails at the hydration phase. If you look at the example given below, the search count is empty and the <For> list throws an error:

Error: Hydration Mismatch. Unable to find DOM nodes for hydration key: [...]

If you open the page source, you will notice that the data is present at the bottom of the page:

<p>Found <!--$--><!--/--> results</p><ul></ul></div>
[...]
<script>$R[3]($R[0],$R[4]=["all","results"]);</script>

Expected behavior 🤔

Solid has the API response but is not able to hydrate the component. Using the input field triggers API calls and works fine afterwards.

Steps to reproduce 🕹

// src/components/Search.tsx
export default function Search() {
  const [searchParams, setSearchParams] = useSearchParams();

  const [results] = createResource<string[], string>(
    () => searchParams.q?.trim() || "",
    async (q) => {
      const response = await fetch("http://localhost:3000/api/search?q=" + encodeURIComponent(q));
      return response.json();
    },
  );
  
  return (
    <>
      <p>Found {results.latest?.length} results</p>
      <ul>
        <For each={results.latest}>{(result) => <li>{result}</li>}</For>
      </ul>
    </>
  );
}
// src/app.tsx
<MetaProvider>
  <Title>SolidStart - Basic</Title>
  <SearchPanel />
  <Suspense>{props.children}</Suspense>
</MetaProvider>
// src/api/search.ts
export async function GET() {
  // purposefully simplified for this bug report
  return ["some", "results"];
}

Context 🔦

I am writing a search component sitting at the top of the layout. On every page, the user should be able to type a search criteria and see matching results. I'm using useSearchParams so that the search results are still visible if the user opens a link that contains the search criteria.

Your environment 🌎

System:
  OS: Linux 6.7 Fedora 39
  CPU: 12 amd64
Binaries:
  Node: 21.7.1 (using NVM)
  npm: 10.5.0 (using NVM)
$ npm ls | grep solid
├── @solidjs/[email protected]
├── @solidjs/[email protected]
├── @solidjs/[email protected]
├── [email protected]
@FrenchDilettante FrenchDilettante added the bug Something isn't working label Mar 28, 2024
@FrenchDilettante
Copy link
Author

Closing this issue.

As soon as I opened it, I realised that the component sits outside of the tag in the layout. Moving it inside fixed the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant