@appigram/react-code-split-ssr 1.2.0 → 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.
@@ -7,18 +7,20 @@ 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 = yield preload.element.props.mod;
21
- const renderElement = ({ path, bundle }) => {
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
26
  const isSSR = (preload && preload.path === path) || (!preload && !path);
@@ -26,9 +28,9 @@ const generateRoutes = (options = {
26
28
  return _jsx(Element, {}, void 0);
27
29
  };
28
30
  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));
31
+ return (_jsxs(Routes, { children: [options.routes.map((props, i) => {
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));
32
34
  };
33
35
  });
34
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.0",
3
+ "version": "1.2.1",
4
4
  "description": "React code splitting with SSR",
5
5
  "main": "./lib/index.js",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import { ReactElement } from 'react'
1
+ import { ReactElement } from "react";
2
2
  import { matchPath, Route, Routes } from "react-router-dom";
3
3
 
4
4
  export interface IJSXModule {
@@ -7,26 +7,22 @@ export interface IJSXModule {
7
7
 
8
8
  export interface ISSRRoute {
9
9
  caseSensitive?: boolean;
10
- children?: React.ReactNode;
11
- element?: ReactElement;
12
- index?: boolean;
13
- path?: string;
10
+ children?: React.ReactNode;
11
+ element?: () => 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?: () => 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
  ): Promise<React.FC> => {
32
28
  if (!Array.isArray(options.routes) || options.routes.length === 0) {
@@ -34,17 +30,19 @@ const generateRoutes = async (
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 = await preload.element.props.mod;
36
+ const preloadedComp: any =
37
+ preload === undefined
38
+ ? await options.notFoundComp().props.mod
39
+ : await preload.element().props.mod;
42
40
 
43
- const renderElement = ({path , bundle }: IRenderElement) => {
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;
47
- return <Element />
45
+ return <Element />;
48
46
  };
49
47
 
50
48
  return () => {
@@ -55,12 +53,14 @@ const generateRoutes = async (
55
53
  <Route
56
54
  key={i}
57
55
  path={props.path}
58
- element={renderElement({ path: props.path, bundle: props.element})}
56
+ element={renderElement(props.path, props.element())}
59
57
  />
60
- )})}
58
+ );
59
+ })}
60
+ <Route element={renderElement(null, options.notFoundComp())} />
61
61
  </Routes>
62
62
  );
63
- }
63
+ };
64
64
  };
65
65
 
66
66
  export default generateRoutes;