48 | ```
49 |
50 | Looking for a boilerplate?
51 | Try [Hyperapp Starter](https://github.com/kriasoft/hyperapp-starter)
52 | with pre-configured server-side rendering and many more.
53 |
54 | ## Installation
55 |
56 | Using [npm](https://www.npmjs.com/package/hyperapp-render):
57 |
58 | ```bash
59 | npm install hyperapp-render --save
60 | ```
61 |
62 | Or using a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network) like
63 | [unpkg.com](https://unpkg.com/hyperapp-render) or
64 | [jsDelivr](https://cdn.jsdelivr.net/npm/hyperapp-render)
65 | with the following script tag:
66 |
67 | ```html
68 |
69 | ```
70 |
71 | You can find the library in `window.hyperappRender`.
72 |
73 | We support all ES5-compliant browsers, including Internet Explorer 9 and above,
74 | but depending on your target browsers you may need to include
75 | [polyfills]() for
76 | [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and
77 | [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
78 | before any other code.
79 |
80 | ## Usage
81 |
82 | The library provides two functions
83 | which you can use depending on your needs or personal preferences:
84 |
85 | ```jsx
86 | import { renderToString, renderToStream } from 'hyperapp-render'
87 |
88 | renderToString() // =>
89 | renderToString(view(state, actions)) // =>
90 | renderToString(view, state, actions) // =>
91 |
92 | renderToStream() // => =>
93 | renderToStream(view(state, actions)) // => =>
94 | renderToStream(view, state, actions) // => =>
95 | ```
96 |
97 | **Note:** `renderToStream` is available from
98 | [Node.js](https://nodejs.org/en/) environment only (v6 or newer).
99 |
100 | ## Overview
101 |
102 | You can use `renderToString` function to generate HTML on the server
103 | and send the markup down on the initial request for faster page loads
104 | and to allow search engines to crawl your pages for
105 | [SEO](https://en.wikipedia.org/wiki/Search_engine_optimization) purposes.
106 |
107 | If you call [`hyperapp.app()`](https://github.com/hyperapp/hyperapp/tree/1.2.9#mounting)
108 | on a node that already has this server-rendered markup,
109 | Hyperapp will preserve it and only attach event handlers, allowing you
110 | to have a very performant first-load experience.
111 |
112 | The `renderToStream` function returns a
113 | [Readable stream](https://nodejs.org/api/stream.html#stream_readable_streams)
114 | that outputs an HTML string.
115 | The HTML output by this stream is exactly equal to what `renderToString` would return.
116 | By using this function you can reduce [TTFB](https://en.wikipedia.org/wiki/Time_to_first_byte)
117 | and improve user experience even more.
118 |
119 | ## Caveats
120 |
121 | The library automatically escapes text content and attribute values
122 | of [virtual DOM nodes](https://github.com/hyperapp/hyperapp/tree/1.2.9#view)
123 | to protect your application against
124 | [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting) attacks.
125 | However, it is not safe to allow "user input" for node names or attribute keys:
126 |
127 | ```jsx
128 | const Node = 'div onclick="alert()"'
129 | renderToString(Hi)
130 | // =>