Skip to content

Commit

Permalink
fix: make Tutor-MFE config compatible with React Router v6
Browse files Browse the repository at this point in the history
As part of the React Router v5 -> v6 upgrade, history was no longer
being passed in. This broke the Tutor deployment method of MFEs, which
puts all the MFEs in a common server and uses different top-level dirs
to separate them.

The fix for this is to pass in an explicit basename to the Router.
  • Loading branch information
ormsbee committed Oct 2, 2023
1 parent 8182d21 commit 6f080b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ export const history = (typeof window !== 'undefined')
basename: getPath(getConfig().PUBLIC_PATH),
}) : createMemoryHistory();

/**
* The string basename that is the root directory of this MFE.
*
* In devstack, this should always just return an empty string, because each MFE is in its own
* server/domain.
*
* In Tutor, all MFEs are deployed to a common server, each under a different top-level directory.
* The basename is the root path for a given MFE, e.g. "/library-authoring". It is set by tutor-mfe
* as an ENV variable in the Docker file, and we read it here from that configuration so that it
* can be passed into a Router later.
*
* By convention, our basenames do not have a trailing slash. So it's "/library-authoring", and not
* "/library-authoring/". Our configuration is inconsistent on this, so this function always strips
* off the trailing slash from any value entered in configuration.
*/
export function basename() {
const configBasename = getPath(getConfig().PUBLIC_PATH);
return configBasename.endsWith('/') ? configBasename.slice(0, -1) : configBasename;
}

/**
* The default handler for the initialization lifecycle's `initError` phase. Logs the error to the
* LoggingService using `logError`
Expand Down
3 changes: 2 additions & 1 deletion src/react/AppProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IntlProvider,
LOCALE_CHANGED,
} from '../i18n';
import { basename } from '../initialize';

/**
* A wrapper component for React-based micro-frontends to initialize a number of common data/
Expand Down Expand Up @@ -72,7 +73,7 @@ export default function AppProvider({ store, children, wrapWithRouter }) {
>
<OptionalReduxProvider store={store}>
{wrapWithRouter ? (
<Router>
<Router basename={basename()}>
{children}
</Router>
) : children}
Expand Down

0 comments on commit 6f080b2

Please sign in to comment.