-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppRouter.tsx
150 lines (140 loc) · 3.97 KB
/
AppRouter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { EventEmitter } from "eventemitter3";
import {
MicrofrontendContext,
MicrofrontendManifest,
ErrorBoundary,
} from "microfrontends";
import { useQuery } from "react-query";
import { Layout } from "./Layout/index.tsx";
const eventBus = new EventEmitter();
const Fallback = () => (
<div>Something went wrong. This is a Fallback component.</div>
);
const AppRoutes = ({ manifests }: { manifests: MicrofrontendManifest[] }) => {
const location = useLocation();
const navigate = useNavigate();
const homedir = `${process.env.PUBLIC_URL || ""}${location.pathname}`;
return (
<MicrofrontendContext.Provider
value={{
manifests,
navigate,
eventBus,
fetch,
homedir,
url: location.pathname,
user: { permissions: [] },
layout: {
navItems: [],
logout: () => {},
},
ErrorBoundary: (props) => (
<ErrorBoundary Fallback={Fallback} {...props} />
),
}}
>
<Routes>
<Route path="*" element={<Layout />} />
</Routes>
</MicrofrontendContext.Provider>
);
};
type MicrofrontendRoutesProps = {
getMicrofrontendManifests: (
overrides: string[]
) => Promise<MicrofrontendManifest[]>;
};
export const AppRouter = ({
getMicrofrontendManifests,
}: MicrofrontendRoutesProps) => {
const { data: manifests = [] } = useQuery("manifests", () =>
getMicrofrontendManifests([]).then((manifests) =>
overrideManifests(manifests)
)
);
return (
<BrowserRouter basename={process.env.PUBLIC_URL}>
<AppRoutes manifests={manifests} />
</BrowserRouter>
);
};
async function overrideManifests(
manifests: MicrofrontendManifest[]
): Promise<MicrofrontendManifest[]> {
const allowedDomains = [/mykeels.github.io$/, /localhost:\d{4,5}$/];
const { override_manifest = [] } = getSearchParams<"override_manifest">();
return await Promise.all(
override_manifest
.filter((url) => {
const isUrlAllowed = allowedDomains.some((rgx) => {
try {
return rgx.test(new URL(url).origin);
} catch {
return null;
}
});
if (!isUrlAllowed) {
console.warn(`URL is not allowed to override manifests.`, {
url,
allowedDomains,
});
}
return isUrlAllowed;
})
.filter(Boolean)
.map((url) =>
fetch(url)
.then((res) => res.json())
.then((data) => {
const urlParams = new URL(url);
return {
...data,
entry: `${urlParams.origin}${urlParams.pathname.replace(
"/microfrontend-manifest.json",
""
)}/remoteEntry.js`,
};
})
.catch((err) => {
console.error("Error fetching manifest", err);
return null;
})
)
)
.then((overrides: MicrofrontendManifest[]) => overrides.filter(Boolean))
.then((overrides) => {
return manifests
.map((manifest) => {
const override =
overrides.find((o) => o.scope === manifest.scope) || manifest;
return override;
})
.concat(
overrides.filter(
(o) => !manifests.map((m) => m.scope).includes(o.scope)
)
);
})
.then((manifests) => {
console.log(manifests);
return manifests;
});
}
function getSearchParams<TKeys extends string>(): {
[key in TKeys]: string[] | undefined;
} {
const params = new URLSearchParams(
location.search ??
location.hash.split("?")?.[location.hash.split("?").length - 1]
);
const entries = {} as Record<any, string[]>;
for (let key of Array.from(params.keys())) {
entries[key] = params.getAll(key);
}
return entries as {
[key in TKeys]: string[] | undefined;
};
}