@appigram/react-code-split-ssr 1.2.0 → 1.2.4
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/README.md +4 -4
- package/lib/generate-routes.js +12 -12
- package/package.json +5 -5
- package/src/generate-routes.tsx +31 -33
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ React code splitting with server side rendering
|
|
|
3
3
|
|
|
4
4
|
## How it works
|
|
5
5
|
|
|
6
|
-
- Based on React Router
|
|
6
|
+
- Based on React Router 6
|
|
7
7
|
- Server side: Load all components synchronously, then render to string.
|
|
8
8
|
- Client side: Load the initial component before rendering, render the entire screen synchronously, then load the rest of routes asynchronously.
|
|
9
9
|
|
|
@@ -26,9 +26,6 @@ export const routes = [
|
|
|
26
26
|
{ exact: true, path: '/posts', component: Posts },
|
|
27
27
|
]
|
|
28
28
|
|
|
29
|
-
export const redirects = [
|
|
30
|
-
{ from: '/test', to: '/posts' }
|
|
31
|
-
]
|
|
32
29
|
```
|
|
33
30
|
Client:
|
|
34
31
|
```js
|
|
@@ -105,6 +102,9 @@ A function that returns a `Promise` object which can be resolved to React Router
|
|
|
105
102
|
- redirects {objects}[] - An array of `<Redirect>` props object
|
|
106
103
|
- notFoundComp {Component} - A React component for 404 Not Found, only accepts `() => <Bundle/>`
|
|
107
104
|
|
|
105
|
+
## v1.2.0
|
|
106
|
+
- [x] Remove redirects, Fix React-Router v6 specific rendering
|
|
107
|
+
|
|
108
108
|
## v1.1.0
|
|
109
109
|
- [x] Upgrade to React-Router v6
|
|
110
110
|
|
package/lib/generate-routes.js
CHANGED
|
@@ -7,28 +7,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
+
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");
|
|
18
18
|
}
|
|
19
19
|
const preload = options.routes.find((route) => !!matchPath(route.path, options.pathname));
|
|
20
|
-
const preloadedComp =
|
|
21
|
-
|
|
20
|
+
const preloadedComp = preload === undefined
|
|
21
|
+
? yield options.notFoundComp().props.mod
|
|
22
|
+
: yield preload.element().props.mod;
|
|
23
|
+
const renderElement = (path, bundle) => {
|
|
22
24
|
if (!preloadedComp)
|
|
23
25
|
return bundle;
|
|
24
|
-
const
|
|
25
|
-
const Element =
|
|
26
|
-
return _jsx(Element, {}, void 0);
|
|
27
|
-
};
|
|
28
|
-
return () => {
|
|
29
|
-
return (_jsx(Routes, { children: options.routes.map((props, i) => {
|
|
30
|
-
return (_jsx(Route, { path: props.path, element: renderElement({ path: props.path, bundle: props.element }) }, i));
|
|
31
|
-
}) }, void 0));
|
|
26
|
+
const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
|
|
27
|
+
const Element = isRouteMatched ? preloadedComp.default : bundle;
|
|
28
|
+
return isRouteMatched ? _jsx(Element, {}, void 0) : Element;
|
|
32
29
|
};
|
|
30
|
+
return (_jsxs(Routes, { children: [options.routes.map((props, i) => {
|
|
31
|
+
return (_jsx(Route, { path: props.path, element: renderElement(props.path, props.element) }, i));
|
|
32
|
+
}), _jsx(Route, { path: '*', element: renderElement(null, options.notFoundComp) }, 'nf')] }, void 0));
|
|
33
33
|
});
|
|
34
34
|
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.4",
|
|
4
4
|
"description": "React code splitting with SSR",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
"author": "Eugene Sysmanov",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/react": "^17.0.
|
|
25
|
+
"@types/react": "^17.0.37",
|
|
26
26
|
"@types/react-router-dom": "^5.3.2",
|
|
27
27
|
"tslint": "^6.1.3",
|
|
28
28
|
"tslint-react": "^5.0.0",
|
|
29
|
-
"typescript": "^4.
|
|
29
|
+
"typescript": "^4.5.4"
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"dependencies": {
|
|
32
32
|
"react": "^17.0.2",
|
|
33
33
|
"react-dom": "^17.0.2",
|
|
34
|
-
"react-router-dom": "^6.
|
|
34
|
+
"react-router-dom": "^6.1.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/generate-routes.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReactElement } from
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
2
|
import { matchPath, Route, Routes } from "react-router-dom";
|
|
3
3
|
|
|
4
4
|
export interface IJSXModule {
|
|
@@ -7,60 +7,58 @@ export interface IJSXModule {
|
|
|
7
7
|
|
|
8
8
|
export interface ISSRRoute {
|
|
9
9
|
caseSensitive?: boolean;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
element?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
|
|
12
|
+
index?: boolean;
|
|
13
|
+
path?: string;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface IOptions {
|
|
17
17
|
pathname: string;
|
|
18
18
|
routes: ISSRRoute[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface IRenderElement {
|
|
22
|
-
path: string;
|
|
23
|
-
bundle: ReactElement;
|
|
19
|
+
notFoundComp?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
const generateRoutes = async (
|
|
27
23
|
options: IOptions = {
|
|
28
24
|
pathname: "/",
|
|
29
|
-
routes: []
|
|
25
|
+
routes: [],
|
|
30
26
|
}
|
|
31
|
-
)
|
|
27
|
+
) => {
|
|
32
28
|
if (!Array.isArray(options.routes) || options.routes.length === 0) {
|
|
33
29
|
throw new Error("options.routes must be an non-empty array");
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
const preload = options.routes.find(
|
|
37
|
-
(route) =>
|
|
38
|
-
!!matchPath(route.path, options.pathname)
|
|
33
|
+
(route) => !!matchPath(route.path, options.pathname)
|
|
39
34
|
);
|
|
40
35
|
|
|
41
|
-
const preloadedComp: any =
|
|
36
|
+
const preloadedComp: any =
|
|
37
|
+
preload === undefined
|
|
38
|
+
? await options.notFoundComp().props.mod
|
|
39
|
+
: await preload.element().props.mod;
|
|
42
40
|
|
|
43
|
-
const renderElement = (
|
|
41
|
+
const renderElement = (path: string, bundle: ReactElement) => {
|
|
44
42
|
if (!preloadedComp) return bundle;
|
|
45
|
-
const
|
|
46
|
-
const Element =
|
|
47
|
-
return <Element />
|
|
43
|
+
const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
|
|
44
|
+
const Element = isRouteMatched ? preloadedComp.default : bundle;
|
|
45
|
+
return isRouteMatched ? <Element /> : Element
|
|
48
46
|
};
|
|
49
47
|
|
|
50
|
-
return (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
return (
|
|
49
|
+
<Routes>
|
|
50
|
+
{options.routes.map((props, i) => {
|
|
51
|
+
return (
|
|
52
|
+
<Route
|
|
53
|
+
key={i}
|
|
54
|
+
path={props.path}
|
|
55
|
+
element={renderElement(props.path, props.element)}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
})}
|
|
59
|
+
<Route key='nf' path='*' element={renderElement(null, options.notFoundComp)} />
|
|
60
|
+
</Routes>
|
|
61
|
+
);
|
|
64
62
|
};
|
|
65
63
|
|
|
66
64
|
export default generateRoutes;
|