diff --git a/package.json b/package.json index 272f2650..19a531fd 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/server/renderer.js b/server/renderer.js index 415141ee..2cbe3712 100644 --- a/server/renderer.js +++ b/server/renderer.js @@ -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(/window.__PRELOADED_STATE__ = ${serializedState}; + `; return template({ title: head.title.toString(), preloadedStateScript, body }); diff --git a/src/app.js b/src/app.js index f18a8b43..6653d09b 100644 --- a/src/app.js +++ b/src/app.js @@ -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( ); diff --git a/src/index.js b/src/index.js index fa803020..5cf465fb 100644 --- a/src/index.js +++ b/src/index.js @@ -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)); diff --git a/yarn.lock b/yarn.lock index 5cd0ec04..a24e7b50 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"