Fix resource instances always getting cached on SSR
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()} />;
};
🦋 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
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.