Merge pull request #96 from sharetribe/data-serialisation

Data serialisation
This commit is contained in:
Kimmo Puputti 2017-03-29 09:23:58 +03:00 committed by GitHub
commit 2588382a94
5 changed files with 19 additions and 8 deletions

View file

@ -28,7 +28,7 @@
"redux-thunk": "^2.2.0",
"sanitize.css": "^5.0.0",
"sharetribe-scripts": "0.9.2",
"sharetribe-sdk": "git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#2972b2ec40509e89b30626fcfeb92327a6b805a0",
"sharetribe-sdk": "git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#1a785c9b62a21b3837d450c44b72a1d87e144429",
"source-map-support": "^0.4.12",
"url": "^0.11.0"
},

View file

@ -1,6 +1,7 @@
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const { types } = require('sharetribe-sdk');
const { renderApp } = require('./importer');
const buildPath = path.resolve(__dirname, '..', 'build');
@ -43,9 +44,14 @@ exports.render = function(requestUrl, context, preloadedState) {
// For security reasons we ensure that preloaded state is considered as a string
// by replacing '<' character with its unicode equivalent.
// http://redux.js.org/docs/recipes/ServerRendering.html#security-considerations
const serializedState = JSON.stringify(preloadedState).replace(/</g, '\\u003c');
const serializedState = JSON.stringify(preloadedState, types.replacer).replace(/</g, '\\u003c');
// At this point the serializedState is a string, the second
// JSON.stringify wraps it within double quotes and escapes the
// contents properly so the value can be injected in the script tag
// as a string.
const preloadedStateScript = `
<script>window.__PRELOADED_STATE__ = ${serializedState};</script>
<script>window.__PRELOADED_STATE__ = ${JSON.stringify(serializedState)};</script>
`;
return template({ title: head.title.toString(), preloadedStateScript, body });

View file

@ -67,7 +67,11 @@ ServerApp.propTypes = { url: string.isRequired, context: any.isRequired, store:
* - {Object} head: Application head metadata from react-helmet
*/
export const renderApp = (url, serverContext, preloadedState) => {
const store = configureStore(preloadedState);
// Pass `null` as the SDK instance since we're only rendering the
// component tree with the preloaded store state and components
// shouldn't do any SDK calls in the (server) rendering lifecycle.
const store = configureStore(null, preloadedState);
const body = ReactDOMServer.renderToString(
<ServerApp url={url} context={serverContext} store={store} />
);

View file

@ -53,9 +53,10 @@ const renderWhenAuthInfoLoaded = store => {
// If we're in a browser already, render the client application.
if (typeof window !== 'undefined') {
// eslint-disable-next-line no-underscore-dangle
const preloadedState = window.__PRELOADED_STATE__ || {};
const preloadedState = window.__PRELOADED_STATE__ || '{}';
const initialState = JSON.parse(preloadedState, types.reviver);
const sdk = createInstance({ clientId: config.sdk.clientId, baseUrl: config.sdk.baseUrl });
const store = configureStore(sdk, preloadedState);
const store = configureStore(sdk, initialState);
store.runSaga(createRootSaga(sdk));

View file

@ -5938,9 +5938,9 @@ sharetribe-scripts@0.9.2:
optionalDependencies:
fsevents "1.0.17"
"sharetribe-sdk@git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#2972b2ec40509e89b30626fcfeb92327a6b805a0":
"sharetribe-sdk@git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#1a785c9b62a21b3837d450c44b72a1d87e144429":
version "0.0.1"
resolved "git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#2972b2ec40509e89b30626fcfeb92327a6b805a0"
resolved "git+ssh://git@github.com/sharetribe/sharetribe-sdk-js#1a785c9b62a21b3837d450c44b72a1d87e144429"
dependencies:
axios "^0.15.3"
js-cookie "^2.1.3"