Server Side Rendering Javascript in PHP application with V8Js.
- PHP >= 7.1
- V8Js PHP extension (See V8 Javascript compiled files for PHP)
Using Composer:
$ composer require davidecesarano/embryo-ssr
You can use SSR if you want rendering a "snapshot" of our app. The JavaScript frameworks (like Vuejs or React) build client-side applications that manipulate DOM in the browser as output.
With Embryo SSR is possible to render the components into HTML strings on the server, send them directly to the browser, and finally "hydrate" the static markup into a fully interactive app on the client.
use Embryo\ServerSideRendering\SSR;
$v8js = new \V8Js;
$ssr = new SSR($v8js);
echo $ssr
->env([
'NODE_ENV' => 'production',
'VUE_ENV' => 'server'
])
->context([
'user' => [
'name' => 'Davide'
]
])
->entry('path/to/js/entry-server.js')
->render();
You may quickly test using the built-in PHP server going to http://localhost:8000.
$ cd example
$ php -S localhost:8000
Enables or disables server side rendering. When disabled, the client script and the fallback html will be rendered instead.
ENV variables are placed in process.env
when the server script is executed.
The path to your server script. Must be an arrary for multiple files.
Context is passed to the server script in the context
variable.
Writes a javascript script. For example: var a = "My Var"
.
Sets the fallback html for when server side rendering is disabled. For example: <div id="app"></div>
.