Normally, maomi generates HTML elements in browsers. At startup, it takes an empty DOM node, a.k.a. a mount-point, and then repeatedly insert or modify other nodes.
The server side rendering can generate static HTML segments in the server. The HTML segment can be inserted into the mount-point before startup. Then maomi will reuse the generated HTML nodes at startup.
This allows static contents embed in HTML directly, and be understood by search engines better.
Because maomi is in rust, the component code can be compiled to native binary which can be used to generate HTML segments. It means the components are compiled twice: one is to the native binary which is used to generate the static HTML, the other is to the WebAssembly binary which is used in browser runtime.
To enable server side rendering, some features need to be specified in Cargo.toml. The "prerendering" feature is to enable the server side support, and the "prerendering-apply" feature is to enable the reuse of the server side generated nodes.
Note that these features slightly increase the runtime overhead. Do not enable them if server side rendering is not needed.
To be rendered in server side, the component must implement "PrerenderableComponent". This trait has two associated types and two functions.
The "QueryData" type refers to the query, e.g. the URL params, the POST data, or some other related data. For static contents, it can simply be "()".
The "PrerenderingData" type is converted from the "QueryData", containing some useful parts of the query for the component startup. It should be serializable and transferable from server side to the client side.
When doing server side rendering, firstly, the "prerendering_data" function is called in server side to convert "QueryData" to "PrerenderingData". (It may also be called in client side when doing client side rendering.)
Secondly, the component starts in the server side, and the "apply_prerendering_data" is called. This function can modify component fields according to the "PrerenderingData". The HTML segment can be generated when the component created.
Thirdly, the generated HTML should be embed into the mount-point, and the "PrerenderingData" should also be transferred to the client side.
Fourthly, the component starts in the client side, and the "apply_prerendering_data" is also called. This function must do the same thing as it did in the server side. Thus this component can reuse the generated HTML nodes.
Note that these features slightly increase the runtime overhead. Do not enable them if server side rendering is not needed.
To generate HTML in server side, a special kind of backend context is used.
To reuse the generated HTML, the backend context should be initialized in with "PrerenderingData".