Skip to content

Commit a08e27b

Browse files
authored
Merge pull request #127 from bcgsc/bugfix/DEVSU-1689-fix-routing-when-deployed-to-subfolder
Bugfix/devsu 1689 fix routing when deployed to subfolder
2 parents 271d269 + 6ce2841 commit a08e27b

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed

app/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ ModuleRegistry.registerModules([
104104
CsvExportModule,
105105
]);
106106

107+
/**
108+
* this is the subfolder (if any) that the application is deployed under
109+
* @note a properly formatted 'basename' cannot have a trailing slash
110+
* https://v5.reactrouter.com/web/api/BrowserRouter/basename-string
111+
*/
112+
const basename = window._env_.PUBLIC_PATH.endsWith('/') ? window._env_.PUBLIC_PATH.slice(0, -1) : window._env_.PUBLIC_PATH;
113+
107114
/**
108115
* Entry point to application. Handles routing, app theme, and logged in state.
109116
*/
@@ -115,7 +122,7 @@ function App() {
115122
<SnackbarProvider anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}>
116123
<SnackbarUtilsConfigurator />
117124
<CssBaseline />
118-
<BrowserRouter basename={window._env_.PUBLIC_PATH}>
125+
<BrowserRouter basename={basename}>
119126
<CacheBuster>
120127
<MainView />
121128
</CacheBuster>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import { screen, render } from '@testing-library/react';
3+
import { StaticRouter } from 'react-router-dom';
4+
import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
5+
import Sidebar from '..';
6+
import ResourceContext from '../../../context/ResourceContext';
7+
8+
const context = {};
9+
const permissions = {
10+
germlineAccess: true,
11+
reportAccess: true,
12+
adminAccess: true,
13+
};
14+
15+
// otherwise nothing will render due to the `Hidden` component
16+
// https://github.com/enzymejs/enzyme/issues/2179#issuecomment-529320192
17+
const theme = createMuiTheme({ props: { MuiWithWidth: { initialWidth: 'md' } } });
18+
19+
test('includes basename in links when provided', async () => {
20+
render(
21+
<MuiThemeProvider theme={theme}>
22+
<ResourceContext.Provider value={permissions}>
23+
<StaticRouter location="/ipr" context={context} basename="/ipr">
24+
<Sidebar />
25+
</StaticRouter>
26+
</ResourceContext.Provider>
27+
</MuiThemeProvider>,
28+
);
29+
30+
let link = (await screen.findByText('Reports')).closest('a');
31+
expect(link.href).toEqual(`${window.location.origin}/ipr/reports`);
32+
33+
link = (await screen.findByText('Germline')).closest('a');
34+
expect(link.href).toEqual(`${window.location.origin}/ipr/germline`);
35+
36+
link = (await screen.findByText('Users')).closest('a');
37+
expect(link.href).toEqual(`${window.location.origin}/ipr/admin/users`);
38+
});
39+
40+
test('has valid links when no basename provided', async () => {
41+
render(
42+
<MuiThemeProvider theme={theme}>
43+
<ResourceContext.Provider value={permissions}>
44+
<StaticRouter location="/" context={context} basename="">
45+
<Sidebar />
46+
</StaticRouter>
47+
</ResourceContext.Provider>
48+
</MuiThemeProvider>,
49+
);
50+
51+
let link = (await screen.findByText('Reports')).closest('a');
52+
expect(link.href).toEqual(`${window.location.origin}/reports`);
53+
54+
link = (await screen.findByText('Germline')).closest('a');
55+
expect(link.href).toEqual(`${window.location.origin}/germline`);
56+
57+
link = (await screen.findByText('Users')).closest('a');
58+
expect(link.href).toEqual(`${window.location.origin}/admin/users`);
59+
});

app/components/Sidebar/index.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React, { useContext, useCallback } from 'react';
2-
import { useLocation } from 'react-router-dom';
2+
import { Link, useLocation } from 'react-router-dom';
33
import {
44
Drawer,
55
Divider,
66
Hidden,
77
IconButton,
8-
Link,
98
List,
109
ListItem,
1110
Typography,
11+
Link as MuiLink,
1212
} from '@material-ui/core';
1313
import ChevronLeftIcon from '@material-ui/icons/ChevronLeft';
1414
import AssignmentIcon from '@material-ui/icons/Assignment';
@@ -51,7 +51,7 @@ const Sidebar = (): JSX.Element => {
5151
`}
5252
disableGutters
5353
>
54-
<Link className="sidebar__link" href="/reports">
54+
<Link className="sidebar__link" to="/reports">
5555
<AssignmentIcon color="action" />
5656
<Typography
5757
display="inline"
@@ -73,7 +73,7 @@ const Sidebar = (): JSX.Element => {
7373
`}
7474
disableGutters
7575
>
76-
<Link className="sidebar__link" href="/germline">
76+
<Link className="sidebar__link" to="/germline">
7777
<GermlineIcon className="sidebar__custom-icon" />
7878
<Typography
7979
display="inline"
@@ -88,7 +88,7 @@ const Sidebar = (): JSX.Element => {
8888
className="sidebar__list-item"
8989
disableGutters
9090
>
91-
<Link
91+
<MuiLink
9292
className="sidebar__link"
9393
href={window._env_.GRAPHKB_URL}
9494
rel="noopener noreferrer"
@@ -101,7 +101,7 @@ const Sidebar = (): JSX.Element => {
101101
>
102102
Graph Knowledgebase
103103
</Typography>
104-
</Link>
104+
</MuiLink>
105105
</ListItem>
106106
<ListItem
107107
className={`
@@ -110,7 +110,7 @@ const Sidebar = (): JSX.Element => {
110110
`}
111111
disableGutters
112112
>
113-
<Link className="sidebar__link" href="/terms">
113+
<Link className="sidebar__link" to="/terms">
114114
<HelpOutlineIcon color="action" />
115115
<Typography
116116
display="inline"
@@ -130,7 +130,7 @@ const Sidebar = (): JSX.Element => {
130130
`}
131131
disableGutters
132132
>
133-
<Link className="sidebar__link" href="/admin/users">
133+
<Link className="sidebar__link" to="/admin/users">
134134
<PersonIcon color="action" />
135135
<Typography
136136
display="inline"
@@ -147,7 +147,7 @@ const Sidebar = (): JSX.Element => {
147147
`}
148148
disableGutters
149149
>
150-
<Link className="sidebar__link" href="/admin/groups">
150+
<Link className="sidebar__link" to="/admin/groups">
151151
<PeopleIcon color="action" />
152152
<Typography
153153
display="inline"
@@ -164,7 +164,7 @@ const Sidebar = (): JSX.Element => {
164164
`}
165165
disableGutters
166166
>
167-
<Link className="sidebar__link" href="/admin/projects">
167+
<Link className="sidebar__link" to="/admin/projects">
168168
<FolderSharedIcon color="action" />
169169
<Typography
170170
display="inline"
@@ -181,7 +181,7 @@ const Sidebar = (): JSX.Element => {
181181
`}
182182
disableGutters
183183
>
184-
<Link className="sidebar__link" href="/template">
184+
<Link className="sidebar__link" to="/template">
185185
<DashboardIcon color="action" />
186186
<Typography
187187
display="inline"

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "ipr-client",
4-
"version": "6.6.2",
4+
"version": "6.6.3",
55
"keywords": [],
66
"license": "GPL-3.0",
77
"sideEffects": false,

0 commit comments

Comments
 (0)