Skip to content

Instantly share code, notes, and snippets.

@lkmill
Last active December 23, 2017 12:17
Show Gist options
  • Select an option

  • Save lkmill/39e3b131a208e1884507799bd3c4b0fe to your computer and use it in GitHub Desktop.

Select an option

Save lkmill/39e3b131a208e1884507799bd3c4b0fe to your computer and use it in GitHub Desktop.

Revisions

  1. lkmill revised this gist Dec 23, 2017. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions render-react.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    '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)
    }
  2. lkmill created this gist Jul 9, 2017.
    29 changes: 29 additions & 0 deletions render-dust.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    '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)
    }
    25 changes: 25 additions & 0 deletions render-hyperscript-jsx.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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))
    }
    }
    22 changes: 22 additions & 0 deletions render-peact-single.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    '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)
    }
    32 changes: 32 additions & 0 deletions render-preact-multiple.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    '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)
    }