Skip to content

Commit 582ab52

Browse files
committed
fix(solid): give ownerless components a root in the production build
1 parent 404d06a commit 582ab52

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
A component created outside any owner (at module scope, in a timer or event handler, or after an `await`) now runs under its own root in the production build, as it already did in dev and observe. Before, the production `createComponent` called the component with no owner, so `useContext` threw `NoOwnerError` there even when the context had a default, while dev returned the default. Components created under an owner run as before, with no extra root.

‎packages/solid/src/client/component.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { untrack, createMemo, runAsSolidCopy } from "@solidjs/signals";
1+
import { untrack, createMemo, createRoot, getOwner, runAsSolidCopy } from "@solidjs/signals";
22
import {
33
IS_DEV,
44
IS_OBSERVE,
@@ -100,7 +100,8 @@ export function createComponent<T extends Record<string, any>>(
100100
return runAsSolidCopy(() => observedComponent(Comp, props || ({} as T), name, loc));
101101
}
102102
if (IS_OBSERVE) return observedComponent(Comp, props || ({} as T), name, loc);
103-
return untrack(() => Comp(props || ({} as T)));
103+
// Ownerless creation gets the root dev and observe always give, so context reads match.
104+
return (getOwner() ? untrack : createRoot)(() => untrack(() => Comp(props || ({} as T))));
104105
}
105106

106107
/**

‎packages/solid/test/dist-client-artifact.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ describe("solid-js client artifacts", () => {
8181
expect(read("solid.js")).not.toContain("._name = ");
8282
});
8383

84+
test.each([
85+
["prod", prod],
86+
["observe", observe],
87+
["dev", dev]
88+
])("%s: a component created outside any owner reads a context default", (_, artifact) => {
89+
const Theme = artifact.createContext("light");
90+
let seen: unknown;
91+
function Badge() {
92+
seen = artifact.useContext(Theme);
93+
return null;
94+
}
95+
expect(getOwner()).toBeNull();
96+
artifact.createComponent(Badge, {});
97+
expect(seen).toBe("light");
98+
});
99+
100+
test("prod: a component created under an owner runs in that owner, with no root of its own", () => {
101+
let outer: unknown;
102+
let inner: unknown;
103+
prod.createRoot(() => {
104+
outer = getOwner();
105+
prod.createComponent(() => {
106+
inner = getOwner();
107+
return null;
108+
}, {});
109+
});
110+
expect(inner).toBe(outer);
111+
});
112+
84113
test("the three artifacts export the same surface", () => {
85114
expect(Object.keys(observe).sort()).toEqual(Object.keys(prod).sort());
86115
expect(Object.keys(dev).sort()).toEqual(Object.keys(prod).sort());

0 commit comments

Comments
 (0)