forked from openedx/frontend-base
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding loading for internal and federated apps
This adds code to load internal (‘direct’) and federated (module federation) apps based on code in the site config file. It reads the file and translates that into component loading in the shell. This needs to be married to a routing strategy that only loads the components when their route has been visited - at the moment, I just want to test that this works at all and so am loading everything into the ‘homepage’, so to speak.
- Loading branch information
Showing
2 changed files
with
136 additions
and
4 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,80 @@ | ||
import { loadRemote } from '@module-federation/runtime'; | ||
import { | ||
ComponentType, lazy, LazyExoticComponent, useEffect, useState | ||
} from 'react'; | ||
|
||
function loadComponent(scope, module) { | ||
return async (): Promise<{ default: ComponentType<any> }> => { | ||
// Initializes the share scope. This fills it with known provided modules from this build and all remotes | ||
const Module = await loadRemote<{ default: ComponentType<any> }>(`${scope}/${module.slice(2)}`); | ||
if (Module === null) { | ||
throw new Error('Unable to load module.'); | ||
} | ||
return Module; | ||
}; | ||
} | ||
|
||
const useDynamicScript = url => { | ||
const [ready, setReady] = useState(false); | ||
const [errorLoading, setErrorLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!url) { | ||
return () => {}; | ||
} | ||
|
||
setReady(false); | ||
setErrorLoading(false); | ||
|
||
const element = document.createElement('script'); | ||
|
||
element.src = url; | ||
element.type = 'text/javascript'; | ||
element.async = true; | ||
|
||
element.onload = () => { | ||
setReady(true); | ||
}; | ||
|
||
element.onerror = () => { | ||
setReady(false); | ||
setErrorLoading(true); | ||
}; | ||
|
||
document.head.appendChild(element); | ||
|
||
return () => { | ||
document.head.removeChild(element); | ||
}; | ||
}, [url]); | ||
|
||
return { | ||
errorLoading, | ||
ready, | ||
}; | ||
}; | ||
|
||
const useFederatedComponent = (remoteUrl, scope, module) => { | ||
// const key = `${remoteUrl}-${scope}-${module}`; | ||
const [Component, setComponent] = useState<LazyExoticComponent<ComponentType<any>> | null>(null); | ||
|
||
const { ready, errorLoading } = useDynamicScript(remoteUrl); | ||
useEffect(() => { | ||
// if (Component) { | ||
setComponent(null); | ||
// } | ||
// Only recalculate when key changes | ||
}, [remoteUrl, scope, module]); | ||
|
||
useEffect(() => { | ||
if (ready && !Component) { | ||
const loadedComponent = lazy(loadComponent(scope, module)); | ||
setComponent(loadedComponent); | ||
} | ||
// key includes all dependencies (scope/module) | ||
}, [Component, ready, remoteUrl, scope, module]); | ||
|
||
return { errorLoading, Component }; | ||
}; | ||
|
||
export default useFederatedComponent; |