@appigram/react-code-split-ssr 1.3.0 → 1.3.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/README.md +70 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,114 +1,130 @@
|
|
|
1
1
|
# React Code Split SSR
|
|
2
|
+
|
|
2
3
|
React code splitting with server side rendering
|
|
3
4
|
|
|
4
5
|
## How it works
|
|
5
6
|
|
|
6
|
-
-
|
|
7
|
+
- React 19
|
|
8
|
+
- Based on React Router 7
|
|
7
9
|
- Server side: Load all components synchronously, then render to string.
|
|
8
10
|
- Client side: Load the initial component before rendering, render the entire screen synchronously, then load the rest of routes asynchronously.
|
|
9
11
|
|
|
10
12
|
#### Note: This packages is only tested in Meteor projects, but technically it can be used in any Nodejs projects. Here is an [example](https://github.com/lhz516/react-code-split-ssr-example) with Meteor
|
|
11
13
|
|
|
12
14
|
## Usage
|
|
15
|
+
|
|
13
16
|
```
|
|
14
17
|
$ npm i react-code-split-ssr --save
|
|
15
18
|
```
|
|
19
|
+
|
|
16
20
|
Client/server shared:
|
|
21
|
+
|
|
17
22
|
```js
|
|
18
|
-
import { Bundle } from 'react-code-split-ssr'
|
|
19
|
-
import React from 'react'
|
|
23
|
+
import { Bundle } from 'react-code-split-ssr';
|
|
24
|
+
import React from 'react';
|
|
20
25
|
|
|
21
|
-
const Home = () => <Bundle mod={import('/imports/modules/home')}
|
|
22
|
-
const Posts = () => <Bundle mod={import('/imports/modules/posts')}
|
|
26
|
+
const Home = () => <Bundle mod={import('/imports/modules/home')} />;
|
|
27
|
+
const Posts = () => <Bundle mod={import('/imports/modules/posts')} />;
|
|
23
28
|
|
|
24
29
|
export const routes = [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
]
|
|
28
|
-
|
|
30
|
+
{ exact: true, path: '/', component: Home },
|
|
31
|
+
{ exact: true, path: '/posts', component: Posts },
|
|
32
|
+
];
|
|
29
33
|
```
|
|
34
|
+
|
|
30
35
|
Client:
|
|
36
|
+
|
|
31
37
|
```js
|
|
32
|
-
import
|
|
33
|
-
import { render } from 'react-dom'
|
|
34
|
-
import { matchPath } from 'react-router'
|
|
35
|
-
import { BrowserRouter, Route, Switch } from 'react-router-dom'
|
|
36
|
-
import { generateRoutes } from 'react-code-split-ssr'
|
|
37
|
-
import { routes, redirects } from '/imports/routes'
|
|
38
|
+
import React from 'react';
|
|
39
|
+
import { render } from 'react-dom';
|
|
40
|
+
import { matchPath } from 'react-router';
|
|
41
|
+
import { BrowserRouter, Route, Switch } from 'react-router-dom';
|
|
42
|
+
import { generateRoutes } from 'react-code-split-ssr';
|
|
43
|
+
import { routes, redirects } from '/imports/routes';
|
|
38
44
|
// import some components...
|
|
39
45
|
|
|
40
46
|
/* In an async function */
|
|
41
47
|
const Routes = await generateRoutes({
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
})
|
|
48
|
+
routes,
|
|
49
|
+
redirects,
|
|
50
|
+
notFoundComp: NotFound,
|
|
51
|
+
pathname: window.location.pathname,
|
|
52
|
+
});
|
|
47
53
|
render(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
)
|
|
54
|
+
<BrowserRouter>
|
|
55
|
+
<Layout>
|
|
56
|
+
<Routes />
|
|
57
|
+
</Layout>
|
|
58
|
+
</BrowserRouter>,
|
|
59
|
+
document.getElementById('app')
|
|
60
|
+
);
|
|
55
61
|
```
|
|
62
|
+
|
|
56
63
|
Server:
|
|
64
|
+
|
|
57
65
|
```js
|
|
58
|
-
import { Route, StaticRouter, Switch } from 'react-router'
|
|
59
|
-
import React from 'react'
|
|
60
|
-
import { renderToString } from 'react-dom/server'
|
|
61
|
-
import { generateRoutes } from 'react-code-split-ssr'
|
|
62
|
-
import { routes, redirects } from '/imports/routes'
|
|
66
|
+
import { Route, StaticRouter, Switch } from 'react-router';
|
|
67
|
+
import React from 'react';
|
|
68
|
+
import { renderToString } from 'react-dom/server';
|
|
69
|
+
import { generateRoutes } from 'react-code-split-ssr';
|
|
70
|
+
import { routes, redirects } from '/imports/routes';
|
|
63
71
|
// import some components...
|
|
64
72
|
|
|
65
73
|
/* In an async server router */
|
|
66
74
|
const Routes = await generateRoutes({
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
const ServerRoutes = ({url, context = {}}) => (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
</StaticRouter>
|
|
82
|
-
)
|
|
83
|
-
const bodyHtmlString = renderToString(<ServerRoutes url={sink.request.url} />)
|
|
75
|
+
routes,
|
|
76
|
+
redirects,
|
|
77
|
+
notFoundComp: NotFound,
|
|
78
|
+
pathname: req.url,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const ServerRoutes = ({ url, context = {} }) => (
|
|
82
|
+
<StaticRouter location={url.pathname} context={context}>
|
|
83
|
+
<Layout>
|
|
84
|
+
<Routes />
|
|
85
|
+
</Layout>
|
|
86
|
+
</StaticRouter>
|
|
87
|
+
);
|
|
88
|
+
const bodyHtmlString = renderToString(<ServerRoutes url={sink.request.url} />);
|
|
84
89
|
// Use bodyHtmlString to where you need
|
|
85
90
|
```
|
|
86
91
|
|
|
87
92
|
## API
|
|
88
93
|
|
|
89
94
|
### Bundle - React Component \<Bundle\>
|
|
95
|
+
|
|
90
96
|
#### Props
|
|
91
|
-
|
|
97
|
+
|
|
98
|
+
- mod {Promise<Component>} **_Required_** - A `Promise` object which can be resolved to React Component
|
|
92
99
|
- loading {Component} - A React Component
|
|
93
100
|
|
|
94
101
|
### generateRoutes - Func (object: Options)
|
|
102
|
+
|
|
95
103
|
A function that returns a `Promise` object which can be resolved to React Router routes wrapped in `<Switch>`
|
|
96
104
|
|
|
97
105
|
#### Options
|
|
98
|
-
|
|
99
|
-
-
|
|
106
|
+
|
|
107
|
+
- pathname {string} **_Required_** - Pathname for initial loading
|
|
108
|
+
- routes {objects}[] **_Required_** - An array of `<Route>` props object
|
|
100
109
|
- `component` field only accepts `() => <Bundle/>`
|
|
101
110
|
- `location`, `render` fields are currently not supported
|
|
102
|
-
- redirects
|
|
111
|
+
- redirects {objects}[] - An array of `<Redirect>` props object
|
|
103
112
|
- notFoundComp {Component} - A React component for 404 Not Found, only accepts `() => <Bundle/>`
|
|
104
113
|
|
|
114
|
+
## v1.3.0
|
|
115
|
+
|
|
116
|
+
- [x] Upgrade to React-Router 7 and React 19
|
|
117
|
+
|
|
105
118
|
## v1.2.0
|
|
119
|
+
|
|
106
120
|
- [x] Remove redirects, Fix React-Router v6 specific rendering
|
|
107
121
|
|
|
108
122
|
## v1.1.0
|
|
123
|
+
|
|
109
124
|
- [x] Upgrade to React-Router v6
|
|
110
125
|
|
|
111
126
|
## v1.0.0
|
|
127
|
+
|
|
112
128
|
- [ ] SSR correctly for redirected routes
|
|
113
|
-
- [
|
|
129
|
+
- [x] Change `notFoundComp` to `() => <Bundle />`
|
|
114
130
|
- [ ] Add more argument validations
|