@appigram/react-code-split-ssr 1.2.0-beta.5 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generate-routes.js +6 -6
- package/package.json +2 -2
- package/src/generate-routes.tsx +16 -17
package/lib/generate-routes.js
CHANGED
|
@@ -11,7 +11,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
11
11
|
import { matchPath, Route, Routes } from "react-router-dom";
|
|
12
12
|
const generateRoutes = (options = {
|
|
13
13
|
pathname: "/",
|
|
14
|
-
routes: []
|
|
14
|
+
routes: [],
|
|
15
15
|
}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
if (!Array.isArray(options.routes) || options.routes.length === 0) {
|
|
17
17
|
throw new Error("options.routes must be an non-empty array");
|
|
@@ -20,17 +20,17 @@ const generateRoutes = (options = {
|
|
|
20
20
|
const preloadedComp = preload === undefined
|
|
21
21
|
? yield options.notFoundComp().props.mod
|
|
22
22
|
: yield preload.element().props.mod;
|
|
23
|
-
const
|
|
23
|
+
const renderElement = (path, bundle) => {
|
|
24
24
|
if (!preloadedComp)
|
|
25
25
|
return bundle;
|
|
26
26
|
const isSSR = (preload && preload.path === path) || (!preload && !path);
|
|
27
|
-
|
|
27
|
+
const Element = isSSR ? preloadedComp.default : bundle;
|
|
28
|
+
return _jsx(Element, {}, void 0);
|
|
28
29
|
};
|
|
29
30
|
return () => {
|
|
30
31
|
return (_jsxs(Routes, { children: [options.routes.map((props, i) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}), _jsx(Route, { element: renderComp(null, options.notFoundComp) }, void 0)] }, void 0));
|
|
32
|
+
return (_jsx(Route, { path: props.path, element: renderElement(props.path, props.element()) }, i));
|
|
33
|
+
}), _jsx(Route, { element: renderElement(null, options.notFoundComp()) }, void 0)] }, void 0));
|
|
34
34
|
};
|
|
35
35
|
});
|
|
36
36
|
export default generateRoutes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appigram/react-code-split-ssr",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "React code splitting with SSR",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"tslint-react": "^5.0.0",
|
|
29
29
|
"typescript": "^4.4.4"
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"peerDependencies": {
|
|
32
32
|
"react": "^17.0.2",
|
|
33
33
|
"react-dom": "^17.0.2",
|
|
34
34
|
"react-router-dom": "^6.0.2"
|
package/src/generate-routes.tsx
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { ReactElement
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
2
|
import { matchPath, Route, Routes } from "react-router-dom";
|
|
3
|
-
// import Bundle from './bundle'
|
|
4
3
|
|
|
5
4
|
export interface IJSXModule {
|
|
6
|
-
default:
|
|
5
|
+
default: React.FC | React.ComponentClass;
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
export interface ISSRRoute {
|
|
10
9
|
caseSensitive?: boolean;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
element?: () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
|
|
12
|
+
index?: boolean;
|
|
13
|
+
path?: string;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
export interface IOptions {
|
|
@@ -23,7 +22,7 @@ export interface IOptions {
|
|
|
23
22
|
const generateRoutes = async (
|
|
24
23
|
options: IOptions = {
|
|
25
24
|
pathname: "/",
|
|
26
|
-
routes: []
|
|
25
|
+
routes: [],
|
|
27
26
|
}
|
|
28
27
|
): Promise<React.FC> => {
|
|
29
28
|
if (!Array.isArray(options.routes) || options.routes.length === 0) {
|
|
@@ -31,8 +30,7 @@ const generateRoutes = async (
|
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
const preload = options.routes.find(
|
|
34
|
-
(route) =>
|
|
35
|
-
!!matchPath(route.path, options.pathname)
|
|
33
|
+
(route) => !!matchPath(route.path, options.pathname)
|
|
36
34
|
);
|
|
37
35
|
|
|
38
36
|
const preloadedComp: any =
|
|
@@ -40,25 +38,26 @@ const generateRoutes = async (
|
|
|
40
38
|
? await options.notFoundComp().props.mod
|
|
41
39
|
: await preload.element().props.mod;
|
|
42
40
|
|
|
43
|
-
const
|
|
41
|
+
const renderElement = (path: string, bundle: ReactElement) => {
|
|
44
42
|
if (!preloadedComp) return bundle;
|
|
45
43
|
const isSSR = (preload && preload.path === path) || (!preload && !path);
|
|
46
|
-
|
|
44
|
+
const Element = isSSR ? preloadedComp.default : bundle;
|
|
45
|
+
return <Element />;
|
|
47
46
|
};
|
|
48
47
|
|
|
49
48
|
return () => {
|
|
50
49
|
return (
|
|
51
50
|
<Routes>
|
|
52
51
|
{options.routes.map((props, i) => {
|
|
53
|
-
const element = renderComp(props.path, props.element)
|
|
54
52
|
return (
|
|
55
53
|
<Route
|
|
56
54
|
key={i}
|
|
57
|
-
{
|
|
58
|
-
element={element}
|
|
55
|
+
path={props.path}
|
|
56
|
+
element={renderElement(props.path, props.element())}
|
|
59
57
|
/>
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
);
|
|
59
|
+
})}
|
|
60
|
+
<Route element={renderElement(null, options.notFoundComp())} />
|
|
62
61
|
</Routes>
|
|
63
62
|
);
|
|
64
63
|
};
|