Hoppa till innehåll

Render

Returns HTML for a script tag with the possibility to pass data from the server. Framework agnostic.

Server data is embedded as JSON in script tags. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html

index.js

render([props], [settings]) ⇨ String

@soleil-se/app-util/server

Get a HTML string for rendering an application.

Returns: String - HTML for rendering an application.

ParamTypeDefaultDescription
[props]Object{}Props that will be passed to the app
[settings]Object{}Settings object.
[settings.html]StringHTML that will be rendered inside mount element.
[settings.async]BooleanfalseIf the app script should be loaded asynchronously.
[settings.defer]BooleantrueIf the app script should be loaded after DOM is ready.
[settings.req]ObjectThe req object from Sitevision, pass this to optimize browser specific script loading if you have multiple instances of the app.

Read more about async and defer.

Example

import router from '@Sitevision/api/common/router';
import { render } from '@soleil-se/app-util/server';
router.get('/', (req, res) => {
const props = { foo: 'bar' };
res.send(render(props));
};

All settings

import router from '@Sitevision/api/common/router';
import { render } from '@soleil-se/app-util/server';
router.get('/', (req, res) => {
const props = { foo: 'bar' };
const settings = {
html: '<noscript>You can put a noscript message here for example!</noscript>',
async: false,
defer: true,
req,
}
res.send(render(props, settings));
};