Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 2b6d8c3

Browse files
committed
Resolved router error on app start
1 parent 4ac024f commit 2b6d8c3

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"node-cache": "^5.1.2",
3636
"node-fetch": "^2.6.7",
3737
"normalize.css": "^8.0.1",
38-
"path-to-regexp": "^6.2.0",
3938
"pretty-error": "^4.0.0",
4039
"react": "^17.0.2",
4140
"react-dom": "^17.0.2",

src/js/components/AppRouter/config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const Home = loadable(() => import("../Home"));
99
const About = loadable(() => import("../About"));
1010
const DefaultPage = loadable(() => import("../DefaultPage"));
1111

12+
function Empty() {
13+
return null;
14+
}
15+
1216
const getRouteConfig = (name) => {
1317
switch (name) {
1418
case "home":
@@ -31,7 +35,7 @@ const getRouteConfig = (name) => {
3135
// return a 404 page
3236
console.error(`Route ${name} does not have a component.`);
3337
return {
34-
Component: null,
38+
Component: Empty,
3539
};
3640
}
3741
};

src/service/Router/router.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import NodeCache from "node-cache";
2-
import { match } from "path-to-regexp";
2+
import { matchPath } from "react-router";
33

44
import { toRoutes } from "./adapter";
55
import { get } from "./service";
@@ -13,24 +13,24 @@ const cache = new NodeCache({
1313
useClones: false,
1414
});
1515

16-
const refreshCache = async (key) => {
16+
const refreshCache = async (key) =>
1717
get(key)
1818
.then(({ lang, routes }) => {
1919
cache.set(lang, Object.freeze(routes));
20+
return routes;
2021
})
2122
.catch((e) => {
2223
console.error(e);
2324
return null;
2425
});
25-
};
2626

2727
const getData = async (key) => {
2828
const value = cache.get(key);
2929
// if no routing table is cached,
3030
// then it will retrieve a new routing table
3131
if (!value) {
32-
await refreshCache(key);
33-
return cache.get(key);
32+
const data = await refreshCache(key);
33+
return data;
3434
}
3535
// Otherwise, we retrieve the cached version
3636
return value;
@@ -43,7 +43,7 @@ const getRoutingTable = async () => {
4343

4444
const isValidPath = async (urlPath) => {
4545
const routes = await getRoutingTable();
46-
return routes.find((n) => match(n.path)(urlPath));
46+
return routes.find((n) => matchPath(n.path, urlPath));
4747
};
4848

4949
const getPath = async (docType) => {

0 commit comments

Comments
 (0)