Skip to content

Commit fd6924b

Browse files
rinsukirolinh
authored andcommitted
fix: 404 Not Found error when baseUrl is not /
1 parent a06e19b commit fd6924b

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ declare global {
2121

2222
const buildAPIUrl = (env: Environment): string => {
2323
if (!env.isDev) {
24-
return `${document.location.origin}/api/`;
24+
const fallbackAPIUrl = `${document.location.origin}/api/`;
25+
try {
26+
const base = document.querySelector('base')?.href;
27+
if (base != null) return new URL('api/', base).href;
28+
} catch (e) {
29+
console.error('Failed to determine API path', e);
30+
}
31+
return fallbackAPIUrl;
2532
}
2633

2734
// NOTE: Do not edit those `process.env.VAR_NAME` variable accesses

src/router/Routes.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,39 @@ export type Props = {
1010
RootComponent: JSX.Element;
1111
};
1212

13+
const pathname = (href: string | undefined) => {
14+
if (href == null) return undefined;
15+
try {
16+
return new URL(href).pathname;
17+
} catch (e) {
18+
console.error('Failed to determine base path', e);
19+
return undefined;
20+
}
21+
};
22+
1323
export const Routes = function Routes(props: Props) {
1424
const createRouter = props.router.isInMemory ? createMemoryRouter : createBrowserRouter;
25+
const opts = props.router.isInMemory
26+
? undefined
27+
: {
28+
basename: pathname(document.querySelector('base')?.href),
29+
};
1530

16-
const router = createRouter([
17-
{
18-
path: '/',
19-
element: props.RootComponent,
20-
children: [
21-
{
22-
path: ApplicationPath.ServiceMap,
23-
element: <ServiceMapApp />,
24-
},
25-
],
26-
},
27-
]);
31+
const router = createRouter(
32+
[
33+
{
34+
path: '/',
35+
element: props.RootComponent,
36+
children: [
37+
{
38+
path: ApplicationPath.ServiceMap,
39+
element: <ServiceMapApp />,
40+
},
41+
],
42+
},
43+
],
44+
opts,
45+
);
2846

2947
return <RouterProvider router={router} />;
3048
};

0 commit comments

Comments
 (0)