jedireza / hapi-react-views

:package: A hapi view engine for React components
MIT License
231 stars 33 forks source link

additional rendering options #75

Closed travi closed 5 years ago

travi commented 5 years ago

would you consider supporting additional rendering approaches?

i'd like to use emotion and support SSR of styles, which would require processing with emotion after rendering the component.

i'm sure you dont want to couple this plugin to emotion, so the best option might be to allow providing a function for renderMethod in addition to the current string options. it could expect to receive ViewElement and context as input and return the viewOutput string.

for example:

import { renderToString } from 'react-dom/server'
import { renderStylesToString } from 'emotion-server'

const context = { name: 'Steve' };
const renderOpts = {
    runtimeOptions: {
        doctype: '<!DOCTYPE html>',
        renderMethod: (ViewElement, context) => {
          return renderStylesToString(renderToString(ViewElement(context)));
        }
    }
};

const output = await server.render('template', context, renderOpts);

i'd be open to sending a PR if this seems like a reasonable option to you.

jedireza commented 5 years ago

This is interesting. I'd like to see what support for something like this looks like. Feel free to submit a PR.

travi commented 5 years ago

sorry that i havent gotten around to submitting anything for this. i could see it being valuable for some things, but my specific use case is no longer needed since emotion 10 no longer requires special handling for SSR so i'm going to close this out