-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Framework: add separate server state hydration #3343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ | |
* External dependencies | ||
*/ | ||
import debugModule from 'debug'; | ||
import pick from 'lodash/pick'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createReduxStore, reducer } from 'state'; | ||
import { SERIALIZE, DESERIALIZE } from 'state/action-types' | ||
import { SERIALIZE, DESERIALIZE, SERVER_DESERIALIZE } from 'state/action-types' | ||
import { getLocalForage } from 'lib/localforage'; | ||
import config from 'config'; | ||
|
||
|
@@ -24,7 +25,8 @@ export const MAX_AGE = 7 * DAY_IN_HOURS * HOUR_IN_MS; | |
function getInitialServerState() { | ||
// Bootstrapped state from a server-render | ||
if ( typeof window === 'object' && window.initialReduxState ) { | ||
return window.initialReduxState; | ||
const serverState = reducer( window.initialReduxState, { type: SERVER_DESERIALIZE } ); | ||
return pick( serverState, Object.keys( window.initialReduxState ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I don't, server state will override persisted state with default initial state. @seear which sections of the tree do you see us bootstrapping, and do we need to be more precise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the state tree being bootstrapped is defined by the A reducer that did not return any default state for the |
||
} | ||
return {}; | ||
} | ||
|
@@ -34,11 +36,9 @@ function serialize( state ) { | |
return Object.assign( serializedState, { _timestamp: Date.now() } ); | ||
} | ||
|
||
function deserialize( localforageState ) { | ||
delete localforageState._timestamp; | ||
const serverState = getInitialServerState(); | ||
const mergedState = Object.assign( {}, localforageState, serverState ); | ||
return reducer( mergedState, { type: DESERIALIZE } ); | ||
function deserialize( state ) { | ||
delete state._timestamp; | ||
return reducer( state, { type: DESERIALIZE } ); | ||
} | ||
|
||
function loadInitialState( initialState ) { | ||
|
@@ -51,7 +51,10 @@ function loadInitialState( initialState ) { | |
debug( 'stored state is too old, building redux store from scratch' ); | ||
initialState = {}; | ||
} | ||
return createReduxStore( deserialize( initialState ) ); | ||
const localforageState = deserialize( initialState ); | ||
const serverState = getInitialServerState(); | ||
const mergedState = Object.assign( {}, localforageState, serverState ); | ||
return createReduxStore( mergedState ); | ||
} | ||
|
||
function loadInitialStateFailed( error ) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to say that
BOOTSTRAP
may give more clue as to what this is for, but I checked back, and that term is not used at all in the server-rendering section of the Redux docs, so I think it is fine as is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not attached to the name, so I'd be happy to open up another PR if we find that another term is more clear.