Last active
July 25, 2023 12:02
-
-
Save developit/6e117d53f4f32b8f1e63bb43f5f6e937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var preact = require('preact'); | |
var createRenderer = require('./preact-dom-renderer'); | |
// set up a rendering target | |
var renderer = createRenderer(); | |
// render some stuff into the internal DOM | |
renderer.render(preact.h(PreactApp, pageConfig)); | |
// get a snapshot of the current HTML | |
console.log( renderer.html() ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var preact = require('preact'); | |
var undom = require('undom'); | |
/** | |
* Prototype stateful server renderer. | |
*/ | |
var doc; | |
module.exports = function createRenderer() { | |
if (!doc) { | |
doc = undom(); | |
Object.assign(global, doc.defaultView); | |
} | |
var root, parent = doc.createElement('x-root'); | |
doc.body.appendChild(parent); | |
return { | |
render: function(jsx) { | |
root = preact.render(jsx, parent, root); | |
return this; | |
}, | |
html: function() { | |
return serializeHtml(root); | |
} | |
}; | |
} | |
function serializeHtml(el) { | |
if (el.nodeType===3) return esc(el.nodeValue); | |
var name = String(el.nodeName).toLowerCase(), | |
str = '<'+name, | |
hasClass = false, | |
c, i; | |
for (i=0; i<el.attributes.length; i++) { | |
let name = el.attributes[i].name; | |
if (name==='class') hasClass = true; | |
str += ' '+name+'="'+esc(el.attributes[i].value)+'"'; | |
} | |
if (el.className && !hasClass) str += ' class="'+el.className+'"'; | |
str += '>'; | |
for (i=0; i<el.childNodes.length; i++) { | |
c = serializeHtml(el.childNodes[i]); | |
if (c) str += c; | |
} | |
return str + '</'+name+'>'; | |
} | |
function esc(str) { return String(str).replace(/[&<>"']/g, escMap); } | |
function escMap(s) { return '&'+map[s]+';'; } | |
var map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed!