An open source design system that's fully customizable and agent ready
TypeScript
refactor(DropdownMenu): a failed sheet preload is the preload's result, not an empty catch (#9069)
Christopher Chedeau3a25c59c0efd
diff --git a/.changeset/menu-sheet-preload-result.md b/.changeset/menu-sheet-preload-result.mdnew file mode 100644index 0000000000..05cd015f6e--- /dev/null+++ b/.changeset/menu-sheet-preload-result.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[chore] A bottom-sheet DropdownMenu's internal sheet preload reports a failed load in its result, with the loader's error, instead of catching it with an empty handler. Nothing changes for callers: a failed preload still raises no unhandled rejection, and the next open loads the sheet's actions.++@vjeuxdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex dc6ff5e6e4..a41b0cc377 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -651,9 +651,10 @@ function DropdownMenuBottomSheet({}}// The closed sheet renders nothing, so load its rows at rest: the first- // open shows the actions at once.+ // open shows the actions at once. The preload's result says whether they+ // loaded; after a failure the first open loads them.useEffect(() => {- preloadMenuBottomSheetActionList();+ void preloadMenuBottomSheetActionList();}, []);const currentSubmenu = submenuPath.at(-1);const currentItems = currentSubmenu?.items ?? items;diff --git a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.tsindex a35834445d..c6075631c8 100644--- a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts+++ b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts@@ -4,8 +4,9 @@* @file lazyMenuBottomSheet.test.ts* @input lazyMenuBottomSheet's action-list preload, with the list's module* failing to load once- * @output A failed preload is no unhandled rejection, and is not kept: the- * next load asks the module loader again+ * @output A failed preload is the preload's result, with the loader's error,+ * never an unhandled rejection, and is not kept: the next load asks the+ * module loader again* @position Unit test of ./lazyMenuBottomSheet; the hydration case is in* DropdownMenu.ssr.test.tsx*/@@ -18,7 +19,7 @@ afterEach(() => {});describe('preloadMenuBottomSheetActionList', () => {- it('drops a failed preload without an unhandled rejection, and asks the module loader again on the next call', async () => {+ it('reports a failed preload in its result, never as an unhandled rejection, and asks the module loader again on the next call', async () => {vi.resetModules();let attempts = 0;vi.doMock('./MenuBottomSheetActionList', () => {@@ -34,18 +35,24 @@ describe('preloadMenuBottomSheetActionList', () => {try {const {preloadMenuBottomSheetActionList} =await import('./lazyMenuBottomSheet');- preloadMenuBottomSheetActionList();- await vi.waitFor(() => expect(attempts).toBe(1));+ const failed = await preloadMenuBottomSheetActionList();+ expect(attempts).toBe(1);+ // The failure is the preload's result, with the loader's error.+ expect(failed.isLoaded).toBe(false);+ expect(failed.isLoaded === false ? failed.error : null).toBeInstanceOf(+ Error,+ );// An unhandled rejection is reported after the rejecting task's// microtasks: give it two macrotasks.await new Promise(resolve => setTimeout(resolve, 0));await new Promise(resolve => setTimeout(resolve, 0));expect(unhandled).toEqual([]);- preloadMenuBottomSheetActionList();- await vi.waitFor(() => expect(attempts).toBe(2));- const loaded = await import('./MenuBottomSheetActionList');- expect(typeof loaded.MenuBottomSheetActionList).toBe('function');+ // The failed load was not kept: the next call asks the loader again.+ expect(await preloadMenuBottomSheetActionList()).toEqual({+ isLoaded: true,+ });+ expect(attempts).toBe(2);} finally {process.off('unhandledRejection', onUnhandled);}diff --git a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.tsindex 5b3471d0d9..00971419ad 100644--- a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts+++ b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts@@ -15,10 +15,10 @@* of pages whose dropdowns only open as anchored popovers. Each wrapper keeps* the wrapped component's props and renders nothing until its module loads.*- * The action list preloads at rest; a failed load is not kept, so the next- * preload or open asks the module loader again (a loader that retries then- * loads it; a browser does not fetch a native module again once its import- * failed).+ * The action list preloads at rest, and the preload's result says whether it+ * loaded. A failed load is not kept, so the next preload or open asks the+ * module loader again (a loader that retries then loads it; a browser does not+ * fetch a native module again once its import failed).** Neither wrapper renders on the server or in the render that hydrates server* HTML (spec:AST-013 FR9): a lazy component that has not loaded yet would@@ -67,13 +67,25 @@ async function loadActionList(): Promise<typeof ActionListModule> {return actionListLoad;}+/** What a preload of the action list came to: loaded, or failed with the loader's error. */+export type ActionListPreload =+ | {readonly isLoaded: true}+ | {readonly isLoaded: false; readonly error: unknown};+/**- * Starts loading the action list; a sheet menu calls it at rest. A preload- * that fails is dropped, never an unhandled rejection: the first open asks the- * module loader again.+ * Starts loading the action list; a sheet menu calls it at rest. Its result+ * says whether the list loaded: a failure (a dropped connection) is that+ * result, with the loader's error, and never a rejection. The failed load is+ * not kept, so the first open asks the module loader again, and a load that+ * fails there reaches the menu through lazy().*/-export function preloadMenuBottomSheetActionList(): void {- loadActionList().catch(() => {});+export async function preloadMenuBottomSheetActionList(): Promise<ActionListPreload> {+ try {+ await loadActionList();+ return {isLoaded: true};+ } catch (error) {+ return {isLoaded: false, error};+ }}const LazyMenuBottomSheetActionList = lazy(async () =>