Skip to content

Commit 526a301

Browse files
committed
feat: expose HTTP request and response from server-side
1 parent a1fffca commit 526a301

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Really cool starter boilerplate with the most popular technologies:
3737
- [Babel](https://babeljs.io) for transpile ES6+ to ES5.
3838
- [React Hot Loader](https://github.com/gaearon/react-hot-loader) to tweak React components in real time.
3939
- [nodemon](https://nodemon.io) to monitor for any changes in your Node.js application and automatically restart the server.
40-
- [axios](https://github.com/mzabriskie/axios) as the Promise-based HTTP client for the browser and Node.js.
40+
- [axios](https://github.com/axios/axios) as the Promise-based HTTP client for the browser and Node.js.
4141
- [redux-thunk](https://github.com/gaearon/redux-thunk) as the middleware to deal with asynchronous action.
4242
- [react-helmet](https://github.com/nfl/react-helmet) to manage title, meta, styles and scripts tags on both server and client.
4343
- [loadable-component](https://github.com/smooth-code/loadable-components) to lazy load component when needed in app. Reduce your bundle size without stress.
@@ -226,7 +226,7 @@ export default [
226226

227227
### Data Fetching from Server-side
228228

229-
Just write Redux actions and stores as normal (read the [Redux](https://redux.js.org) document if you are new). The starter using [axios](https://github.com/mzabriskie/axios) as the data fetcher, it's quite simple and easy to use. If the action creator is asynchronous then it will return a Promise (or a Promise.all) in the inner function.
229+
Just write Redux actions and stores as normal (read the [Redux](https://redux.js.org) document if you are new). The starter using [axios](https://github.com/axios/axios) as the data fetcher, it's quite simple and easy to use. If the action creator is asynchronous then it will return a Promise (or a Promise.all) in the inner function.
230230

231231
Register the action(s) in `./src/routes.tsx`, which have to be called from server-side:
232232

@@ -239,7 +239,8 @@ export default [
239239
exact: true,
240240
component: RouteComponent,
241241
// Actions in the loadData function will be fetched from server-side
242-
loadData: () => [
242+
// You can access the URL parameters, Redux store, HTTP request and response by the event object
243+
loadData: ({ params, getState, req, res }) => [
243244
myReduxAction(),
244245
// Add other pre-fetched actions here
245246
],
@@ -264,7 +265,12 @@ app.get("*", (req, res) => {
264265
if (route.loadData) {
265266
return Promise.all(
266267
route
267-
.loadData({ params: match.params, getState: store.getState })
268+
.loadData({
269+
params: match.params,
270+
getState: store.getState,
271+
req,
272+
res,
273+
})
268274
.map((item) => store.dispatch(item))
269275
);
270276
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-cool-starter",
3-
"version": "3.6.1",
3+
"version": "3.7.0",
44
"description": "A starter boilerplate for a universal web application with the best development experience and best practices.",
55
"license": "MIT",
66
"homepage": "https://github.com/wellyshen/react-cool-starter",

src/server.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ app.get("*", (req, res) => {
7171
if (route.loadData)
7272
return Promise.all(
7373
route
74-
.loadData({ params: match.params, getState: store.getState })
74+
.loadData({
75+
params: match.params,
76+
getState: store.getState,
77+
req,
78+
res,
79+
})
7580
.map((item: MyAction) => store.dispatch(item))
7681
);
7782

0 commit comments

Comments
 (0)