Skip to content

Commit 66d9f58

Browse files
committed
fix(fields): load the grid body from a committed mount's effect, not React.lazy
A render React discards before it commits requests nothing, so a failed load cannot retry in a loop; each later mount asks the loader again. Claude-Session: https://claude.ai/code/session_01ADHkmNmMEXS4EzeVz9RjvD Co-authored-by: Claude <noreply@anthropic.com>
1 parent 86285df commit 66d9f58

2 files changed

Lines changed: 41 additions & 33 deletions

File tree

‎.changeset/36451-grid-field-lazy-retry.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fix(fields): a line-item grid whose first load failed loads again on its next mo
77
Clause-②: no
88
Metadata-protocol: none
99

10-
The line-item grid's body loads on first use. When that first load failed once (a network blip, a deploy that swapped chunk names) and the host had not preloaded it, the grid showed its error from then on, even after the body was reachable again, until the page was reloaded. `GridField` / `LineItemsField` now replace the lazy body whose load failed, so the failed mount still reports the error to the nearest error boundary and the next mount asks for the body again. The `grid` form-field widget (the registered `field:grid` and `getLazyFieldWidget('grid')`) now resolves to that same face, so it recovers the same way.
10+
The line-item grid's body loads on first use. When that first load failed once (a network blip, a deploy that swapped chunk names) and the host had not preloaded it, the grid showed its error from then on, even after the body was reachable again, until the page was reloaded. `GridField` / `LineItemsField` no longer hold the body in a `React.lazy`, which keeps a failed load for good: each mount that finds the body still loading asks for it once, a failure still reaches the nearest error boundary, and the next mount asks again. The `grid` form-field widget (the registered `field:grid` and `getLazyFieldWidget('grid')`) now resolves to that same face, so it recovers the same way.
1111

1212
What does not change: a mount after the body has loaded renders it directly, `preloadGridField()` still never rejects, and the props of both names are unchanged. A form drawing a `grid` field shows the grid's own loading placeholder while the body loads. No metadata key, value or default changes.

‎ui/packages/fields/src/widgets/GridField.ts‎

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ let gridFieldWidgetModule: GridFieldWidgetModule | undefined;
422422
let gridFieldWidgetLoad: Promise<GridFieldWidgetModule> | undefined;
423423

424424
/**
425-
* The ONE loader of the grid's body: the lazy face below and {@link
425+
* The ONE loader of the grid's body: the face below and {@link
426426
* preloadGridField} go through it (and the `grid` form-field loader in
427427
* `../index.tsx` reaches it through the face), so the body is requested once
428428
* and a mount after any of them has resolved renders without a boundary. A
@@ -459,28 +459,6 @@ export function preloadGridField(): Promise<void> {
459459
);
460460
}
461461

462-
/**
463-
* The lazy body a mount that finds it still loading renders. `React.lazy`
464-
* keeps its first outcome for good, a rejection included, so a lazy whose load
465-
* failed is replaced by a fresh one: the failed mount still hands the error to
466-
* the nearest error boundary, and the next mount asks the loader again instead
467-
* of rethrowing that failure until the page is reloaded.
468-
*/
469-
let LazyGridFieldWidget = lazyGridFieldWidget();
470-
471-
function lazyGridFieldWidget(): React.LazyExoticComponent<React.ComponentType<GridFieldWidgetProps>> {
472-
const lazy = React.lazy(() =>
473-
loadGridFieldWidget().then(
474-
(loaded) => ({ default: loaded.GridFieldWidget }),
475-
(error: unknown) => {
476-
if (LazyGridFieldWidget === lazy) LazyGridFieldWidget = lazyGridFieldWidget();
477-
throw error;
478-
},
479-
),
480-
);
481-
return lazy;
482-
}
483-
484462
/**
485463
* What a grid mounted before its body arrived shows meanwhile: a box of the
486464
* grid's header height, marked busy, so the form does not jump when the table
@@ -499,20 +477,50 @@ function GridFieldLoading(): React.ReactElement {
499477
* props as the body, see `GridFieldWidgetProps`.
500478
*/
501479
export function GridField(props: GridFieldWidgetProps): React.ReactElement {
502-
// Decided once per mount: switching from the boundary to the direct render
503-
// when the load finishes would remount the body and drop the focused cell,
504-
// and so would switching to the lazy that replaces a failed one.
480+
// Decided once per mount: switching from the loading face to the direct
481+
// render when the load finishes would remount the body and drop the focused
482+
// cell.
505483
const [loadedAtMount] = React.useState(() => gridFieldWidgetModule);
506-
const [LazyAtMount] = React.useState(() => LazyGridFieldWidget);
507484
if (loadedAtMount) {
508485
const { GridFieldWidget } = loadedAtMount;
509486
return React.createElement(GridFieldWidget, props);
510487
}
511-
return React.createElement(
512-
React.Suspense,
513-
{ fallback: React.createElement(GridFieldLoading) },
514-
React.createElement(LazyAtMount, props),
515-
);
488+
return React.createElement(GridFieldOnceLoaded, props);
489+
}
490+
491+
type GridFieldLoadOutcome =
492+
| { readonly loaded: GridFieldWidgetModule }
493+
| { readonly failed: unknown };
494+
495+
/**
496+
* A grid mounted before its body arrived: {@link GridFieldLoading} until the
497+
* body loads, then the body. Each COMMITTED mount asks the loader once, from an
498+
* effect, and a failed load is thrown from the next render to the nearest
499+
* error boundary. Not `React.lazy`: a lazy component keeps its first outcome
500+
* for good, a rejection included, so a later mount would rethrow that failure
501+
* until the page is reloaded. A render React discards before it commits runs
502+
* no effect, so it requests nothing, and the loader forgets a failure, so the
503+
* next mount (an error boundary's reset, a reopened form) loads again.
504+
*/
505+
function GridFieldOnceLoaded(props: GridFieldWidgetProps): React.ReactElement {
506+
const [outcome, setOutcome] = React.useState<GridFieldLoadOutcome | undefined>(undefined);
507+
React.useEffect(() => {
508+
let mounted = true;
509+
loadGridFieldWidget().then(
510+
(loaded) => {
511+
if (mounted) setOutcome({ loaded });
512+
},
513+
(failed: unknown) => {
514+
if (mounted) setOutcome({ failed });
515+
},
516+
);
517+
return () => {
518+
mounted = false;
519+
};
520+
}, []);
521+
if (!outcome) return React.createElement(GridFieldLoading);
522+
if ('failed' in outcome) throw outcome.failed;
523+
return React.createElement(outcome.loaded.GridFieldWidget, props);
516524
}
517525

518526
/** Semantic alias — the master-detail subform's child grid. */

0 commit comments

Comments
 (0)