Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions storybook/load-error-recovery.ts

Copy link
Copy Markdown
Member

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 ?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

converted to ts 👍

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();
}
4 changes: 4 additions & 0 deletions storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ const config: StorybookConfig = {
},
],
build: {
// Let the browser discover JavaScript dependencies as it
// evaluates each module. Preloading the complete dependency
// graph creates hundreds of concurrent requests on first load.
modulePreload: false,
// Storybook's preview includes its shared runtime and all stories.
// Automatic chunk splitting is already enabled; 3.5 MB leaves a
// meaningful budget above the current 3.15 MB largest chunk.
Expand Down
7 changes: 7 additions & 0 deletions storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import { WithMaxWidthWrapper } from './decorators/with-max-width-wrapper';
import { WithRTL } from './decorators/with-rtl';
import { WithDesignSystemTheme } from './decorators/with-design-system-theme';
import { ComponentStatusIndicator } from './components/component-status-indicator';
import { handlePreloadError } from './load-error-recovery';
import theme from './theme';

if ( typeof window !== 'undefined' ) {
window.addEventListener( 'vite:preloadError', handlePreloadError, {
once: true,
} );
}

export const globalTypes = {
direction: {
name: 'RTL',
Expand Down
76 changes: 76 additions & 0 deletions storybook/test/load-error-recovery.test.ts
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 );
} );
} );
3 changes: 2 additions & 1 deletion storybook/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"compilerOptions": {
"jsx": "react-jsx",
"rootDir": ".",
"noEmit": true
"noEmit": true,
"types": [ "jest" ]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 @types/jest in storybook/package.json.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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" ]
Expand Down
Loading