-
Notifications
You must be signed in to change notification settings - Fork 97
refactor(react): Simplify onError and onSuccess handlers in lazy loading #1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/stable-lazy
Are you sure you want to change the base?
Changes from all commits
92edcc3
ac28989
73fcb86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | ||||||||||||||||||||||||||||||
| 'use client' | ||||||||||||||||||||||||||||||
| import { type ComponentType, type LazyExoticComponent, lazy as originalLazy } from 'react' | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const reloadOnErrorStorageKeySymbol = Symbol('reloadOnErrorStorageKey') | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| interface LazyOptions { | ||||||||||||||||||||||||||||||
| onSuccess?: ({ load }: { load: () => Promise<{ default: ComponentType<any> }> }) => void | ||||||||||||||||||||||||||||||
| onError?: ({ error, load }: { error: unknown; load: () => Promise<{ default: ComponentType<any> }> }) => void | ||||||||||||||||||||||||||||||
| onSuccess?: () => void | ||||||||||||||||||||||||||||||
| onError?: (options: { error: unknown }) => void | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
6
to
9
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
|
|
@@ -47,14 +49,29 @@ export const createLazy = | |||||||||||||||||||||||||||||
| ): LazyExoticComponent<T> & { | ||||||||||||||||||||||||||||||
| load: () => Promise<{ default: T }> | ||||||||||||||||||||||||||||||
| } => { | ||||||||||||||||||||||||||||||
| const storageKey = load.toString() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const composedOnSuccess = () => { | ||||||||||||||||||||||||||||||
| options?.onSuccess?.({ load }) | ||||||||||||||||||||||||||||||
| defaultOptions.onSuccess?.({ load }) | ||||||||||||||||||||||||||||||
| ;(options?.onSuccess as undefined | ((arg: { [reloadOnErrorStorageKeySymbol]: string }) => void))?.({ | ||||||||||||||||||||||||||||||
| [reloadOnErrorStorageKeySymbol]: storageKey, | ||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||
| ;(defaultOptions.onSuccess as undefined | ((arg: { [reloadOnErrorStorageKeySymbol]: string }) => void))?.({ | ||||||||||||||||||||||||||||||
| [reloadOnErrorStorageKeySymbol]: storageKey, | ||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const composedOnError = (error: unknown) => { | ||||||||||||||||||||||||||||||
| options?.onError?.({ error, load }) | ||||||||||||||||||||||||||||||
| defaultOptions.onError?.({ error, load }) | ||||||||||||||||||||||||||||||
| ;(options?.onError as undefined | ((arg: { error: unknown; [reloadOnErrorStorageKeySymbol]: string }) => void))?.( | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| error, | ||||||||||||||||||||||||||||||
| [reloadOnErrorStorageKeySymbol]: storageKey, | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| ;( | ||||||||||||||||||||||||||||||
| defaultOptions.onError as | ||||||||||||||||||||||||||||||
| | undefined | ||||||||||||||||||||||||||||||
| | ((arg: { error: unknown; [reloadOnErrorStorageKeySymbol]: string }) => void) | ||||||||||||||||||||||||||||||
| )?.({ error, [reloadOnErrorStorageKeySymbol]: storageKey }) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return Object.assign( | ||||||||||||||||||||||||||||||
|
|
@@ -188,14 +205,14 @@ export const reloadOnError = ({ | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||
| ...options, | ||||||||||||||||||||||||||||||
| onSuccess: ({ load }) => { | ||||||||||||||||||||||||||||||
| options.onSuccess?.({ load }) | ||||||||||||||||||||||||||||||
| reloadStorage.removeItem(load.toString()) | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| onError: ({ error, load }) => { | ||||||||||||||||||||||||||||||
| options.onError?.({ error, load }) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const storageKey = load.toString() | ||||||||||||||||||||||||||||||
| onSuccess: ((arg: any) => { | ||||||||||||||||||||||||||||||
| ;(options.onSuccess as any)?.(arg) | ||||||||||||||||||||||||||||||
| const storageKey = arg?.[reloadOnErrorStorageKeySymbol] as string | ||||||||||||||||||||||||||||||
| reloadStorage.removeItem(storageKey) | ||||||||||||||||||||||||||||||
| }) as LazyOptions['onSuccess'], | ||||||||||||||||||||||||||||||
|
Comment on lines
+208
to
+212
|
||||||||||||||||||||||||||||||
| onError: (errorOptions: { error: unknown }) => { | ||||||||||||||||||||||||||||||
| options.onError?.(errorOptions) | ||||||||||||||||||||||||||||||
| const storageKey = (errorOptions as any)[reloadOnErrorStorageKeySymbol] as string | ||||||||||||||||||||||||||||||
|
Comment on lines
+210
to
+215
|
||||||||||||||||||||||||||||||
| const storageKey = arg?.[reloadOnErrorStorageKeySymbol] as string | |
| reloadStorage.removeItem(storageKey) | |
| }) as LazyOptions['onSuccess'], | |
| onError: (errorOptions: { error: unknown }) => { | |
| options.onError?.(errorOptions) | |
| const storageKey = (errorOptions as any)[reloadOnErrorStorageKeySymbol] as string | |
| const storageKey = arg?.[reloadOnErrorStorageKeySymbol] | |
| if (typeof storageKey !== 'string') return | |
| reloadStorage.removeItem(storageKey) | |
| }) as LazyOptions['onSuccess'], | |
| onError: (errorOptions: { error: unknown }) => { | |
| options.onError?.(errorOptions) | |
| const storageKey = (errorOptions as any)[reloadOnErrorStorageKeySymbol] | |
| if (typeof storageKey !== 'string') return |
Copilot
AI
Apr 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reloadOnError's onError assumes errorOptions carries reloadOnErrorStorageKeySymbol; if it's missing, storageKey becomes undefined and getItem/setItem/removeItem will operate on the wrong key. Guard against a missing/invalid storageKey before using storage and consider making the key part of the typed onError context so this cannot happen silently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting reloadOnErrorStorageKeySymbol appears to be only for plumbing/testing and leaks an internal implementation detail into the public API surface. Prefer keeping this symbol internal (non-exported) and adjust tests to assert via behavior (storage interactions) or use partial matchers (e.g., objectContaining) rather than depending on a public symbol.