Skip to content

Commit f9600ee

Browse files
committed
feat: a sane basename
This separates webpack's publicPath from react-router's basename. They have similar effects - and still need to be set in conjunction in dev mode (but not in a production build, where publicPath is set to 'auto') - but it is very confusing to give them the same name in configuration. Relatedly, this also removes support for `history` and its similarly confusing reliance on basename, which is not even supported on newer versions of the package, anyway. Consuming apps should be using `useNavigate()` instead.
1 parent c7c2842 commit f9600ee

19 files changed

+46
-307
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ Then move the files out of the way (move src to some other sub-dir, mostly) to m
7575
- csrfTokenApiPath: '/csrf/api/v1/token',
7676
- languagePreferenceCookieName: 'openedx-language-preference',
7777
- userInfoCookieName: 'edx-user-info',
78-
- publicPath: '/',
7978
- environment: 'production',
80-
- the `basename` and `history` exports have been replaced by function getters: `getBasename` and `getHistory`. This is because it may not be possible to determine the values of the original constants at code initialization time, since our config may arrive asynchronously. This ensures that anyone trying to get these values gets a current value.
79+
- the `basename` and export has been replaced by: `getBasename`. This is because it may not be possible to determine the values of the original constants at code initialization time, since our config may arrive asynchronously. This ensures that anyone trying to get these values gets a current value.
80+
- the `history` export no longer exists. Consumers should be using react-router 6's `useNavigate()` API instead.
8181
- When using MockAuthService, set the authenticated user by calling setAuthenticatedUser after instantiating the service. It's not okay for us to add arbitrary config values to the site config.
8282
- `REFRESH_ACCESS_TOKEN_ENDPOINT` has been replaced with `refreshAccessTokenApiPath`. It is now a path that defaults to '/login_refresh'. The Auth service assumes it is an endpoint on the LMS, and joins the path with `lmsBaseUrl`. This change creates more parity with other paths such as `csrfTokenApiPath`.
8383

docs/how_tos/migrate-frontend-app.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,16 @@ Optional config
499499

500500
Other configuration is now optional, and many values have been given sensible defaults. But these configuration variables are also available (as of this writing):
501501

502+
- environment: EnvironmentTypes
503+
- basename: string
504+
- mfeConfigApiUrl: string | null
502505
- accessTokenCookieName: string
503506
- languagePreferenceCookieName: string
504507
- userInfoCookieName: string
505508
- csrfTokenApiPath: string
506509
- refreshAccessTokenApiPath: string
507510
- ignoredErrorRegex: RegExp | null
508511
- segmentKey: string | null
509-
- environment: EnvironmentTypes
510-
- mfeConfigApiUrl: string | null
511-
- publicPath: string
512512

513513
URL Config changes
514514
------------------
@@ -573,7 +573,7 @@ Once you've verified your test suite still works, you should delete the `.env.te
573573
A sample `site.config.test.tsx` file:
574574

575575
```
576-
import { SiteConfig } from '@openedx/frontend-base';
576+
import { EnvironmentTypes, SiteConfig } from '@openedx/frontend-base';
577577
578578
const siteConfig: SiteConfig = {
579579
siteId: 'test',
@@ -582,7 +582,7 @@ const siteConfig: SiteConfig = {
582582
lmsBaseUrl: 'http://localhost:18000',
583583
loginUrl: 'http://localhost:18000/login',
584584
logoutUrl: 'http://localhost:18000/logout',
585-
environment: 'dev',
585+
environment: EnvironmentTypes.TEST,
586586
apps: [{
587587
appId: 'test-app',
588588
routes: [{
@@ -599,10 +599,9 @@ const siteConfig: SiteConfig = {
599599
csrfTokenApiPath: '/csrf/api/v1/token',
600600
languagePreferenceCookieName: 'openedx-language-preference',
601601
refreshAccessTokenApiPath: '/login_refresh',
602-
segmentKey: '',
603602
userInfoCookieName: 'edx-user-info',
604603
ignoredErrorRegex: null,
605-
publicPath: '/',
604+
segmentKey: '',
606605
};
607606
608607
export default siteConfig;

package-lock.json

Lines changed: 13 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"glob": "^7.2.3",
7777
"globals": "^15.11.0",
7878
"gradient-string": "^2.0.2",
79-
"history": "^4.10.1",
8079
"html-webpack-plugin": "5.6.0",
8180
"identity-obj-proxy": "3.0.0",
8281
"image-minimizer-webpack-plugin": "3.8.3",
@@ -112,7 +111,7 @@
112111
"ts-loader": "^9.5.1",
113112
"typescript": "^5.6.3",
114113
"typescript-eslint": "^8.11.0",
115-
"universal-cookie": "^4.0.4",
114+
"universal-cookie": "^8.0.1",
116115
"url-loader": "^4.1.1",
117116
"uuid": "^11.0.2",
118117
"webpack": "^5.97.1",
@@ -142,7 +141,7 @@
142141
"nodemon": "^3.1.4"
143142
},
144143
"peerDependencies": {
145-
"@openedx/paragon": "^22.20.1",
144+
"@openedx/paragon": "^22.20.2",
146145
"react": "^18.3.1",
147146
"react-dom": "^18.3.1",
148147
"react-redux": "^8.1.3",

runtime/config/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,13 @@ let siteConfig: SiteConfig = {
123123
apps: [],
124124
externalRoutes: [],
125125
externalLinkUrlOverrides: [],
126+
mfeConfigApiUrl: null,
126127
accessTokenCookieName: 'edx-jwt-cookie-header-payload',
127128
csrfTokenApiPath: '/csrf/api/v1/token',
128129
ignoredErrorRegex: null,
129130
languagePreferenceCookieName: 'openedx-language-preference',
130-
publicPath: '/',
131131
refreshAccessTokenApiPath: '/login_refresh',
132132
userInfoCookieName: 'edx-user-info',
133-
mfeConfigApiUrl: null,
134133
segmentKey: null,
135134
};
136135

runtime/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export {
7878
export {
7979
auth,
8080
getBasename,
81-
getHistory,
8281
initError,
8382
initialize
8483
} from './initialize';
@@ -125,11 +124,9 @@ export {
125124
export {
126125
camelCaseObject,
127126
convertKeyNames,
128-
getPath,
129127
getQueryParameters,
130128
isValidVariableName,
131129
modifyObjectKeys,
132-
parseURL,
133130
snakeCaseObject
134131
} from './utils';
135132

runtime/initialize.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
* @module Initialization
4848
*/
4949

50-
import {
51-
createBrowserHistory,
52-
createMemoryHistory
53-
} from 'history';
5450
/*
5551
This 'site.config' package is a special 'magic' alias in our webpack configuration in the `config`
5652
folder. It points at an `site.config.tsx` file in the root of a site's repository.
@@ -94,30 +90,15 @@ import {
9490
} from './logging';
9591
import { GoogleAnalyticsLoader } from './scripts';
9692
import { publish } from './subscriptions';
97-
import { getPath } from './utils';
9893

9994
/**
100-
* A browser history or memory history object created by the [history](https://github.com/ReactTraining/history)
101-
* package. Applications are encouraged to use this history object, rather than creating their own,
102-
* as behavior may be undefined when managing history via multiple mechanisms/instances. Note that
103-
* in environments where browser history may be inaccessible due to `window` being undefined, this
104-
* falls back to memory history.
105-
*/
106-
export const getHistory = () => ((typeof window !== 'undefined')
107-
? createBrowserHistory({ basename: getPath(getSiteConfig().publicPath) })
108-
: createMemoryHistory());
109-
110-
/**
111-
* The string basename that is the root directory of this MFE.
112-
*
113-
* In devstack, this should always just return "/", because each MFE is in its own server/domain.
95+
* If set in configuration, a basename will be prepended to all relative routes under BrowserRouter.
11496
*
115-
* In Tutor, all MFEs are deployed to a common server, each under a different top-level directory.
116-
* The basename is the root path for a given MFE, e.g. "/library-authoring". It is set by tutor-mfe
117-
* as an ENV variable in the Docker file, and we read it here from that configuration so that it
118-
* can be passed into a Router later.
97+
* Unlike webpack's publicPath, the basename cannot be auto-discovered, so when publicPath is set
98+
* (or when it's set to 'auto' and the site is being served from a path other than '/') this
99+
* needs to be configured.
119100
*/
120-
export const getBasename = () => getPath(getSiteConfig().publicPath);
101+
export const getBasename = () => getSiteConfig().basename;
121102

122103
/**
123104
* The default handler for the initialization lifecycle's `initError` phase. Logs the error to the

runtime/initialize.test.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { createBrowserHistory } from 'history';
2-
31
import {
42
SITE_ANALYTICS_INITIALIZED,
53
SITE_AUTH_INITIALIZED,
@@ -10,7 +8,7 @@ import {
108
SITE_PUBSUB_INITIALIZED,
119
SITE_READY,
1210
} from './constants';
13-
import { getHistory, initialize } from './initialize';
11+
import { initialize } from './initialize';
1412

1513
import { configureAnalytics, SegmentAnalyticsService } from './analytics';
1614
import {
@@ -38,7 +36,6 @@ jest.mock('./auth');
3836
jest.mock('./analytics');
3937
jest.mock('./i18n');
4038
jest.mock('./auth/LocalForageCache');
41-
jest.mock('history');
4239

4340
let config = null;
4441
const newConfig = {
@@ -357,13 +354,3 @@ describe('initialize', () => {
357354
});
358355
});
359356
});
360-
361-
describe('history', () => {
362-
it('browser history called by default path', async () => {
363-
getHistory();
364-
// import history from initialize;
365-
expect(createBrowserHistory).toHaveBeenCalledWith({
366-
basename: '/',
367-
});
368-
});
369-
});

runtime/site.config.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const siteConfig: SiteConfig = {
77
lmsBaseUrl: 'http://localhost:18000',
88
loginUrl: 'http://localhost:18000/login',
99
logoutUrl: 'http://localhost:18000/logout',
10+
1011
environment: EnvironmentTypes.TEST,
1112
apps: [{
1213
appId: 'test-app',
@@ -24,10 +25,9 @@ const siteConfig: SiteConfig = {
2425
csrfTokenApiPath: '/csrf/api/v1/token',
2526
languagePreferenceCookieName: 'openedx-language-preference',
2627
refreshAccessTokenApiPath: '/login_refresh',
27-
segmentKey: 'segment_whoa',
2828
userInfoCookieName: 'edx-user-info',
2929
ignoredErrorRegex: null,
30-
publicPath: '/'
30+
segmentKey: 'segment_whoa',
3131
};
3232

3333
export default siteConfig;

0 commit comments

Comments
 (0)