Skip to content

Commit

Permalink
fix retry import error
Browse files Browse the repository at this point in the history
  • Loading branch information
pasyukevich committed Jul 17, 2024
1 parent fc9b7ac commit a1b3496
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/libs/Navigation/AppNavigator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React, {lazy, memo, Suspense, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';
import lazyRetry from '@src/utils/lazyRetry';

const AuthScreens = lazy(() => import('./AuthScreens'));
const PublicScreens = lazy(() => import('./PublicScreens'));
const AuthScreens = lazy(() => lazyRetry(() => import('./AuthScreens')));

Check failure on line 7 in src/libs/Navigation/AppNavigator/index.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Type 'Promise<typeof import("/home/runner/work/App/App/src/libs/Navigation/AppNavigator/AuthScreens")>' is not assignable to type 'Promise<{ default: LazyExoticComponent<ComponentType>; }>'.
const PublicScreens = lazy(() => lazyRetry(() => import('./PublicScreens')));

Check failure on line 8 in src/libs/Navigation/AppNavigator/index.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Type 'Promise<typeof import("/home/runner/work/App/App/src/libs/Navigation/AppNavigator/PublicScreens")>' is not assignable to type 'Promise<{ default: LazyExoticComponent<ComponentType>; }>'.

type AppNavigatorProps = {
/** If we have an authToken this is true */
Expand Down
37 changes: 37 additions & 0 deletions src/utils/lazyRetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type {ComponentType, LazyExoticComponent} from 'react';

type ComponentImport = () => Promise<{default: LazyExoticComponent<ComponentType>}>;

/**
* Attempts to lazily import a React component with a retry mechanism on failure.
* If the initial import fails with a `ChunkLoadError`, the function will refresh the page once and retry the import.
* If the import fails again after the refresh, the error is propagated.
*
* @param componentImport - A function that returns a promise resolving to a lazily imported React component.
* @returns A promise that resolves to the imported component or rejects with an error after a retry attempt.
*/
const lazyRetry = function (componentImport: ComponentImport): Promise<{default: LazyExoticComponent<ComponentType>}> {
return new Promise((resolve, reject) => {
// Retrieve the retry status from sessionStorage, defaulting to 'false' if not set
const hasRefreshed: unknown = JSON.parse(sessionStorage.getItem('retry-lazy-refreshed') ?? 'false');

componentImport()
.then((component) => {
// Reset the retry status to 'false' on successful import
sessionStorage.setItem('retry-lazy-refreshed', 'false'); // success so reset the refresh
resolve(component);
})
.catch((error) => {
if (!hasRefreshed) {
// Set the retry status to 'true' and refresh the page
sessionStorage.setItem('retry-lazy-refreshed', 'true');
window.location.reload(); // Refresh the page to retry the import
} else {
// If the import fails again reject with the error to trigger default error handling
reject(error);
}
});
});
};

export default lazyRetry;

0 comments on commit a1b3496

Please sign in to comment.