Skip to content

Conversation

XiNiHa
Copy link
Contributor

@XiNiHa XiNiHa commented May 27, 2025

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()} />;
};

Copy link

changeset-bot bot commented May 27, 2025

🦋 Changeset detected

Latest commit: a5f5928

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

@ryansolid
Copy link
Member

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 ryansolid merged commit bdba4dc into solidjs:main Aug 6, 2025
@XiNiHa XiNiHa deleted the fix-ssr-resource-cache branch August 7, 2025 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants