Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(DropdownMenu): a failed sheet preload is handled and retried, never an unhandled rejection (#8478)

Christopher Chedeaufd0aba82b31b
diff --git a/.changeset/a-bottom-sheet-dropdownmenu-whose-sheet-preload--53fh.md b/.changeset/a-bottom-sheet-dropdownmenu-whose-sheet-preload--53fh.md
new file mode 100644
index 0000000000..e9d4790e48
--- /dev/null
+++ b/.changeset/a-bottom-sheet-dropdownmenu-whose-sheet-preload--53fh.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A bottom-sheet DropdownMenu whose sheet preload fails (a dropped connection) no longer reports an unhandled promise rejection, and no longer keeps the failed load, so a module loader that retries loads the sheet's actions on the next open. (#8478)
+@vjeux
diff --git a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts
new file mode 100644
index 0000000000..a35834445d
--- /dev/null
+++ b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts
@@ -0,0 +1,53 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @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
+ * @position Unit test of ./lazyMenuBottomSheet; the hydration case is in
+ * DropdownMenu.ssr.test.tsx
+ */
+
+import {afterEach, describe, expect, it, vi} from 'vitest';
+
+afterEach(() => {
+ vi.doUnmock('./MenuBottomSheetActionList');
+ vi.resetModules();
+});
+
+describe('preloadMenuBottomSheetActionList', () => {
+ it('drops a failed preload without an unhandled rejection, and asks the module loader again on the next call', async () => {
+ vi.resetModules();
+ let attempts = 0;
+ vi.doMock('./MenuBottomSheetActionList', () => {
+ attempts += 1;
+ if (attempts === 1) {
+ throw new Error('the connection dropped');
+ }
+ return {MenuBottomSheetActionList: () => null};
+ });
+ const unhandled: unknown[] = [];
+ const onUnhandled = (reason: unknown) => unhandled.push(reason);
+ process.on('unhandledRejection', onUnhandled);
+ try {
+ const {preloadMenuBottomSheetActionList} =
+ await import('./lazyMenuBottomSheet');
+ preloadMenuBottomSheetActionList();
+ await vi.waitFor(() => expect(attempts).toBe(1));
+ // 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');
+ } finally {
+ process.off('unhandledRejection', onUnhandled);
+ }
+ });
+});
diff --git a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts
index f8784d44d3..5b3471d0d9 100644
--- a/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts
+++ b/packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts
@@ -15,6 +15,11 @@
* 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).
+ *
* 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
* leave server markup incomplete and abort hydration. The closed sheet has no
@@ -47,16 +52,28 @@ const LazyMenuBottomSheet = lazy(async () =>
let actionListModule: typeof ActionListModule | null = null;
let actionListLoad: Promise<typeof ActionListModule> | null = null;
async function loadActionList(): Promise<typeof ActionListModule> {
- actionListLoad ??= import('./MenuBottomSheetActionList').then(module => {
- actionListModule = module;
- return module;
- });
+ actionListLoad ??= import('./MenuBottomSheetActionList').then(
+ module => {
+ actionListModule = module;
+ return module;
+ },
+ (error: unknown) => {
+ // A failed load (a dropped connection) is not kept: the next caller
+ // asks the module loader again.
+ actionListLoad = null;
+ throw error;
+ },
+ );
return actionListLoad;
}
-/** Starts loading the action list; a sheet menu calls it at rest. */
+/**
+ * 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.
+ */
export function preloadMenuBottomSheetActionList(): void {
- void loadActionList();
+ loadActionList().catch(() => {});
}
const LazyMenuBottomSheetActionList = lazy(async () =>