-
Notifications
You must be signed in to change notification settings - Fork 702
CONSOLE-5300: Parallelize context detection to reduce initial load time #16477
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
Merged
openshift-merge-bot
merged 2 commits into
openshift:main
from
logonoff:CONSOLE-5300-skeleton
May 22, 2026
Merged
Changes from all commits
Commits
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
179 changes: 179 additions & 0 deletions
179
frontend/packages/console-app/src/components/detect-context/DetectContext.tsx
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,179 @@ | ||
| import type { FC, Provider as ProviderComponent, ReactNode } from 'react'; | ||
| import { createContext, Suspense, useContext, useEffect } from 'react'; | ||
| import type { LoadedAndResolvedExtension } from '@openshift/dynamic-plugin-sdk'; | ||
| import { | ||
| Masthead, | ||
| MastheadContent, | ||
| MastheadMain, | ||
| Page, | ||
| PageSection, | ||
| PageSidebar, | ||
| PageSidebarBody, | ||
| } from '@patternfly/react-core'; | ||
| import { createPath, useLocation } from 'react-router'; | ||
| import type { Perspective, ReduxReducer, ContextProvider } from '@console/dynamic-plugin-sdk'; | ||
| import { | ||
| PerspectiveContext, | ||
| useResolvedExtensions, | ||
| isContextProvider, | ||
| isReduxReducer, | ||
| } from '@console/dynamic-plugin-sdk'; | ||
| import { applyReduxExtensions } from '@console/internal/redux'; | ||
| import { LoadingBox } from '@console/shared/src/components/loading/LoadingBox'; | ||
| import { usePerspectives } from '@console/shared/src/hooks/usePerspectives'; | ||
| import { useLanguage } from '../user-preferences/language/useLanguage'; | ||
| import { usePreferredLanguage } from '../user-preferences/language/usePreferredLanguage'; | ||
| import { NamespaceContext, useValuesForNamespaceContext } from './namespace'; | ||
| import PerspectiveDetector from './PerspectiveDetector'; | ||
| import { useValuesForPerspectiveContext } from './useValuesForPerspectiveContext'; | ||
|
|
||
| const getPerspectiveURLParam = (perspectives: Perspective[]) => { | ||
| const perspectiveIDs = perspectives.map( | ||
| (nextPerspective: Perspective) => nextPerspective.properties.id, | ||
| ); | ||
|
|
||
| const urlParams = new URLSearchParams(window.location.search); | ||
| const perspectiveParam = urlParams.get('perspective'); | ||
| return perspectiveParam && perspectiveIDs.includes(perspectiveParam) ? perspectiveParam : ''; | ||
| }; | ||
|
|
||
| const ContextProviderExtensionsContext = createContext< | ||
| LoadedAndResolvedExtension<ContextProvider>[] | ||
| >([]); | ||
|
|
||
| const EnhancedProvider: FC<{ | ||
| provider: ProviderComponent<any>; | ||
| useValueHook: () => any; | ||
| children: ReactNode; | ||
| }> = ({ provider: Component, useValueHook, children }) => { | ||
| const value = useValueHook(); | ||
| return <Component value={value}>{children}</Component>; | ||
| }; | ||
|
|
||
| const PF_BREAKPOINT_XL = 1200; | ||
|
|
||
| /** Empty PatternFly Page shell shown while DetectContext is initializing. */ | ||
| export const PageSkeleton: FC<{ blame: string }> = ({ blame }) => ( | ||
| <Page | ||
| isContentFilled | ||
| masthead={ | ||
| <Masthead> | ||
| <MastheadMain /> | ||
| <MastheadContent> | ||
| <div className="co-page-skeleton__masthead-spacer" /> | ||
| </MastheadContent> | ||
| </Masthead> | ||
| } | ||
| sidebar={ | ||
| <PageSidebar isSidebarOpen={window.innerWidth >= PF_BREAKPOINT_XL}> | ||
| <PageSidebarBody /> | ||
| </PageSidebar> | ||
| } | ||
| > | ||
| <PageSection isFilled hasBodyWrapper={false}> | ||
| <LoadingBox blame={blame} /> | ||
| </PageSection> | ||
| </Page> | ||
| ); | ||
|
|
||
| /** Wraps children in plugin-provided context providers resolved by DetectContext. */ | ||
| export const ContextProviderExtensionWrapper: FC<{ children: ReactNode }> = ({ children }) => { | ||
| const contextProviderExtensions = useContext(ContextProviderExtensionsContext); | ||
| return ( | ||
| <Suspense fallback={<PageSkeleton blame="ContextProviderExtensions" />}> | ||
| {contextProviderExtensions.reduce( | ||
| (acc, e) => ( | ||
| <EnhancedProvider key={e.uid} {...e.properties}> | ||
| {acc} | ||
| </EnhancedProvider> | ||
| ), | ||
| children, | ||
| )} | ||
| </Suspense> | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Bootstraps the console by running all detection and resolution hooks at the | ||
| * same component level so their async work executes in parallel: | ||
| * | ||
| * - Detect the active perspective (user prefs, URL param, or auto-detection) | ||
| * - Detect the active namespace (user prefs, URL, or K8s API fallback) | ||
| * - Detect the preferred language (user prefs, then apply via i18n) | ||
| * - Resolve all ReduxReducer and ContextProvider plugin extensions | ||
| * | ||
| * Once ready, provides the resolved values via PerspectiveContext, | ||
| * NamespaceContext, and ContextProviderExtensionsContext. | ||
| */ | ||
| export const DetectContext: FC<{ children: ReactNode }> = ({ children }) => { | ||
| const [ | ||
| activePerspective, | ||
| setActivePerspective, | ||
| perspectiveLoaded, | ||
| ] = useValuesForPerspectiveContext(); | ||
| const { namespace, setNamespace, loaded: namespaceLoaded } = useValuesForNamespaceContext(); | ||
|
|
||
| const [preferredLanguage, , preferredLanguageLoaded] = usePreferredLanguage(); | ||
| useLanguage(preferredLanguage, preferredLanguageLoaded); | ||
|
logonoff marked this conversation as resolved.
|
||
|
|
||
| const [reduxReducerExtensions, reducersResolved] = useResolvedExtensions<ReduxReducer>( | ||
| isReduxReducer, | ||
| ); | ||
| const [contextProviderExtensions, providersResolved] = useResolvedExtensions<ContextProvider>( | ||
| isContextProvider, | ||
| ); | ||
|
|
||
| const perspectiveExtensions = usePerspectives(); | ||
| const perspectiveParam = getPerspectiveURLParam(perspectiveExtensions); | ||
| const location = useLocation(); | ||
|
|
||
| useEffect(() => { | ||
| if (perspectiveParam && perspectiveParam !== activePerspective) { | ||
| setActivePerspective(perspectiveParam, createPath(location)); | ||
| } | ||
| }, [perspectiveParam, activePerspective, setActivePerspective, location]); | ||
|
|
||
| useEffect(() => { | ||
| if (reducersResolved) { | ||
| applyReduxExtensions(reduxReducerExtensions); | ||
| } | ||
| }, [reducersResolved, reduxReducerExtensions]); | ||
|
|
||
| const needsPerspectiveDetection = perspectiveLoaded && !activePerspective; | ||
| const ready = | ||
| perspectiveLoaded && | ||
| !!activePerspective && | ||
| namespaceLoaded && | ||
| reducersResolved && | ||
| providersResolved && | ||
| preferredLanguageLoaded; | ||
|
|
||
| if (!ready) { | ||
| const pending: string[] = []; | ||
| if (!perspectiveLoaded) pending.push('Perspective'); | ||
| if (needsPerspectiveDetection) pending.push('PerspectiveDetection'); | ||
| if (!namespaceLoaded) pending.push('Namespace'); | ||
| if (!reducersResolved) pending.push('Reducers'); | ||
| if (!providersResolved) pending.push('Providers'); | ||
| if (!preferredLanguageLoaded) pending.push('Language'); | ||
|
|
||
| return ( | ||
| <> | ||
| {needsPerspectiveDetection && ( | ||
| <PerspectiveDetector setActivePerspective={setActivePerspective} /> | ||
| )} | ||
| <PageSkeleton blame={pending.join(', ')} /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <PerspectiveContext.Provider value={{ activePerspective, setActivePerspective }}> | ||
| <NamespaceContext.Provider value={{ namespace, setNamespace }}> | ||
| <ContextProviderExtensionsContext.Provider value={contextProviderExtensions}> | ||
| {children} | ||
| </ContextProviderExtensionsContext.Provider> | ||
| </NamespaceContext.Provider> | ||
| </PerspectiveContext.Provider> | ||
| ); | ||
| }; | ||
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions
11
frontend/packages/console-app/src/components/detect-language/DetectLanguage.tsx
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
frontend/packages/console-app/src/components/detect-namespace/DetectNamespace.tsx
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
frontend/packages/console-app/src/components/detect-perspective/DetectPerspective.tsx
This file was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
frontend/packages/console-shared/src/hooks/useActiveNamespace.ts
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
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.