Skip to content

Instantly share code, notes, and snippets.

@viniciusCamargo
Forked from developit/htm-ssr.js
Created February 15, 2021 13:31
Show Gist options
  • Save viniciusCamargo/64187b35cab20fa45700cc1acf3e87ae to your computer and use it in GitHub Desktop.
Save viniciusCamargo/64187b35cab20fa45700cc1acf3e87ae to your computer and use it in GitHub Desktop.
const express = require('express');
const { html, Component } = require('htm/preact');
const renderToString = require('preact-render-to-string');
class App extends Component {
render(props) {
return html`
<div class="app">
<h1>This is an app</h1>
<p>Current server time: ${new Date + ''}</p>
</div>
`;
}
}
const app = express();
app.get('/', (request, response) => {
// render your app to an HTML string
const body = renderToString(html`<${App} url=${request.url} />`);
// wrap it in an HTML document and send it back
response.send(`<!DOCTYPE html><html><body>${body}</body></html>`);
});
{
"dependencies": {
"htm": "^2.2.1",
"preact": "^8.5.2",
"express": "^4.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment