A React compatibility layer for Preact
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Server-side rendering capabilities for generating HTML strings from components, supporting both static markup and hydration patterns.
Renders components to HTML strings for server-side rendering and hydration.
/**
* Render a component to an HTML string
* @param {VNode} vnode - Virtual DOM node to render
* @returns {string} HTML string representation
*/
function renderToString(vnode);
/**
* Render a component to static HTML markup (alias for renderToString)
* @param {VNode} vnode - Virtual DOM node to render
* @returns {string} Static HTML string without React-specific attributes
*/
function renderToStaticMarkup(vnode);Usage Examples:
import { renderToString, renderToStaticMarkup } from 'preact-compat/server';
import App from './App';
// Server-side rendering with hydration support
const html = renderToString(<App />);
const response = `
<!DOCTYPE html>
<html>
<body>
<div id="root">${html}</div>
<script src="/bundle.js"></script>
</body>
</html>
`;
// Static HTML generation (no hydration)
const staticHtml = renderToStaticMarkup(<StaticPage />);// Import from server module
import { renderToString, renderToStaticMarkup } from 'preact-compat/server';
// CommonJS
const { renderToString, renderToStaticMarkup } = require('preact-compat/server');When using server-side rendering, you'll typically want to hydrate the rendered content on the client:
// Server-side (Node.js)
import { renderToString } from 'preact-compat/server';
const serverHtml = renderToString(<App />);
// Client-side
import { hydrate } from 'preact-compat';
hydrate(<App />, document.getElementById('root'));The server-side rendering functionality is provided through integration with the preact-render-to-string package:
// Internal implementation (server.js)
var renderToString = dep(require('preact-render-to-string'));
function dep(obj) { return obj['default'] || obj; }
module.exports = {
renderToString: renderToString,
renderToStaticMarkup: renderToString // Alias - same implementation
};renderToString and renderToStaticMarkup use the same underlying function from preact-render-to-stringUnlike React's server rendering:
renderToString and renderToStaticMarkup (both produce the same output)type RenderFunction = (vnode: VNode) => string;Install with Tessl CLI
npx tessl i tessl/npm-preact-compat