mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Fix preloadedState serialisation
- Use reviver and replacer from the SDK to properly encode/decode the SDK types
This commit is contained in:
parent
227d2f3890
commit
3f20100e40
2 changed files with 11 additions and 4 deletions
|
|
@ -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 });
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue