What is preact-render-to-string?
The preact-render-to-string package is used to render Preact components to HTML strings, which can be used for server-side rendering (SSR), generating static pages, or even testing components by comparing the output string. It is specifically designed for use with the Preact library, which is a fast 3kB alternative to React with the same modern API.
What are preact-render-to-string's main functionalities?
Render Preact components to HTML
This feature allows you to render a Preact component to an HTML string. It is useful for server-side rendering to send the initial HTML to the browser.
import { h } from 'preact';
import renderToString from 'preact-render-to-string';
const MyComponent = () => <div>Hello, World!</div>;
const html = renderToString(<MyComponent />);
console.log(html); // Outputs: <div>Hello, World!</div>
Render with JSX
You can use JSX syntax to define the components that you want to render to a string, making it easy to write and understand.
import { h } from 'preact';
import renderToString from 'preact-render-to-string';
const html = renderToString(<div className='example'>Example</div>);
console.log(html); // Outputs: <div class="example">Example</div>
Render with options
The package allows you to pass options to the render function, such as 'pretty' for pretty-printed HTML output.
import { h } from 'preact';
import renderToString from 'preact-render-to-string';
const MyComponent = () => <div>Hello, World!</div>;
const html = renderToString(<MyComponent />, { pretty: true });
console.log(html); // Outputs HTML with nice formatting
Other packages similar to preact-render-to-string
vue-server-renderer
This package is used for server-side rendering of Vue.js applications. It turns Vue components into HTML strings, similar to what preact-render-to-string does for Preact components. It is tailored to the Vue.js framework and integrates with its reactivity system.
inferno-server
Inferno-server provides methods for server-side rendering of Inferno components. Inferno is another React-like library for building high-performance user interfaces, and inferno-server offers similar functionalities to preact-render-to-string for the Inferno ecosystem.
preact-render-to-string
Render JSX and Preact components to an HTML string.
Works in Node & the browser, making it useful for universal/isomorphic rendering.
>> Cute Fox-Related Demo (@ CodePen) <<
Render JSX/VDOM to HTML
import { render } from 'preact-render-to-string';
import { h } from 'preact';
let vdom = <div class="foo">content</div>;
let html = render(vdom);
console.log(html);
Render Preact Components to HTML
import { render } from 'preact-render-to-string';
import { h, Component } from 'preact';
class Fox extends Component {
render({ name }) {
return <span class="fox">{name}</span>;
}
}
const Box = ({ type, children }) => (
<div class={`box box-${type}`}>{children}</div>
);
let html = render(
<Box type="open">
<Fox name="Finn" />
</Box>
);
console.log(html);
Render JSX / Preact / Whatever via Express!
import express from 'express';
import { h } from 'preact';
import { render } from 'preact-render-to-string';
const Fox = ({ name }) => (
<div class="fox">
<h5>{name}</h5>
<p>This page is all about {name}.</p>
</div>
);
const app = express();
app.listen(8080);
app.get('/:fox', (req, res) => {
let html = render(<Fox name={req.params.fox} />);
res.send(`<!DOCTYPE html><html><body>${html}</body></html>`);
});
Error Boundaries
Rendering errors can be caught by Preact via getDerivedStateFromErrors
or componentDidCatch
. To enable that feature in preact-render-to-string
set errorBoundaries = true
import { options } from 'preact';
options.errorBoundaries = true;
Suspense
& lazy
components with preact/compat
npm install preact preact-render-to-string
export default () => {
return <h1>Home page</h1>;
};
import { Suspense, lazy } from 'preact/compat';
const HomePage = lazy(() => import('./pages/home'));
const Main = () => {
return (
<Suspense fallback={<p>Loading</p>}>
<HomePage />
</Suspense>
);
};
import { renderToStringAsync } from 'preact-render-to-string';
import { Main } from './main';
const main = async () => {
const html = await renderToStringAsync(<Main />);
console.log(html);
};
main().catch((error) => {
console.error(error);
});
License
MIT