Module: web-toggle-point-ssr

Server Side Rendering code for isomorphic / universal applications

Methods

(inner) serializationFactory(params) → {module:web-toggle-point-ssr.serialization}

A factory for creating a serialization object that has methods for serializing and deserializing JSON data in server-rendered web applications.
Parameters:
Name Type Description
params object parameters
Properties
Name Type Description
id string The id attribute of the backing application/json script.
logWarning external:HostApplication.logWarning A method that logs warnings; will be used when malformed JSON is found in the backing store when deserialize on the client, which should only be possible if processed in a system downstream from the origin.
Returns:
Some serialization / deserialization methods
Type
module:web-toggle-point-ssr.serialization
Example
const logWarning = (warning) => console.log(warning);
const id = "app_features";
const { getScriptMarkup, getJSONFromScript } = serializationFactory({ id, logWarning });

(inner) withJsonIsomorphism(WrappedComponent, logWarning, params) → {external:React.Component}

A React Higher-Order-Component that allows serializing data during server-side rendering, via a preceding "application/json" script, which are then realised into a prop when first hydrating on the client. It will be reactive to subsequent non-`undefined` prop values, for that prop. The package "browser" export includes the code to read the script, omitted from the "import" / "require" export (server package).
Parameters:
Name Type Description
WrappedComponent external:React.Component The React component that will receive the props.
logWarning external:HostApplication.logWarning a method that logs warnings; will be used when malformed JSON is found in the backing store.
params object parameters
Properties
Name Type Description
scriptId string The id attribute of the backing application/json script.
propName string The name of the prop to inject into the wrapped React component.
Returns:
Wrapped react component.
Type
external:React.Component
Example
const logWarning = (warning) => console.log(warning);
const scriptId = "app_features";
const propName = "features";
export default withJsonIsomorphism(MyReactComponent, logWarning, { scriptId, propName });

Type Definitions

getJSONFromScript() → {object}

Returns:
The JSON content of the script element.
Type
object

getScriptMarkup(params)

Parameters:
Name Type Description
params object parameters
Properties
Name Type Description
content object The JSON content to be serialized.

getScriptReactElement(params)

Parameters:
Name Type Description
params object parameters
Properties
Name Type Description
content object The JSON content to be serialized.

serialization

An object containing methods for serializing and deserializing JSON data in server-rendered web applications.
Type:
  • object
Properties:
Name Type Description
getScriptMarkup module:web-toggle-point-ssr.getScriptMarkup Gets a string containing markup for a type="application/json" script element with the specified content.
getScriptReactElement module:web-toggle-point-ssr.getScriptReactElement Gets a React element for a type="application/json" script element with the specified content.
getJSONFromScript module:web-toggle-point-ssr.getJSONFromScript Returns the JSON content of the script element.