Skip to content

Commit ec0300e

Browse files
committed
chore: enable frontend base url without slash at the end
1 parent bcad193 commit ec0300e

File tree

10 files changed

+44
-21
lines changed

10 files changed

+44
-21
lines changed

compose/env.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ MB3_FRONTEND_HOST=localhost
9191
MB3_FRONTEND_PORT=8080
9292

9393
# The base URL
94-
MB3_FRONTEND_BASE_URL=/MassBank/
94+
MB3_FRONTEND_BASE_URL=/MassBank
9595

9696
# The version of the frontend
9797
MB3_FRONTEND_VERSION=3.0.0 (beta)

web-frontend/server.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const backendUrl =
1919
? process.env.MB3_API_URL.replace(/\/$/, '')
2020
: 'http://localhost:8081/MassBank-api';
2121
const frontendUrl = process.env.MB3_FRONTEND_URL ?? 'http://localhost:8080';
22-
const frontendBaseUrl = process.env.MB3_FRONTEND_BASE_URL ?? '/MassBank3/';
22+
const frontendBaseUrl =
23+
process.env.MB3_FRONTEND_BASE_URL &&
24+
process.env.MB3_FRONTEND_BASE_URL.trim().length > 0
25+
? process.env.MB3_FRONTEND_BASE_URL.replace(/\/$/, '')
26+
: '/MassBank3';
2327
const exportServiceUrl =
2428
process.env.EXPORT_SERVICE_URL ?? 'http://localhost:8083';
2529
const version = process.env.MB3_VERSION ?? '0.4.0 (beta)';
@@ -135,17 +139,26 @@ async function getLastmodDate() {
135139
return lastmodDate;
136140
}
137141

142+
// Create router for redirecting to the frontend with base URL in case slash is missing
143+
const redirectRouter = express.Router();
144+
const regex = new RegExp(`^${frontendBaseUrl}$`);
145+
app.use(regex, redirectRouter);
146+
redirectRouter.get('', async (req: Request, res: Response) => {
147+
const redirectUrl = frontendUrl + frontendBaseUrl + '/';
148+
res.redirect(redirectUrl);
149+
});
150+
138151
// Create router for base URL
139152
const baseRouter = express.Router();
140-
app.use(frontendBaseUrl, baseRouter);
153+
app.use(frontendBaseUrl + '/', baseRouter);
141154

142155
const nRecords = 40000;
143156
const prefixUrl = frontendUrl + frontendBaseUrl;
144157

