@appigram/react-code-split-ssr 1.2.0-beta.7 → 1.2.3

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 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 4
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
 
@@ -7,11 +7,11 @@ 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");
@@ -23,14 +23,12 @@ const generateRoutes = (options = {
23
23
  const renderElement = (path, bundle) => {
24
24
  if (!preloadedComp)
25
25
  return bundle;
26
- const isSSR = (preload && preload.path === path) || (!preload && !path);
27
- return isSSR ? preloadedComp.default : bundle;
26
+ const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
27
+ const Element = isRouteMatched ? preloadedComp.default : bundle;
28
+ return isRouteMatched ? _jsx(Element, {}, void 0) : Element;
28
29
  };
29
- const routes = options.routes.map((props, i) => {
30
- const element = renderElement(props.path, props.element);
31
- return (_jsx(Route, Object.assign({}, props, { element: element }), i));
32
- });
33
- routes.push(_jsx(Route, { element: renderElement(null, options.notFoundComp) }, options.routes.length));
34
- return (_jsx(Routes, { children: routes }, void 0));
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) }, void 0)] }, void 0));
35
33
  });
36
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.0-beta.7",
3
+ "version": "1.2.3",
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.34",
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.4.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.0.2"
34
+ "react-router-dom": "^6.1.1"
35
35
  }
36
36
  }
@@ -1,40 +1,36 @@
1
- import { ReactElement, JSXElementConstructor } from 'react'
1
+ import { ReactElement } from "react";
2
2
  import { matchPath, Route, Routes } from "react-router-dom";
3
- // import Bundle from './bundle'
4
-
5
- export type IElement = ReactElement<any, string | JSXElementConstructor<any>>
6
3
 
7
4
  export interface IJSXModule {
8
- default: IElement;
5
+ default: React.FC | React.ComponentClass;
9
6
  }
10
7
 
11
8
  export interface ISSRRoute {
12
9
  caseSensitive?: boolean;
13
- children?: React.ReactNode;
14
- element?: () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
15
- index?: boolean;
16
- path?: string;
10
+ children?: React.ReactNode;
11
+ element?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
12
+ index?: boolean;
13
+ path?: string;
17
14
  }
18
15
 
19
16
  export interface IOptions {
20
17
  pathname: string;
21
18
  routes: ISSRRoute[];
22
- notFoundComp?: () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
19
+ notFoundComp?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
23
20
  }
24
21
 
25
22
  const generateRoutes = async (
26
23
  options: IOptions = {
27
24
  pathname: "/",
28
- routes: []
25
+ routes: [],
29
26
  }
30
- ): Promise<IElement> => {
27
+ ) => {
31
28
  if (!Array.isArray(options.routes) || options.routes.length === 0) {
32
29
  throw new Error("options.routes must be an non-empty array");
33
30
  }
34
31
 
35
32
  const preload = options.routes.find(
36
- (route) =>
37
- !!matchPath(route.path, options.pathname)
33
+ (route) => !!matchPath(route.path, options.pathname)
38
34
  );
39
35
 
40
36
  const preloadedComp: any =
@@ -42,25 +38,25 @@ const generateRoutes = async (
42
38
  ? await options.notFoundComp().props.mod
43
39
  : await preload.element().props.mod;
44
40
 
45
- const renderElement = (path: string, bundle: React.FC) => {
41
+ const renderElement = (path: string, bundle: ReactElement) => {
46
42
  if (!preloadedComp) return bundle;
47
- const isSSR = (preload && preload.path === path) || (!preload && !path);
48
- return isSSR ? preloadedComp.default : bundle;
43
+ const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
44
+ const Element = isRouteMatched ? preloadedComp.default : bundle;
45
+ return isRouteMatched ? <Element /> : Element
49
46
  };
50
47
 
51
- const routes = options.routes.map((props, i) => {
52
- const element = renderElement(props.path, props.element)
53
- return (
54
- <Route
55
- key={i}
56
- {...props}
57
- element={element}
58
- />
59
- )})
60
- routes.push(<Route key={options.routes.length} element={renderElement(null, options.notFoundComp)} />)
61
48
  return (
62
49
  <Routes>
63
- {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 path='*' element={renderElement(null, options.notFoundComp)} />
64
60
  </Routes>
65
61
  );
66
62
  };