Skip to content

Commit f1fd96f

Browse files
authored
Merge pull request #20 from XbyOrange/v1.1.0
V1.1.0
2 parents 08a3f63 + a22123d commit f1fd96f

8 files changed

Lines changed: 286 additions & 67 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ script:
2020
- npm run lint
2121
- npm run test-ci
2222
- npm run build
23-
# - npm run coveralls
23+
- npm run coveralls
2424
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then sonar-scanner; fi'
2525

2626
deploy:

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
### Removed
1212
### BREAKING CHANGES
1313

14+
## [1.1.0] - 2019-08-21
15+
### Added
16+
- Add server side data behavior tests and documentation.
17+
1418
## [1.1.0-beta.2] - 2019-08-19
1519
### Added
16-
- Add helper for registering sources that should be loaded during server side data rendering.
20+
- Add method for registering sources that should be loaded during server side data rendering.
1721

1822
### Fixed
1923
- Fix server side data connect. Load data in client when no server data is available

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,71 @@ export const ConnectedModule = connect(
177177

178178
> This parser function should not be used as a replacement to Mercury Selectors. As a good practice, the preference is to use Selectors if possible instead of this function.
179179
180+
## Prefetching data on server side rendering
181+
182+
Methods for prefetching data on server side rendering are available too. When data is prefetched in server side, the connect function will pass the `value` property calculated on server side to the components directly. It will not modify the `loading` property until the first load on client side is finished (At first client-side load, the resource will not be considered as `loading` to maintain the server-side value in the component until it finish loading).
183+
184+
### Server side data methods and components
185+
186+
* `addServerSideData(source)`
187+
* Arguments
188+
* source - `<Object> or <Array> of <Objects>` Mercury source or sources that should be read when `readServerSideData` method is executed. Can be Mercury origins or selectors of any type, queried or not.
189+
* `readServerSideData([source])`
190+
* Arguments
191+
* source - `<Object> or <Array> of <Objects>` Mercury source or sources that should be read when `readServerSideData` method is executed. Can be Mercury origins or selectors of any type, queried or not.
192+
* Returns
193+
* `<Object>` This method is asynchronous, and returns an object containing all server side data ready to be set on the `<ServerSideData>` context component.
194+
* `<ServerSideData data={data} clientSide={true}><App/></ServerSideData>` Component that sets the result of the `readServerSideData` method in a context to make it available from all mercury connected children components.
195+
* Props
196+
* data - `<Object>` Object containing the result of the `readServerSideData` method.
197+
* clientSide - `<Boolean>` If false, the connect method will not dispatch automatically the read method of the sources marked as "server-side", so, for example, api requests will not be repeated on client side, and data retrieved in server side will be always passed to connected components.
198+
199+
### Example of server side prefecth implementation in a Next project:
200+
201+
In the next example, the data of the "myDataSource" mercury source will be fetched on server side and request will not be repeated on client side. The component will be rendered directly with server side data, and no loading state will be set:
202+
203+
```jsx
204+
// src/home.js
205+
import { addServerSideData, connect } from "@xbyorange/react-mercury";
206+
import { myDataSource } from "src/data";
207+
208+
addServerSideData(myDataSource); // source is marked to be read when readServerSideData method is executed.
209+
210+
export const HomeComponent = ({data}) => {
211+
if(data.loading) {
212+
return <div>Loading...</div>
213+
}
214+
return <div>{data.value}</div>
215+
};
216+
217+
export const mapDataSourceToProps = () => ({
218+
data: myDataSource.read
219+
});
220+
221+
export const Home = connect(mapDataSourceToProps)(HomeComponent)
222+
223+
```
224+
225+
```jsx
226+
// pages/index.js
227+
import { readServerSideData, ServerSideData } from "@xbyorange/react-mercury";
228+
import { Home } from "src/home";
229+
230+
const Page = ({ serverSideData }) => (
231+
<ServerSideData data={serverSideData} clientSide={false} >
232+
<Home/>
233+
</ServerSideData>
234+
);
235+
236+
Page.getInitialProps = async () => {
237+
return {
238+
serverSideData: await readServerSideData()
239+
}
240+
}
241+
242+
export default Page;
243+
```
244+
180245
## Demo
181246

182247
To run a real implementation example in a React app, you can clone the project and execute the provided demo:

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 75,
21-
functions: 75,
22-
lines: 80,
23-
statements: 80
20+
branches: 100,
21+
functions: 100,
22+
lines: 100,
23+
statements: 100
2424
}
2525
},
2626

package-lock.json

Lines changed: 8 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"coveralls": "cat ./coverage/lcov.info | coveralls"
3333
},
3434
"peerDependencies": {
35-
"react": "^16.7.0"
35+
"react": "^16.7.0",
36+
"@xbyorange/mercury": "^1.1.0"
3637
},
3738
"dependencies": {
3839
"hoist-non-react-statics": "^3.2.1",
@@ -42,7 +43,8 @@
4243
"@babel/core": "^7.4.5",
4344
"@babel/preset-env": "^7.4.5",
4445
"@babel/preset-react": "^7.0.0",
45-
"@xbyorange/mercury-api": "1.0.1",
46+
"@xbyorange/mercury-api": "1.2.0",
47+
"@xbyorange/mercury": "^1.1.0",
4648
"axios": "^0.19.0",
4749
"axios-retry": "^3.1.2",
4850
"babel-core": "^7.0.0-bridge.0",

src/readServerSideData.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ const resultsToObject = results => {
2222
};
2323

2424
export const addServerSideData = sources => {
25-
const sourcesToAdd = isArray(sources) ? sources : [sources];
26-
sourcesToAdd.forEach(source => {
27-
serverSideData.add(source);
28-
});
25+
if (sources) {
26+
const sourcesToAdd = isArray(sources) ? sources : [sources];
27+
sourcesToAdd.forEach(source => {
28+
serverSideData.add(source);
29+
});
30+
}
2931
};
3032

31-
export const readServerSideData = (...args) => {
32-
args.forEach(source => {
33-
addServerSideData(source);
34-
});
33+
export const readServerSideData = sources => {
34+
addServerSideData(sources);
3535
return Promise.all(Array.from(serverSideData).map(getSourceData)).then(resultsToObject);
3636
};

0 commit comments

Comments
 (0)