145158
// serve sitemap index for search engines
146159
baseRouter.get('/robots.txt', async (req: Request, res: Response) => {
147160
try {
148-
const content = `User-agent: *\nAllow: /\n\nSitemap: ${prefixUrl}sitemap.xml`;
161+
const content = `User-agent: *\nAllow: /\n\nSitemap: ${prefixUrl}/sitemap.xml`;
149162

150163
res.status(200).set({ 'Content-Type': 'text/plain' }).send(content);
151164
} catch (e) {
@@ -173,11 +186,11 @@ baseRouter.get('/sitemap.xml', async (req: Request, res: Response) => {
173186
const n = Math.ceil(hitsCount / nRecords);
174187
for (let i = 0; i < n; i++) {
175188
xmlContent.push(
176-
`<sitemap><loc>${prefixUrl}sitemap_${i}.xml</loc><lastmod>${lastmodDate}</lastmod></sitemap>`,
189+
`<sitemap><loc>${prefixUrl}/sitemap_${i}.xml</loc><lastmod>${lastmodDate}</lastmod></sitemap>`,
177190
);
178191
}
179192
xmlContent.push(
180-
`<sitemap><loc>${prefixUrl}sitemap_misc.xml</loc></sitemap>`,
193+
`<sitemap><loc>${prefixUrl}/sitemap_misc.xml</loc></sitemap>`,
181194
);
182195
xmlContent.push('</sitemapindex>');
183196
const xml = xmlFormat(xmlContent.join(''));
@@ -199,16 +212,16 @@ baseRouter.get('/sitemap_misc.xml', async (req: Request, res: Response) => {
199212
`<url><loc>${prefixUrl}</loc><changefreq>weekly</changefreq></url>`,
200213
);
201214
xmlContent.push(
202-
`<url><loc>${prefixUrl}search</loc><changefreq>weekly</changefreq></url>`,
215+
`<url><loc>${prefixUrl}/search</loc><changefreq>weekly</changefreq></url>`,
203216
);
204217
xmlContent.push(
205-
`<url><loc>${prefixUrl}content</loc><changefreq>weekly</changefreq></url>`,
218+
`<url><loc>${prefixUrl}/content</loc><changefreq>weekly</changefreq></url>`,
206219
);
207220
xmlContent.push(
208-
`<url><loc>${prefixUrl}news</loc><changefreq>weekly</changefreq></url>`,
221+
`<url><loc>${prefixUrl}/news</loc><changefreq>weekly</changefreq></url>`,
209222
);
210223
xmlContent.push(
211-
`<url><loc>${prefixUrl}about</loc><changefreq>weekly</changefreq></url>`,
224+
`<url><loc>${prefixUrl}/about</loc><changefreq>weekly</changefreq></url>`,
212225
);
213226

214227
xmlContent.push('</urlset>');
@@ -244,7 +257,7 @@ baseRouter.get(/\/sitemap_\d+\.xml/, async (req: Request, res: Response) => {
244257
const xmlContent: string[] = [xmlHeader];
245258
hits.slice(index * nRecords, (index + 1) * nRecords).forEach((hit) => {
246259
xmlContent.push(
247-
`<url><loc>${prefixUrl}RecordDisplay?id=${hit.accession}</loc><lastmod>${lastmodDate}</lastmod></url>`,
260+
`<url><loc>${prefixUrl}/RecordDisplay?id=${hit.accession}</loc><lastmod>${lastmodDate}</lastmod></url>`,
248261
);
249262
});
250263
xmlContent.push(xmlFooter);

web-frontend/src/elements/basic/Chart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ function Chart({
504504
const url =
505505
frontendUrl +
506506
baseUrl +
507+
'/' +
507508
routes.search.path +
508509
`?${searchParams.toString()}`;
509510

web-frontend/src/elements/common/AccessionSearchInputField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function AccessionSearchInputField({
3838
const handleOnClick = useCallback(
3939
() =>
4040
navigate({
41-
pathname: baseUrl + routes.accession.path,
41+
pathname: baseUrl + '/' + routes.accession.path,
4242
search: `?${createSearchParams({ id: accession })}`,
4343
}),
4444
[accession, baseUrl, navigate],

web-frontend/src/elements/header/Header.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import './Header.scss';
33
import routes from '../../constants/routes';
44
import { Button, Menu, MenuProps } from 'antd';
55
import { Header as HeaderAntD } from 'antd/es/layout/layout';
6-
import { Link, useLocation } from 'react-router-dom';
6+
import { useLocation } from 'react-router-dom';
77
import { usePropertiesContext } from '../../context/properties/properties';
88
import { CSSProperties, useMemo } from 'react';
99
import logo from '../../assets/logo.svg';
@@ -38,7 +38,7 @@ function Header({ height }: InputProps) {
3838
marginRight: 10,
3939
}}
4040
>
41-
<Link to={baseUrl} target="_self">
41+
<a href={baseUrl} target="_self">
4242
<img
4343
src={logo}
4444
alt="MassBank Europe"
@@ -50,7 +50,7 @@ function Header({ height }: InputProps) {
5050
padding: 5,
5151
}}
5252
/>
53-
</Link>
53+
</a>
5454
</Button>
5555
),
5656
};
@@ -67,7 +67,7 @@ function Header({ height }: InputProps) {
6767
route.id !== routes.accessionNext.id,
6868
)
6969
.map((route) => {
70-
const path = baseUrl + route.path;
70+
const path = baseUrl + '/' + route.path;
7171
return {
7272
key: path,
7373
label: (

web-frontend/src/elements/result/ResultLink.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ function ResultLink({ hit, width = '100%', height = '100%' }: InputProps) {
1717

1818
const url = useMemo(
1919
() =>
20-
frontendUrl + baseUrl + routes.accession.path + '?id=' + hit.accession,
20+
frontendUrl +
21+
baseUrl +
22+
'/' +
23+
routes.accession.path +
24+
'?id=' +
25+
hit.accession,
2126
[baseUrl, frontendUrl, hit.accession],
2227
);
2328

web-frontend/src/elements/routes/Routing.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Routing() {
1111
return (
1212
<Route
1313
key={'routing-key-' + route.id}
14-
path={baseUrl + route.path}
14+
path={baseUrl + '/' + route.path}
1515
element={<UserInterface body={<route.component />} />}
1616
/>
1717
);

web-frontend/src/elements/routes/pages/home/QuickSearch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function QuickSearch() {
2222
async (formData: SearchFields) => {
2323
const builtSearchParams = buildSearchParamsFromFormData(formData);
2424
navigate({
25-
pathname: baseUrl + routes.search.path,
25+
pathname: baseUrl + '/' + routes.search.path,
2626
search: `?${Object.keys(builtSearchParams).length > 0 ? createSearchParams(builtSearchParams) : createSearchParams({ plain: 'true' })}`,
2727
});
2828
},

web-frontend/src/elements/routes/pages/search/SearchView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function SearchView() {
149149
const builtSearchParams = buildSearchParamsFromFormData(formData);
150150

151151
navigate({
152-
pathname: baseUrl + routes.search.path,
152+
pathname: baseUrl + '/' + routes.search.path,
153153
search: `?${Object.keys(builtSearchParams).length > 0 ? createSearchParams(builtSearchParams) : createSearchParams({ plain: 'true' })}`,
154154
});
155155
} else {

web-frontend/src/utils/buildSearchUrl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ function buildSearchUrl(
99
const searchParams = new URLSearchParams();
1010
searchParams.set(label, value);
1111
const url =
12-
frontendUrl + baseUrl + routes.search.path + `?${searchParams.toString()}`;
12+
frontendUrl +
13+
baseUrl +
14+
'/' +
15+
routes.search.path +
16+
`?${searchParams.toString()}`;
1317

1418
return url;
1519
}

0 commit comments

Comments
 (0)