diff --git a/.changeset/36451-grid-field-lazy-retry.md b/.changeset/36451-grid-field-lazy-retry.md new file mode 100644 index 00000000000..88eb6184e65 --- /dev/null +++ b/.changeset/36451-grid-field-lazy-retry.md @@ -0,0 +1,12 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): a line-item grid whose first load failed loads again on its next mount, with no page reload + +Clause-②: no +Metadata-protocol: none + +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. + +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. diff --git a/ui/packages/fields/src/index.tsx b/ui/packages/fields/src/index.tsx index 46b299d2317..409e038efaf 100644 --- a/ui/packages/fields/src/index.tsx +++ b/ui/packages/fields/src/index.tsx @@ -36,7 +36,7 @@ import { deriveHexBadgePalette } from './hexBadgePalette.js'; import { formatNumberFieldValue, formatCurrency, formatPercentPoints, percentCellScale } from './widgets/number-format.js'; import { useFieldTranslation } from './widgets/useFieldTranslation.js'; import { useBooleanValueLabel } from './widgets/booleanValueLabel.js'; -import { loadGridFieldWidget } from './widgets/GridField.js'; +import { GridField } from './widgets/GridField.js'; import { RecordHoverCard, type RecordHoverCardObjectDef } from './RecordHoverCard.js'; // Module-level cache so multiple renderers fetching the same lookup ID @@ -4832,9 +4832,11 @@ const fieldWidgetMap = { // Complex data types 'object': () => import('./widgets/ObjectField.js').then(m => ({ default: m.ObjectField })), 'vector': () => import('./widgets/VectorField.js').then(m => ({ default: m.VectorField })), - // The grid's body, through its public face's one loader so a lazy `GridField` - // mounted after this resolved renders without a boundary (objectstack#36102). - 'grid': () => loadGridFieldWidget().then(m => ({ default: m.GridFieldWidget })), + // The grid's public face, already in this chunk, not its body: the face loads + // the body through its one loader (objectstack#36102) and asks again after a + // failed load, where a `React.lazy` wrapped around the body here would keep + // the failure until the page is reloaded (objectstack#36451). + 'grid': () => Promise.resolve({ default: GridField }), // Additional field types from @objectstack/spec 'color': () => import('./widgets/ColorField.js').then(m => ({ default: m.ColorField })), diff --git a/ui/packages/fields/src/widgets/GridField.test.tsx b/ui/packages/fields/src/widgets/GridField.test.tsx index 3a101598d76..e136d201b74 100644 --- a/ui/packages/fields/src/widgets/GridField.test.tsx +++ b/ui/packages/fields/src/widgets/GridField.test.tsx @@ -1575,4 +1575,68 @@ describe('GridField — the lazy face the package exports (objectstack#36102)', const cell = screen.getByTestId('line-items').querySelector('input[aria-label="Description"]') as HTMLInputElement; expect(cell.value).toBe('Widget'); }); + + // objectstack#36451 — a host that renders the grid without preloading it, + // whose first request for the body fails once (a network blip, a deploy that + // swapped chunk names), must not keep that failure for the whole session. + describe('after one failed load of the body', () => { + class Boundary extends React.Component<{ children: React.ReactNode }, { failed: boolean }> { + state = { failed: false }; + static getDerivedStateFromError() { + return { failed: true }; + } + render() { + return this.state.failed ?
: this.props.children; + } + } + + /** Fail the next load of the body once, then let it through; restored by the returned call. */ + function failTheNextBodyLoad(): () => void { + vi.resetModules(); + let failNextLoad = true; + vi.doMock('./grid-field/GridFieldWidget', async (importOriginal) => { + if (failNextLoad) { + failNextLoad = false; + throw new Error('Failed to fetch dynamically imported module: fields-grid'); + } + return importOriginal(); + }); + const quiet = vi.spyOn(console, 'error').mockImplementation(() => {}); + return () => { + quiet.mockRestore(); + vi.doUnmock('./grid-field/GridFieldWidget'); + }; + } + + async function expectFailedThenRecovered(Grid: React.ComponentType