Skip to content

Commit a7260b7

Browse files
committed
fix(fields): a failed load of the grid body no longer sticks until a reload
The lazy face replaces a React.lazy whose load failed, so the next mount asks the loader again; the grid form-field loader now resolves to that face. Claude-Session: https://claude.ai/code/session_01ADHkmNmMEXS4EzeVz9RjvD Co-authored-by: Claude <noreply@anthropic.com>
1 parent cfb5f2d commit a7260b7

3 files changed

Lines changed: 82 additions & 34 deletions

File tree

‎ui/packages/fields/src/index.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { deriveHexBadgePalette } from './hexBadgePalette.js';
3636
import { formatNumberFieldValue, formatCurrency, formatPercentPoints, percentCellScale } from './widgets/number-format.js';
3737
import { useFieldTranslation } from './widgets/useFieldTranslation.js';
3838
import { useBooleanValueLabel } from './widgets/booleanValueLabel.js';
39-
import { loadGridFieldWidget } from './widgets/GridField.js';
39+
import { GridField } from './widgets/GridField.js';
4040
import { RecordHoverCard, type RecordHoverCardObjectDef } from './RecordHoverCard.js';
4141

4242
// Module-level cache so multiple renderers fetching the same lookup ID
@@ -4832,9 +4832,11 @@ const fieldWidgetMap = {
48324832
// Complex data types
48334833
'object': () => import('./widgets/ObjectField.js').then(m => ({ default: m.ObjectField })),
48344834
'vector': () => import('./widgets/VectorField.js').then(m => ({ default: m.VectorField })),
4835-
// The grid's body, through its public face's one loader so a lazy `GridField`
4836-
// mounted after this resolved renders without a boundary (objectstack#36102).
4837-
'grid': () => loadGridFieldWidget().then(m => ({ default: m.GridFieldWidget })),
4835+
// The grid's public face, already in this chunk, not its body: the face loads
4836+
// the body through its one loader (objectstack#36102) and asks again after a
4837+
// failed load, where a `React.lazy` wrapped around the body here would keep
4838+
// the failure until the page is reloaded (objectstack#36451).
4839+
'grid': () => Promise.resolve({ default: GridField }),
48384840

48394841
// Additional field types from @objectstack/spec
48404842
'color': () => import('./widgets/ColorField.js').then(m => ({ default: m.ColorField })),

‎ui/packages/fields/src/widgets/GridField.test.tsx‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,10 +1576,10 @@ describe('GridField — the lazy face the package exports (objectstack#36102)',
15761576
expect(cell.value).toBe('Widget');
15771577
});
15781578

1579-
// objectstack#36451 — a host that renders the face without preloading, whose
1580-
// first request for the body fails once (a network blip, a deploy that
1579+
// objectstack#36451 — a host that renders the grid without preloading it,
1580+
// whose first request for the body fails once (a network blip, a deploy that
15811581
// swapped chunk names), must not keep that failure for the whole session.
1582-
it('a mount after a failed load of the body renders the grid once the body is reachable, with no reload', async () => {
1582+
describe('after one failed load of the body', () => {
15831583
class Boundary extends React.Component<{ children: React.ReactNode }, { failed: boolean }> {
15841584
state = { failed: false };
15851585
static getDerivedStateFromError() {
@@ -1589,29 +1589,54 @@ describe('GridField — the lazy face the package exports (objectstack#36102)',
15891589
return this.state.failed ? <div data-testid="grid-load-failed" /> : this.props.children;
15901590
}
15911591
}
1592-
vi.resetModules();
1593-
let failNextLoad = true;
1594-
vi.doMock('./grid-field/GridFieldWidget', async (importOriginal) => {
1595-
if (failNextLoad) {
1596-
failNextLoad = false;
1597-
throw new Error('Failed to fetch dynamically imported module: fields-grid');
1598-
}
1599-
return importOriginal();
1600-
});
1601-
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
1602-
try {
1603-
const face = await import('./GridField');
1604-
const first = render(<Boundary><face.GridField value={lines} onChange={() => {}} field={field} /></Boundary>);
1592+
1593+
/** Fail the next load of the body once, then let it through; restored by the returned call. */
1594+
function failTheNextBodyLoad(): () => void {
1595+
vi.resetModules();
1596+
let failNextLoad = true;
1597+
vi.doMock('./grid-field/GridFieldWidget', async (importOriginal) => {
1598+
if (failNextLoad) {
1599+
failNextLoad = false;
1600+
throw new Error('Failed to fetch dynamically imported module: fields-grid');
1601+
}
1602+
return importOriginal();
1603+
});
1604+
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
1605+
return () => {
1606+
quiet.mockRestore();
1607+
vi.doUnmock('./grid-field/GridFieldWidget');
1608+
};
1609+
}
1610+
1611+
async function expectFailedThenRecovered(Grid: React.ComponentType<React.ComponentProps<typeof GridFieldFace>>): Promise<void> {
1612+
const first = render(<Boundary><React.Suspense fallback={null}><Grid value={lines} onChange={() => {}} field={field} /></React.Suspense></Boundary>);
16051613
await screen.findByTestId('grid-load-failed');
16061614
first.unmount();
16071615

1608-
render(<Boundary><face.GridField value={lines} onChange={() => {}} field={field} /></Boundary>);
1616+
render(<Boundary><React.Suspense fallback={null}><Grid value={lines} onChange={() => {}} field={field} /></React.Suspense></Boundary>);
16091617
const cell = (await screen.findByTestId('line-items')).querySelector('input[aria-label="Description"]') as HTMLInputElement;
16101618
expect(cell.value).toBe('Widget');
16111619
expect(screen.queryByTestId('grid-load-failed')).toBeNull();
1612-
} finally {
1613-
quiet.mockRestore();
1614-
vi.doUnmock('./grid-field/GridFieldWidget');
16151620
}
1621+
1622+
it('a later mount of GridField renders the grid once the body is reachable, with no reload', async () => {
1623+
const restore = failTheNextBodyLoad();
1624+
try {
1625+
const face = await import('./GridField');
1626+
await expectFailedThenRecovered(face.GridField);
1627+
} finally {
1628+
restore();
1629+
}
1630+
});
1631+
1632+
it('a later mount of the grid form-field widget renders the grid too', async () => {
1633+
const restore = failTheNextBodyLoad();
1634+
try {
1635+
const fields = await import('../index');
1636+
await expectFailedThenRecovered(fields.getLazyFieldWidget('grid'));
1637+
} finally {
1638+
restore();
1639+
}
1640+
});
16161641
});
16171642
});

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,12 @@ 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, {@link
426-
* preloadGridField} and the `grid` form-field loader in `../index.tsx` all go
427-
* through it, so the body is requested once and a mount after any of them has
428-
* resolved renders without a boundary. A failed load is forgotten, so the next
429-
* caller asks again rather than inheriting the failure.
425+
* The ONE loader of the grid's body: the lazy face below and {@link
426+
* preloadGridField} go through it (and the `grid` form-field loader in
427+
* `../index.tsx` reaches it through the face), so the body is requested once
428+
* and a mount after any of them has resolved renders without a boundary. A
429+
* failed load is forgotten, so the next caller asks again rather than
430+
* inheriting the failure.
430431
*/
431432
export function loadGridFieldWidget(): Promise<GridFieldWidgetModule> {
432433
gridFieldWidgetLoad ??= import('./grid-field/GridFieldWidget.js').then(
@@ -458,9 +459,27 @@ export function preloadGridField(): Promise<void> {
458459
);
459460
}
460461

461-
const LazyGridFieldWidget = React.lazy(() =>
462-
loadGridFieldWidget().then((loaded) => ({ default: loaded.GridFieldWidget })),
463-
);
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+
}
464483

465484
/**
466485
* What a grid mounted before its body arrived shows meanwhile: a box of the
@@ -481,16 +500,18 @@ function GridFieldLoading(): React.ReactElement {
481500
*/
482501
export function GridField(props: GridFieldWidgetProps): React.ReactElement {
483502
// Decided once per mount: switching from the boundary to the direct render
484-
// when the load finishes would remount the body and drop the focused cell.
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.
485505
const [loadedAtMount] = React.useState(() => gridFieldWidgetModule);
506+
const [LazyAtMount] = React.useState(() => LazyGridFieldWidget);
486507
if (loadedAtMount) {
487508
const { GridFieldWidget } = loadedAtMount;
488509
return React.createElement(GridFieldWidget, props);
489510
}
490511
return React.createElement(
491512
React.Suspense,
492513
{ fallback: React.createElement(GridFieldLoading) },
493-
React.createElement(LazyGridFieldWidget, props),
514+
React.createElement(LazyAtMount, props),
494515
);
495516
}
496517

0 commit comments

Comments
 (0)