Preact Server Renderer
preact-server-render
is similar to preact-render-to-string
by @developit (author of preact),
but with pluggable formatters. This means you can easily tailer the
output to your needs, without any overhead. Wether you have a
snapshot renderer, or a custom format in mind. Performance wise,
both libraries are equal, only the jsx renderer is a tiny bit faster
here.
Installation
# npm npm install preact-server-renderer # yarn yarn add preact-server-renderer
Usage
const createRenderer CompactRenderer JsxRenderer = ; // Default renderingconst render = ;const html = ; // JSX renderingconst renderJsx = ;const html2 = ; // shallow renderingconst shallow = ;const html3 = ;
CompactRenderer
The CompactRenderer
is the standard html renderer. It doesn't add any
whitespace and simply renders the components to a string.
Example:
const createRenderer CompactRenderer = ; const render = ;console;
Output:
<div class="foo"><h1>Hello</h1></div>
JsxRenderer
This one is meant for snapshot tests, like they are found in jest.
Example:
const createRenderer JsxRenderer = ; const render = ;console;
Output:
<div class="foo"> <h1> Hello </h1></div>
Writing A Custom Renderer
This is where this library really shines. Custom renderers can be easily
plugged into the createRenderer
function. They basically have a few callbacks
for the specific parsing steps. They simply need to adhere to the following
interface and that's it!
License
MIT, see LICENSE.md.