Also known as: "the state problem". You can ignore this section if your app doesn't make XHR/Websocket requests.
Will this cause a "flash" or "blink" while the JavaScript app renders a zero/empty state for the in-flight XHR/Websocket request?
Yes, apps that fetch any remote data via AJAX or websockets will have this problem. There are 2 different solutions:
Prerender-Disable-Ajax-Preload: true
) but the next solution is better.window.__PRELOADED_STATE__
or window.__PRELOADED_STATE_PLAIN__
window.__PRELOADED_STATE__
or window.__PRELOADED_STATE_PLAIN__
. Headless-Render-API.com specifically looks for this variable and will serialize it into the HTML if it exists. It allows you to do something like this (plain React example):// React side note: use componentWillMount instead of didMount to avoid
// "React attempted to reuse markup in a container but the checksum was invalid"
// (componentDidMount is not called during renderToString)
componentWillMount() {
if (window.__PRELOADED_STATE__) {
// reuse the state from the server
this.setState(window.__PRELOADED_STATE__);
} else {
// make an async request and save
// to the __PRELOADED_STATE__ var so
// Headless-Render-API.com will serialize into the HTML
window.fetch('/my-ajax-endpoint')
.then(response => response.json())
.then(json => {
this.setState(json);
window.__PRELOADED_STATE__ = json;
});
}
}
__PRELOADED_STATE__
or window.__PRELOADED_STATE_PLAIN__
variable, we assume you're managing all state and so we automatically disable the Ajax-Preload monkey patch.window.__PRELOADED_STATE__
and window.__PRELOADED_STATE_PLAIN__
do the same thing except the former does base64encode(JSON.stringify(content)) and the latter only does JSON.stringify.