Skip to content

Fix resource instances always getting cached on SSR #2497

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

XiNiHa
Copy link

@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

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.

1 participant