-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Getting the react-router data router and federated modules work…
…ing! This lets the shell load federated modules and then use the “fog of war” API in react-router to add their routes to the router on demand. It works!
- Loading branch information
Showing
12 changed files
with
408 additions
and
272 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
import { Suspense } from 'react'; | ||
import { Outlet } from 'react-router'; | ||
import { | ||
AppProvider, | ||
PluginSlot | ||
} from '../runtime'; | ||
import Footer from './footer'; | ||
import ActiveHeader from './header/ActiveHeader'; | ||
|
||
export default function Shell() { | ||
// TODO: Plugin Slots for header/footer should be per type, i.e., default, learning, and studio slots. | ||
return ( | ||
<AppProvider> | ||
<PluginSlot id="org.openedx.frontend.shell.header.v1"> | ||
<ActiveHeader /> | ||
</PluginSlot> | ||
<Suspense fallback={<div>Loading</div>}> | ||
<Outlet /> | ||
</Suspense> | ||
<PluginSlot id="org.openedx.frontend.shell.footer.v1"> | ||
<Footer /> | ||
</PluginSlot> | ||
</AppProvider> | ||
); | ||
} |
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,2 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const SHELL_ID = 'shell'; |
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,41 @@ | ||
import { loadRemote } from '@module-federation/runtime'; | ||
import { getConfig } from '../../runtime'; | ||
import { | ||
AppConfig, AppConfigTypes, ApplicationModuleConfig, | ||
InternalAppConfig | ||
} from '../../types'; | ||
|
||
export function getFederatedModules() { | ||
const { apps } = getConfig(); | ||
|
||
return apps.filter((app: AppConfig) => app.type === AppConfigTypes.FEDERATED); | ||
} | ||
|
||
export function getFederationRemotes() { | ||
const federatedModules = getFederatedModules(); | ||
return federatedModules.map(app => ({ | ||
name: app.appId, | ||
entry: app.remoteUrl | ||
})); | ||
} | ||
|
||
export async function loadModuleConfig(module, scope) { | ||
let config:ApplicationModuleConfig | null = null; | ||
try { | ||
const loadedRemote = await loadRemote<{ default: ApplicationModuleConfig }>(`${scope}/${module}`); | ||
if (loadedRemote !== null) { | ||
config = loadedRemote.default; | ||
} | ||
} catch (error) { | ||
console.error(`Error loading remote module ${scope}/${module}:`, error); | ||
} | ||
return config; | ||
} | ||
|
||
export function getInternalModules(): Array<InternalAppConfig> { | ||
const { apps } = getConfig(); | ||
|
||
const internalModules = apps.filter((app: AppConfig) => app.type === AppConfigTypes.INTERNAL); | ||
|
||
return internalModules as Array<InternalAppConfig>; | ||
} |
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,16 @@ | ||
import { RouteObject } from 'react-router'; | ||
|
||
import { InternalAppConfig } from '../../types'; | ||
import { getInternalModules } from '../data/moduleUtils'; | ||
|
||
export default function createInternalRoutes() { | ||
const internalModules = getInternalModules(); | ||
|
||
let routes: Array<RouteObject> = []; | ||
|
||
internalModules.forEach((internalModule: InternalAppConfig) => { | ||
const moduleRoutes = internalModule.config.routes; | ||
routes = [...routes, ...moduleRoutes]; | ||
}); | ||
return routes; | ||
} |
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,20 @@ | ||
import { createBrowserRouter } from 'react-router-dom'; | ||
|
||
import { getBasename } from '../../runtime/initialize'; | ||
import { SHELL_ID } from '../data/constants'; | ||
import Shell from '../Shell'; | ||
import createInternalRoutes from './createInternalRoutes'; | ||
import patchRoutesOnNavigation from './patchRoutesOnNavigation'; | ||
|
||
export default function createRouter() { | ||
return createBrowserRouter([ | ||
{ | ||
id: SHELL_ID, | ||
Component: Shell, | ||
children: createInternalRoutes(), | ||
} | ||
], { | ||
basename: getBasename(), | ||
unstable_patchRoutesOnNavigation: patchRoutesOnNavigation, | ||
}); | ||
} |
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,24 @@ | ||
import { FederatedAppConfig } from '../../types'; | ||
import { SHELL_ID } from '../data/constants'; | ||
import { getFederatedModules, loadModuleConfig } from '../data/moduleUtils'; | ||
|
||
export default async function patchRoutesOnNavigation({ path, patch }) { | ||
const federatedModules = getFederatedModules(); | ||
let missingModule: FederatedAppConfig | null = null; | ||
for (let i = 0; i < federatedModules.length; i++) { | ||
const federatedModule = federatedModules[i]; | ||
if (path.startsWith(federatedModule.path)) { | ||
missingModule = federatedModule; | ||
break; | ||
} | ||
} | ||
|
||
if (missingModule) { | ||
const moduleConfig = await loadModuleConfig(missingModule.moduleId, missingModule.appId); | ||
if (moduleConfig) { | ||
patch(SHELL_ID, moduleConfig.routes); | ||
} else { | ||
console.log('uhoh, no module config.'); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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