Skip to content

Commit 56ca647

Browse files
feat: lazy() and clientOnly() accept an { export } option for named-export modules (#3011)
The export name is a call-site literal present in both bundles, so lazy hydration still resolves the component synchronously from the preloaded module namespace. Runtime wrappers that pick an export inside the import thunk remain unsupported (invisible to the sync claim) and now fail loudly in dev instead of silently orphaning SSR'd DOM. lazy()'s bundler-injected moduleUrl moves to the third argument, matching clientOnly; the dom-expressions transformLazy pass pads the options slot with void 0. Co-authored-by: Cursor <[email protected]>
1 parent 57cc98b commit 56ca647

16 files changed

Lines changed: 418 additions & 90 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"solid-js": minor
3+
"@solidjs/web": minor
4+
---
5+
6+
`lazy()` and `clientOnly()` accept an `{ export }` option to select a named export of the resolved module (defaults to `default`). The export name is a call-site literal available in both bundles, so lazy hydration still resolves the component synchronously from the preloaded module — wrappers that pick an export at runtime inside the import thunk remain unsupported and now fail loudly in dev. `lazy()`'s bundler-injected `moduleUrl` moves to the third argument to make room, matching `clientOnly()`.

‎packages/solid-web/server/index.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,19 @@ function registerClientOnlyPreload(moduleUrl: string): void {
194194
* they stay out of the hydration asset map. Without it (untransformed code)
195195
* or without a manifest, behavior is unchanged.
196196
*/
197+
export function clientOnly<M extends Record<string, any>, K extends keyof M & string>(
198+
fn: () => Promise<M>,
199+
options: { lazy?: boolean; export: K },
200+
moduleUrl?: string
201+
): Component<ComponentProps<M[K]> & { fallback?: JSX.Element }>;
202+
export function clientOnly<T extends Component<any>>(
203+
fn: () => Promise<{ default: T }>,
204+
options?: { lazy?: boolean; export?: string },
205+
moduleUrl?: string
206+
): Component<ComponentProps<T> & { fallback?: JSX.Element }>;
197207
export function clientOnly<T extends Component<any>>(
198-
_fn: () => Promise<{ default: T }>,
199-
_options: { lazy?: boolean } = {},
208+
_fn: () => Promise<any>,
209+
_options: { lazy?: boolean; export?: string } = {},
200210
moduleUrl?: string
201211
): Component<ComponentProps<T> & { fallback?: JSX.Element }> {
202212
return props => {

‎packages/solid-web/src/index.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,12 @@ function createElement(tagName: string, is = undefined): HTMLElement | SVGElemen
491491
) as HTMLElement | SVGElement | MathMLElement;
492492
}
493493

494-
function loadClientOnly<T>(fn: () => Promise<{ default: T }>, setComp: Setter<T | undefined>) {
495-
fn().then(m => setComp(() => m.default));
494+
function loadClientOnly<T>(
495+
fn: () => Promise<any>,
496+
setComp: Setter<T | undefined>,
497+
exportName?: string
498+
) {
499+
fn().then((m: any) => setComp(() => (exportName ? m[exportName] : m.default)));
496500
}
497501

498502
/**
@@ -511,25 +515,36 @@ function loadClientOnly<T>(fn: () => Promise<{ default: T }>, setComp: Setter<T
511515
*
512516
* By default the import starts as soon as `clientOnly` is called (module
513517
* load); pass `{ lazy: true }` to defer the import to the component's first
514-
* render.
518+
* render. Pass `{ export: "Name" }` to use a named export of the resolved
519+
* module instead of its default (mirrors `lazy()`'s option).
515520
*
516521
* @example
517522
* ```tsx
518523
* const Chart = clientOnly(() => import("./Chart.jsx"));
519524
* // <Chart fallback={<div>Loading chart…</div>} data={data()} />
520525
* ```
521526
*/
527+
export function clientOnly<M extends Record<string, any>, K extends keyof M & string>(
528+
fn: () => Promise<M>,
529+
options: { lazy?: boolean; export: K },
530+
moduleUrl?: string
531+
): Component<ComponentProps<M[K]> & { fallback?: JSX.Element }>;
522532
export function clientOnly<T extends Component<any>>(
523533
fn: () => Promise<{ default: T }>,
524-
options: { lazy?: boolean } = {},
534+
options?: { lazy?: boolean; export?: string },
535+
moduleUrl?: string
536+
): Component<ComponentProps<T> & { fallback?: JSX.Element }>;
537+
export function clientOnly<T extends Component<any>>(
538+
fn: () => Promise<any>,
539+
options: { lazy?: boolean; export?: string } = {},
525540
// Injected by the bundler's module-URL pass; consumed only by the server
526541
// half (early modulepreload hints). The client ignores it — the import
527542
// thunk itself is the loader here.
528543
_moduleUrl?: string
529544
): Component<ComponentProps<T> & { fallback?: JSX.Element }> {
530545
const [comp, setComp] = createSignal<T>();
531546
let started = !options.lazy;
532-
started && loadClientOnly(fn, setComp);
547+
started && loadClientOnly(fn, setComp, options.export);
533548
return props => {
534549
let Comp: T | undefined;
535550
let m: boolean;
@@ -540,7 +555,7 @@ export function clientOnly<T extends Component<any>>(
540555
// re-run a side-effectful importer.
541556
if (!started) {
542557
started = true;
543-
loadClientOnly(fn, setComp);
558+
loadClientOnly(fn, setComp, options.export);
544559
}
545560
// Deliberately untracked fast path: an already-loaded module (fresh
546561
// render after the import settled) skips the gate machinery entirely.

‎packages/solid-web/test/client-only.spec.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ describe("clientOnly (client)", () => {
9090
dispose();
9191
});
9292

93+
test("{ export } picks a named export of the resolved module (#3011)", async () => {
94+
let resolve!: () => void;
95+
const Chart = (props: { name: string }) => <span>chart {props.name}</span>;
96+
const promise = new Promise<{ Chart: typeof Chart; Legend: Component }>(r => {
97+
resolve = () => r({ Chart, Legend: () => <i>legend</i> });
98+
});
99+
const Widget = clientOnly(() => promise, { export: "Chart" });
100+
101+
const div = document.createElement("div");
102+
const dispose = render(() => <Widget name="io" fallback={<i>wait</i>} />, div);
103+
expect(div.innerHTML).toContain("<i>wait</i>");
104+
105+
resolve();
106+
await settle();
107+
expect(div.innerHTML).toContain("chart ");
108+
expect(div.textContent).toContain("io");
109+
dispose();
110+
});
111+
93112
test("does not forward the fallback prop to the loaded component", async () => {
94113
let seen: Record<string, unknown> | undefined;
95114
const { importer, resolve } = deferredModule((props: Record<string, unknown>) => {

‎packages/solid-web/test/server/client-only-preload.spec.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("clientOnly preload hints", () => {
5050
// Third argument as the compiler's module-URL pass injects it.
5151
const Chart = clientOnly(() => Promise.resolve({ default: NeverRendered }), {}, "./Chart.tsx");
5252
const Widget = (_props: any) => <b>widget</b>;
53-
const LazyWidget = lazy(() => Promise.resolve({ default: Widget }), "./Widget.tsx");
53+
const LazyWidget = lazy(() => Promise.resolve({ default: Widget }), undefined, "./Widget.tsx");
5454

5555
const html = await collect(
5656
() => (

‎packages/solid-web/test/server/lazy-assets-livelock.spec.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ describe("lazy() converges across re-creations with an async asset resolver", ()
9595
});
9696

9797
test("explicit-moduleUrl lazy settles instead of livelocking", async () => {
98-
const Child = lazy(() => Promise.resolve({ default: () => <b>keyed-child</b> }), "./Keyed.tsx");
98+
const Child = lazy(
99+
() => Promise.resolve({ default: () => <b>keyed-child</b> }),
100+
undefined,
101+
"./Keyed.tsx"
102+
);
99103
const counter = { creations: 0 };
100104

101105
const html = await renderComplete(

‎packages/solid-web/test/server/lazy-shell-gating.spec.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ describe("lazy() under a boundary", () => {
4444

4545
test("defers to the enclosing Loading instead of gating the shell", async () => {
4646
const Slow = (_props: any) => <b>content</b>;
47-
const LazySlow = lazy(() => wait(DELAY).then(() => ({ default: Slow })), "./Slow.tsx");
47+
const LazySlow = lazy(
48+
() => wait(DELAY).then(() => ({ default: Slow })),
49+
undefined,
50+
"./Slow.tsx"
51+
);
4852

4953
const { shell, shellAt, html } = await collectTimed(
5054
() => (
@@ -64,7 +68,7 @@ describe("lazy() under a boundary", () => {
6468

6569
test("a preloaded module still inlines with no fallback flash", async () => {
6670
const Fast = (_props: any) => <b>content</b>;
67-
const LazyFast = lazy(() => Promise.resolve({ default: Fast }), "./Slow.tsx");
71+
const LazyFast = lazy(() => Promise.resolve({ default: Fast }), undefined, "./Slow.tsx");
6872
await LazyFast.preload!();
6973

7074
const { html } = await collectTimed(

‎packages/solid-web/test/server/retry-robustness.spec.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("lazy() asset resolution across suspended passes", () => {
3737
return Promise.resolve({ js: ["/assets/mod.js"], css: [] });
3838
};
3939
const Mod = () => <b>mod-content</b>;
40-
const LazyMod = lazy(() => Promise.resolve({ default: Mod }), "./Mod.tsx");
40+
const LazyMod = lazy(() => Promise.resolve({ default: Mod }), undefined, "./Mod.tsx");
4141
// Re-suspends the boundary twice, so its discovery pass runs three
4242
// times and <LazyMod /> is re-created on each — the exact re-creation
4343
// pattern that multiplied resolver work.

‎packages/solid-web/test/server/ssr-stream.spec.tsx‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ describe("SSR Streaming — Basic Rendering", () => {
283283
const manifest = { "./Boom.tsx": { file: "assets/boom.js" } };
284284
const LazyBoom = lazy(
285285
() => new Promise<any>((_, rej) => setTimeout(() => rej(new Error("lazy failed")), 10)),
286+
undefined,
286287
"./Boom.tsx"
287288
) as any;
288289

@@ -1226,7 +1227,7 @@ describe("SSR Streaming — Asset Discovery", () => {
12261227
};
12271228

12281229
const Home = (props: any) => <div>Home Content</div>;
1229-
const LazyHome = lazy(() => asyncValue({ default: Home }), "./Home.tsx");
1230+
const LazyHome = lazy(() => asyncValue({ default: Home }), undefined, "./Home.tsx");
12301231
await LazyHome.preload!();
12311232

12321233
function App() {
@@ -1252,7 +1253,7 @@ describe("SSR Streaming — Asset Discovery", () => {
12521253

12531254
test("lazy with no manifest throws during render", async () => {
12541255
const Home = (props: any) => <div>Home</div>;
1255-
const LazyHome = lazy(() => asyncValue({ default: Home }), "./Home.tsx");
1256+
const LazyHome = lazy(() => asyncValue({ default: Home }), undefined, "./Home.tsx");
12561257
await LazyHome.preload!();
12571258

12581259
function App() {
@@ -1282,8 +1283,8 @@ describe("SSR Streaming — Asset Discovery", () => {
12821283

12831284
const CompA = () => <div>A</div>;
12841285
const CompB = () => <div>B</div>;
1285-
const LazyA = lazy(() => asyncValue({ default: CompA }), "./A.tsx");
1286-
const LazyB = lazy(() => asyncValue({ default: CompB }), "./B.tsx");
1286+
const LazyA = lazy(() => asyncValue({ default: CompA }), undefined, "./A.tsx");
1287+
const LazyB = lazy(() => asyncValue({ default: CompB }), undefined, "./B.tsx");
12871288
await LazyA.preload!();
12881289
await LazyB.preload!();
12891290

@@ -1320,6 +1321,7 @@ describe("SSR Streaming — Asset Discovery", () => {
13201321
const Comp = () => <div>Streamed</div>;
13211322
const LazyComp = lazy(
13221323
() => new Promise<{ default: typeof Comp }>(r => setTimeout(() => r({ default: Comp }), 20)),
1324+
undefined,
13231325
"./Lazy.tsx"
13241326
);
13251327

@@ -1352,7 +1354,7 @@ describe("SSR Streaming — Asset Discovery", () => {
13521354
};
13531355

13541356
const Comp = () => <div>Content</div>;
1355-
const LazyComp = lazy(() => asyncValue({ default: Comp }), "./Comp.tsx");
1357+
const LazyComp = lazy(() => asyncValue({ default: Comp }), undefined, "./Comp.tsx");
13561358
await LazyComp.preload!();
13571359

13581360
function App() {
@@ -1390,7 +1392,7 @@ describe("SSR Streaming — Asset Discovery", () => {
13901392
};
13911393

13921394
const InnerComp = () => <span>Inner</span>;
1393-
const LazyInner = lazy(() => asyncValue({ default: InnerComp }, 10), "./Inner.tsx");
1395+
const LazyInner = lazy(() => asyncValue({ default: InnerComp }, 10), undefined, "./Inner.tsx");
13941396

13951397
const OuterComp = () => (
13961398
<div>
@@ -1405,6 +1407,7 @@ describe("SSR Streaming — Asset Discovery", () => {
14051407
new Promise<{ default: typeof OuterComp }>(r =>
14061408
setTimeout(() => r({ default: OuterComp }), 20)
14071409
),
1410+
undefined,
14081411
"./Outer.tsx"
14091412
);
14101413

@@ -1435,7 +1438,7 @@ describe("SSR Streaming — Asset Discovery", () => {
14351438
};
14361439

14371440
const InnerComp = () => <span>InnerContent</span>;
1438-
const LazyInner = lazy(() => asyncValue({ default: InnerComp }, 5), "./Inner.tsx");
1441+
const LazyInner = lazy(() => asyncValue({ default: InnerComp }, 5), undefined, "./Inner.tsx");
14391442

14401443
function App() {
14411444
const slowData = createMemo(async () => asyncValue("SlowData", 40));
@@ -1487,8 +1490,8 @@ describe("SSR Streaming — Asset Discovery", () => {
14871490
return <main data-page="product">{product()}</main>;
14881491
};
14891492
const Widget = () => <aside data-widget="support">Chat with support</aside>;
1490-
const LazyRoute = lazy(() => asyncValue({ default: Route }), "./Route.tsx");
1491-
const LazyWidget = lazy(() => asyncValue({ default: Widget }), "./Widget.tsx");
1493+
const LazyRoute = lazy(() => asyncValue({ default: Route }), undefined, "./Route.tsx");
1494+
const LazyWidget = lazy(() => asyncValue({ default: Widget }), undefined, "./Widget.tsx");
14921495
await LazyRoute.preload!();
14931496
await LazyWidget.preload!();
14941497

@@ -1537,7 +1540,7 @@ describe("SSR Streaming — CSS Asset Handling", () => {
15371540
};
15381541

15391542
const Comp = () => <div>Content</div>;
1540-
const LazyComp = lazy(() => Promise.resolve({ default: Comp }), "./Comp.tsx");
1543+
const LazyComp = lazy(() => Promise.resolve({ default: Comp }), undefined, "./Comp.tsx");
15411544
await LazyComp.preload!();
15421545
const gate = deferred<string>();
15431546

@@ -1580,7 +1583,11 @@ describe("SSR Streaming — CSS Asset Handling", () => {
15801583
};
15811584

15821585
const StyledComp = () => <div>Styled</div>;
1583-
const LazyStyled = lazy(() => Promise.resolve({ default: StyledComp }), "./Styled.tsx");
1586+
const LazyStyled = lazy(
1587+
() => Promise.resolve({ default: StyledComp }),
1588+
undefined,
1589+
"./Styled.tsx"
1590+
);
15841591
await LazyStyled.preload!();
15851592
const gate = deferred<string>();
15861593

@@ -1632,11 +1639,11 @@ describe("SSR Streaming — CSS Asset Handling", () => {
16321639
};
16331640

16341641
const CompA = () => <div>A</div>;
1635-
const LazyA = lazy(() => asyncValue({ default: CompA }), "./A.tsx");
1642+
const LazyA = lazy(() => asyncValue({ default: CompA }), undefined, "./A.tsx");
16361643
await LazyA.preload!();
16371644

16381645
const CompB = () => <div>B</div>;
1639-
const LazyB = lazy(() => Promise.resolve({ default: CompB }), "./B.tsx");
1646+
const LazyB = lazy(() => Promise.resolve({ default: CompB }), undefined, "./B.tsx");
16401647
await LazyB.preload!();
16411648
const gate = deferred<string>();
16421649

@@ -1693,7 +1700,7 @@ describe("renderToString — Asset Discovery", () => {
16931700
};
16941701

16951702
const Home = (props: any) => <div>Home Content</div>;
1696-
const LazyHome = lazy(() => asyncValue({ default: Home }), "./Home.tsx");
1703+
const LazyHome = lazy(() => asyncValue({ default: Home }), undefined, "./Home.tsx");
16971704

16981705
const html = renderToString(
16991706
() => (
@@ -1721,7 +1728,7 @@ describe("renderToString — Asset Discovery", () => {
17211728
};
17221729

17231730
const Comp = () => <div>Content</div>;
1724-
const LazyComp = lazy(() => asyncValue({ default: Comp }), "./Comp.tsx");
1731+
const LazyComp = lazy(() => asyncValue({ default: Comp }), undefined, "./Comp.tsx");
17251732

17261733
const html = renderToString(
17271734
() => (
@@ -1757,8 +1764,8 @@ describe("renderToString — Asset Discovery", () => {
17571764

17581765
const CompA = () => <div>A</div>;
17591766
const CompB = () => <div>B</div>;
1760-
const LazyA = lazy(() => asyncValue({ default: CompA }), "./A.tsx");
1761-
const LazyB = lazy(() => asyncValue({ default: CompB }), "./B.tsx");
1767+
const LazyA = lazy(() => asyncValue({ default: CompA }), undefined, "./A.tsx");
1768+
const LazyB = lazy(() => asyncValue({ default: CompB }), undefined, "./B.tsx");
17621769

17631770
const html = renderToString(
17641771
() => (
@@ -1790,7 +1797,7 @@ describe("renderToString — Asset Discovery", () => {
17901797
};
17911798

17921799
const Home = () => <div>Home</div>;
1793-
const LazyHome = lazy(() => asyncValue({ default: Home }), "./Home.tsx");
1800+
const LazyHome = lazy(() => asyncValue({ default: Home }), undefined, "./Home.tsx");
17941801

17951802
const html = renderToString(
17961803
() => (
@@ -1817,7 +1824,7 @@ describe("renderToString — Asset Discovery", () => {
18171824
};
18181825

18191826
const Profile = (props: any) => <div>{props.name}</div>;
1820-
const LazyProfile = lazy(() => asyncValue({ default: Profile }), "./Profile.tsx");
1827+
const LazyProfile = lazy(() => asyncValue({ default: Profile }), undefined, "./Profile.tsx");
18211828

18221829
function App() {
18231830
const data = createMemo(() => asyncValue("Jon", 100));
@@ -1848,7 +1855,7 @@ describe("renderToString — Asset Discovery", () => {
18481855

18491856
test("lazy with no manifest throws", () => {
18501857
const Home = () => <div>Home</div>;
1851-
const LazyHome = lazy(() => asyncValue({ default: Home }), "./Home.tsx");
1858+
const LazyHome = lazy(() => asyncValue({ default: Home }), undefined, "./Home.tsx");
18521859

18531860
expect(() =>
18541861
renderToString(() => (
@@ -1879,7 +1886,7 @@ describe("Entry CSS Auto-Discovery", () => {
18791886
};
18801887

18811888
const Comp = () => <div>Content</div>;
1882-
const LazyComp = lazy(() => asyncValue({ default: Comp }), "./Lazy.tsx");
1889+
const LazyComp = lazy(() => asyncValue({ default: Comp }), undefined, "./Lazy.tsx");
18831890
await LazyComp.preload!();
18841891

18851892
function App() {
@@ -1977,7 +1984,7 @@ describe("Entry CSS Auto-Discovery", () => {
19771984
};
19781985

19791986
const Comp = () => <div>Styled</div>;
1980-
const LazyComp = lazy(() => asyncValue({ default: Comp }), "./Styled.tsx");
1987+
const LazyComp = lazy(() => asyncValue({ default: Comp }), undefined, "./Styled.tsx");
19811988
await LazyComp.preload!();
19821989

19831990
function App() {
@@ -2117,6 +2124,7 @@ describe("SSR — Fragment wrapping props.children", () => {
21172124
new Promise<{ default: typeof HomeContent }>(r =>
21182125
setTimeout(() => r({ default: HomeContent }), 10)
21192126
),
2127+
undefined,
21202128
"./Home"
21212129
);
21222130

0 commit comments

Comments
 (0)