Skip to content

Instantly share code, notes, and snippets.

@lkmill
Last active December 23, 2017 12:17
Show Gist options
  • Save lkmill/39e3b131a208e1884507799bd3c4b0fe to your computer and use it in GitHub Desktop.
Save lkmill/39e3b131a208e1884507799bd3c4b0fe to your computer and use it in GitHub Desktop.
render
'use strict'
const Promise = require('bluebird')
const dust = require('dustjs-linkedin')
dust.renderPromise = Promise.promisify(dust.render, { context: dust })
module.exports = async function (template, master) {
const locals = Object.assign({}, this.app.locals, this.locals)
console.log(locals)
let html
if (this.cache) {
html = this.cache.html
}
if (!html && template) {
html = await dust.renderPromise(template, locals)
}
// always render master, since shim mw will add
// scripts depending on user agent string
if (typeof master === 'function') {
html = master(Object.assign({ html }, locals))
}
this.send(html)
}
const { h } = require('hyperscript-jsx/string')
module.exports = function (Master, Component) {
const locals = Object.assign({ query: this.req.query }, this.app.locals, this.locals)
if (typeof Master === 'function') {
if (typeof Component === 'function') {
return this.send(
h(Master, locals,
h(Component, locals)
)
)
}
Component = Master
}
if (typeof Component !== 'function') {
throw new Error('Not a Component')
} else if (Component.prototype && Component.prototype.render) {
const i = new Component(locals)
this.send(i.render(i.props, i.state))
} else {
this.send(Component(locals))
}
}
'use strict'
const { h } = require('preact')
const { render } = require('preact-render-to-string')
module.exports = function (Component, Master) {
const locals = Object.assign({}, this.app.locals, this.locals)
let html
if (!html && typeof Component === 'function') {
html = render(h(Component, locals))
}
// always render master, since shim mw will add
// scripts depending on user agent string
if (typeof Master === 'function') {
html = Master(Object.assign({ html }, locals))
}
this.send(html)
}
'use strict'
const { h } = require('preact')
const { render } = require('preact-render-to-string')
module.exports = function (Master, ...Components) {
// warning: without a full merge the following will allow master or
// components to mutate the locals. but that shoulds really matter.
const locals = Object.assign({}, this.app.locals, this.locals)
let html
if (Components.length) {
let dom = Components.reverse().reduce((result, Component) => {
if (Array.isArray(Component)) {
return Component.map((Component) => h(Component, locals, result))
}
return h(Component, locals, result)
}, null)
html = render(dom)
}
// even if same master is past ovr n ovr we duz not cash it cuz stufs like
// skripts mite change becuz of stufs like user agent string n uddr stuf
if (typeof Master === 'function') {
html = Master(Object.assign({ html }, locals))
}
this.send(html)
}
'use strict'
const React = require('react')
const ReactDOMServer = require('react-dom/server')
module.exports = function (Master, ...Components) {
const locals = Object.assign({}, this.app.locals, this.locals)
let html
if (this.cache) {
html = this.cache.html
}
console.log(Components)
if (!html && Components.length) {
const dom = Components.reverse().reduce((result, Component) => {
if (Array.isArray(Component)) {
return Component.map((Component) => React.createElement(Component, locals, result))
}
return React.createElement(Component, locals, result)
}, null)
console.log(dom)
html = ReactDOMServer.renderToString(dom)
if (this.cache) this.cache.html = html
}
// always render master, since shim mw will add
// scripts depending on user agent string
if (typeof Master === 'function') {
html = Master(Object.assign({ html }, locals))
}
this.send(html)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment