solid icon indicating copy to clipboard operation
solid copied to clipboard

Fix resource instances always getting cached on SSR

Open XiNiHa opened this issue 6 months ago • 1 comments

Summary

Fixes createResource() calls on SSR always caching the initially created resource instance, potentially resulting in stale closure issues on edge cases.

How did you test this change?

Before this PR, the following code didn't fetch and render data on SSR. Now it does.

import { createSignal, createComputed, createResource, lazy } from "solid-js";
const Profile = lazy(() => import("./Profile"));

export default () => {
  const [userId, setUserId] = createSignal();

  const [user] = createResource(() => {
    console.log("LOAD USER");
    return new Promise(res => {
      setTimeout(() => res({ id: "1", firstName: "Jon", lastName: "Snow" }), 400);
    });
  });
  createComputed(() => {
    const u = user();
    if (!u) return;
    setUserId(u.id);
  });
  const [info] = createResource(
    userId,
    () => {
      // simulate cascading data loading
      console.log("LOAD INFO");
      return new Promise(res => {
        setTimeout(
          () =>
            res(["Something Interesting", "Something else you might care about", "Or maybe not"]),
          400
        );
      });
    },
    { initialValue: [] }
  );

  return <Profile user={user()} info={info()} />;
};

XiNiHa avatar May 27 '25 16:05 XiNiHa

🦋 Changeset detected

Latest commit: a5f59281b5e4ee338834d323d6f14fc12479069b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
solid-js Patch
test-integration Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar May 27 '25 16:05 changeset-bot[bot]

I see it only now caches it if it is actually fetching. Ok. This is s tricky area in general and one of the reasons I want to get rid of createComputed in the future but I think this should be ok.

ryansolid avatar Aug 06 '25 19:08 ryansolid