Skip to content

Instantly share code, notes, and snippets.

@koriym
Last active June 1, 2017 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koriym/ac590f3305a736bff2d5a88329f4d77e to your computer and use it in GitHub Desktop.
Save koriym/ac590f3305a736bff2d5a88329f4d77e to your computer and use it in GitHub Desktop.
$cache = new FilesystemCache() // PSR-16 simple cache
$baracoa = new CacheBaracoa($appBundleJsPath, new ExceptionHandler(), $cache);
$html = $baracoa->render('min', ['name' => 'World']);
echo $html; // Hello World
$code = sprintf('
%s
var window = this;
render(%s);',
file_get_contents(__DIR__ . "/ssr.bundle.js"),
json_encode(['hello' =>['name' => 'World']])
);
echo (new V8Js)->executeString($code);
<div id="root"><div data-reactroot="" data-reactid="1" data-react-checksum="410796212"><h1 data-reactid="2"><!-- react-text: 3 -->Hello <!-- /react-text --><!-- react-text: 4 -->World<!-- /react-text --></h1><button data-reactid="5">Click</button></div></div>
window.__PRELOADED_STATE__ = {"hello":{"name":"World"}}
global.render = (state, metas) => ('<html>..</html>');
use Nacmartin\PhpExecJs\PhpExecJs;
$phpexecjs = new PhpExecJs();
print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')"));
// Array
// (
// [0] => red
// [1] => yellow
// [2] => blue
// )
// READMEより
use Koriym\Baracoa\ExceptionHandler;
use Koriym\Baracoa\Baracoa;
$baracoa = new Baracoa($jsDir, new ExceptionHandler());
$html = $baracoa->render('min', ['name' => 'World']);
echo $html; // Hello World
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
import App from '../containers/App';
const preloadedState = window.__PRELOADED_STATE__;
const store = configureStore(preloadedState);
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'),
);
import render from './render';
global.render = render;
const render = state => (
`Hello ${state.name}`
)
import React from 'react';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';
import escape from 'escape-html';
import serialize from 'serialize-javascript';
import App from '../containers/App';
import configureStore from '../store/configureStore';
const render = (state, metas) => {
const store = configureStore(preloadedState);
const root = renderToString(
<Provider store={store}>
<App />
</Provider>,
);
return `<!doctype html>
<html>
<head>
<title>${escape(metas.title)}</title>
</head>
<body>
<div id="root">${root}</div>
<script>
window.__PRELOADED_STATE__ = ${serialize(state)}
</script>
<script src="/build/index.bundle.js"></script>
</body>
</html>
`;
};
export default render;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment