Repository navigation
Storybook: Handle production module load failures #81867
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| const PRELOAD_ERROR_RELOAD_ATTEMPT_KEY = | ||
| 'storybook-preload-error-reload-attempt'; | ||
|
|
||
| export const PRELOAD_ERROR_RELOAD_GUARD_INTERVAL = 60_000; | ||
|
|
||
| type PreloadErrorRecoveryOptions = { | ||
| now?: number; | ||
| reload?: () => void; | ||
| storage?: Pick< Storage, 'getItem' | 'setItem' >; | ||
| }; | ||
|
|
||
| export function handlePreloadError( | ||
| event: Pick< Event, 'preventDefault' >, | ||
| { | ||
| now = Date.now(), | ||
| reload = () => window.top?.location.reload(), | ||
| storage = window.sessionStorage, | ||
| }: PreloadErrorRecoveryOptions = {} | ||
| ) { | ||
| let lastReloadAttempt; | ||
| try { | ||
| const storedReloadAttempt = storage.getItem( | ||
| PRELOAD_ERROR_RELOAD_ATTEMPT_KEY | ||
| ); | ||
| lastReloadAttempt = | ||
| storedReloadAttempt === null ? null : Number( storedReloadAttempt ); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| lastReloadAttempt !== null && | ||
| Number.isFinite( lastReloadAttempt ) && | ||
| now - lastReloadAttempt < PRELOAD_ERROR_RELOAD_GUARD_INTERVAL | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| storage.setItem( PRELOAD_ERROR_RELOAD_ATTEMPT_KEY, String( now ) ); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| reload(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { | ||
| PRELOAD_ERROR_RELOAD_GUARD_INTERVAL, | ||
| handlePreloadError, | ||
| } from '../load-error-recovery'; | ||
|
|
||
| function createStorage( | ||
| reloadAttempt?: string | ||
| ): Pick< Storage, 'getItem' | 'setItem' > { | ||
| const values = new Map< string, string >(); | ||
| if ( reloadAttempt !== undefined ) { | ||
| values.set( 'storybook-preload-error-reload-attempt', reloadAttempt ); | ||
| } | ||
|
|
||
| return { | ||
| getItem: jest.fn( ( key: string ) => values.get( key ) ?? null ), | ||
| setItem: jest.fn( ( key: string, value: string ) => { | ||
| values.set( key, value ); | ||
| } ), | ||
| }; | ||
| } | ||
|
|
||
| describe( 'handlePreloadError', () => { | ||
| it( 'reloads the full Storybook page after the first preload error', () => { | ||
| const event = { preventDefault: jest.fn() }; | ||
| const storage = createStorage(); | ||
| const reload = jest.fn(); | ||
|
|
||
| handlePreloadError( event, { | ||
| now: 100_000, | ||
| reload, | ||
| storage, | ||
| } ); | ||
|
|
||
| expect( storage.setItem ).toHaveBeenCalledWith( | ||
| 'storybook-preload-error-reload-attempt', | ||
| '100000' | ||
| ); | ||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( reload ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
|
|
||
| it( 'lets a repeated preload error surface instead of reloading again', () => { | ||
| const event = { preventDefault: jest.fn() }; | ||
| const storage = createStorage( '100000' ); | ||
| const reload = jest.fn(); | ||
|
|
||
| handlePreloadError( event, { | ||
| now: 100_001, | ||
| reload, | ||
| storage, | ||
| } ); | ||
|
|
||
| expect( storage.setItem ).not.toHaveBeenCalled(); | ||
| expect( event.preventDefault ).not.toHaveBeenCalled(); | ||
| expect( reload ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'allows another recovery attempt after the guard interval', () => { | ||
| const event = { preventDefault: jest.fn() }; | ||
| const storage = createStorage( '100000' ); | ||
| const reload = jest.fn(); | ||
|
|
||
| handlePreloadError( event, { | ||
| now: 100_000 + PRELOAD_ERROR_RELOAD_GUARD_INTERVAL, | ||
| reload, | ||
| storage, | ||
| } ); | ||
|
|
||
| expect( storage.setItem ).toHaveBeenCalledWith( | ||
| 'storybook-preload-error-reload-attempt', | ||
| String( 100_000 + PRELOAD_ERROR_RELOAD_GUARD_INTERVAL ) | ||
| ); | ||
| expect( event.preventDefault ).toHaveBeenCalledTimes( 1 ); | ||
| expect( reload ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,8 @@ | |
| "compilerOptions": { | ||
| "jsx": "react-jsx", | ||
| "rootDir": ".", | ||
| "noEmit": true | ||
| "noEmit": true, | ||
| "types": [ "jest" ] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why I assumed it is already there but we don't have
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also noticed that the output there is not helpful, thus I created #81937 to address that in a separate PR. |
||
| }, | ||
| "include": [ "**/*.tsx", "**/*.ts", "**/*.mjs" ], | ||
| "exclude": [ "scripts/inject-legacy-docgen.mjs" ] | ||
|
|
||
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.
Could these files be
.ts?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.
Yes, I think it can be.
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.
converted to ts 👍