Last active
June 1, 2017 06:47
-
-
Save koriym/ac590f3305a736bff2d5a88329f4d77e to your computer and use it in GitHub Desktop.
PHPのV8JsでSSR ref: http://qiita.com/koriym/items/2b9d0eeb68ca544f84b7
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
$cache = new FilesystemCache() // PSR-16 simple cache | |
$baracoa = new CacheBaracoa($appBundleJsPath, new ExceptionHandler(), $cache); | |
$html = $baracoa->render('min', ['name' => 'World']); | |
echo $html; // Hello World |
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
$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); |
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
<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> |
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
window.__PRELOADED_STATE__ = {"hello":{"name":"World"}} |
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
global.render = (state, metas) => ('<html>..</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
use Nacmartin\PhpExecJs\PhpExecJs; | |
$phpexecjs = new PhpExecJs(); | |
print_r($phpexecjs->evalJs("'red yellow blue'.split(' ')")); | |
// Array | |
// ( | |
// [0] => red | |
// [1] => yellow | |
// [2] => blue | |
// ) | |
// READMEより |
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
use Koriym\Baracoa\ExceptionHandler; | |
use Koriym\Baracoa\Baracoa; | |
$baracoa = new Baracoa($jsDir, new ExceptionHandler()); | |
$html = $baracoa->render('min', ['name' => 'World']); | |
echo $html; // Hello World |
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
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'), | |
); |
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
import render from './render'; | |
global.render = render; |
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
const render = state => ( | |
`Hello ${state.name}` | |
) |
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
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