-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @file Lazy load component with page reload on error. | ||
* It helps to avoid the situation when the user is stuck on the page with a broken chunk. | ||
*/ | ||
|
||
import { lazy } from 'react'; | ||
import { SessionStorageAPI } from '@/packages/sessionStorageAdapter'; | ||
|
||
type ComponentImportType = () => Promise<{ default: React.ComponentType }>; | ||
|
||
const sessionKey = 'lazyWithRetry'; | ||
|
||
/** | ||
* Lazy load component with page reload on error. | ||
* | ||
* @param componentImport - function that returns a promise with a component. | ||
* @param name - name of the component. | ||
* @returns - lazy loaded component. | ||
*/ | ||
const lazyWithRetry = (componentImport: ComponentImportType, name: string) => { | ||
return lazy(async () => { | ||
const hasRefreshed = SessionStorageAPI.getItem(`${sessionKey}-${name}`) || 'false'; | ||
|
||
try { | ||
SessionStorageAPI.setItem(`${sessionKey}-${name}`, 'false'); | ||
return await componentImport(); | ||
} catch (error) { | ||
if (hasRefreshed === 'false') { | ||
SessionStorageAPI.setItem(`${sessionKey}-${name}`, 'true'); | ||
window.location.reload(); | ||
} | ||
|
||
if (hasRefreshed === 'true') throw new Error('chunkLoadError', error); | ||
} | ||
return await componentImport(); | ||
}); | ||
}; | ||
|
||
export default lazyWithRetry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @file Session storage API. | ||
* @copyright Yury Korotovskikh <[email protected]> | ||
*/ | ||
|
||
import type { SessionStorageKey } from './SessionStorageKey'; | ||
|
||
/** | ||
* Session storage API. | ||
*/ | ||
class SessionStorage { | ||
/** | ||
* Get sesstionStorage item. | ||
* | ||
* @param ItemKey - Key. | ||
* @returns Item value. | ||
*/ | ||
public getItem<T>(ItemKey: SessionStorageKey): T | undefined { | ||
try { | ||
const serialisedValue = sessionStorage.getItem(ItemKey); | ||
if (serialisedValue === null) { | ||
return undefined; | ||
} | ||
return JSON.parse(serialisedValue); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Set sessionStorage item. | ||
* | ||
* @param ItemKey - Key. | ||
* @param ItemValue - Value. | ||
*/ | ||
public setItem<T>(ItemKey: SessionStorageKey, ItemValue: T): void { | ||
try { | ||
const serialisedValue = JSON.stringify(ItemValue); | ||
sessionStorage.setItem(ItemKey, serialisedValue); | ||
} catch { | ||
// Do nothing | ||
} | ||
} | ||
|
||
/** | ||
* Remove item from sessionStorage. | ||
* | ||
* @param ItemKey - Key. | ||
*/ | ||
public removeItem(ItemKey: SessionStorageKey): void { | ||
try { | ||
sessionStorage.removeItem(ItemKey); | ||
} catch { | ||
// Do nothing | ||
} | ||
} | ||
} | ||
|
||
export const SessionStorageAPI = new SessionStorage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @file Type declaration. | ||
* @copyright Yury Korotovskikh <[email protected]> | ||
*/ | ||
|
||
/** | ||
* Session storage avialiable keys. | ||
*/ | ||
export type SessionStorageKey = `lazyWithRetry-${string}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @file Index. | ||
* @copyright Yury Korotovskikh <[email protected]> | ||
*/ | ||
|
||
export { SessionStorageAPI } from './SessionStorage'; | ||
export type { SessionStorageKey } from './SessionStorageKey'; |