Repository navigation
EDM-5267: Mitigate errors when UI service restarts #770
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
Open
celdrake
wants to merge
3
commits into
flightctl:main
Choose a base branch
from
celdrake:EDM-5267-mitigate-ui-restart-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| const RELOAD_ATTEMPTED_KEY = 'flightctl-chunk-reload-attempted'; | ||
|
|
||
| /** | ||
| * Returns true only the first time it is called within a tab, recording a marker in | ||
| * sessionStorage so the decision survives the full page reload it triggers. Every later call | ||
| * returns false, capping auto-recovery at a single reload per tab and preventing reload loops | ||
| * (several lazy components can fail on the same screen). If sessionStorage is unavailable we | ||
| * return false, erring towards the ErrorBoundary rather than risking a loop. | ||
| */ | ||
| export const shouldReloadOnce = (): boolean => { | ||
| try { | ||
| if (window.sessionStorage.getItem(RELOAD_ATTEMPTED_KEY)) { | ||
| return false; | ||
| } | ||
| window.sessionStorage.setItem(RELOAD_ATTEMPTED_KEY, 'true'); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Detects a chunk-load failure, i.e. the hashed bundle could not be fetched (404/504 after a | ||
| * redeploy). webpack throws an Error named "ChunkLoadError"; we also match the message to cover | ||
| * CSS chunks and webpack version differences. Any other rejection (e.g. an error thrown while | ||
| * the module evaluates) is a real bug that a reload would only hide, so it is left alone. | ||
| */ | ||
| const isChunkLoadError = (error: unknown): boolean => | ||
| error instanceof Error && (error.name === 'ChunkLoadError' || /Loading (CSS )?chunk .* failed/i.test(error.message)); | ||
|
|
||
| /** | ||
| * Wrapper around React.lazy that recovers from stale chunk-load failures after a UI | ||
| * redeployment. When the UI service restarts, the hashed bundles referenced by an already | ||
| * open tab no longer exist on the server, so the dynamic import() fails (404/504). React.lazy | ||
| * caches that rejection, which makes the error "sticky" across navigation. | ||
| * | ||
| * Reloading the same hashed URL cannot succeed (the file is gone); only a full page reload | ||
| * fetches a fresh index.html with the new chunk names. So on a chunk-load failure we trigger a | ||
| * single guarded full page reload. If the import still fails right after reloading, the error | ||
| * is rethrown and the ErrorBoundary shows its "Reload page" action instead of looping. | ||
| */ | ||
| export function lazyWithReload( | ||
| importer: () => Promise<{ default: React.ComponentType }>, | ||
| ): React.LazyExoticComponent<React.ComponentType> { | ||
| return React.lazy(() => | ||
| importer().catch((error: unknown) => { | ||
| // Check the error type before shouldReloadOnce() so non-chunk errors don't consume the | ||
| // one-shot reload marker. | ||
| if (isChunkLoadError(error) && shouldReloadOnce()) { | ||
| window.location.reload(); | ||
| // Keep <Suspense> in its loading state until the reload takes effect. | ||
| return new Promise<{ default: React.ComponentType }>(() => { | ||
| /* never resolves */ | ||
| }); | ||
| } | ||
| throw error; | ||
| }), | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.