Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot throw error with V8 #8

Open
grimpows opened this issue Feb 6, 2018 · 9 comments
Open

snapshot throw error with V8 #8

grimpows opened this issue Feb 6, 2018 · 9 comments

Comments

@grimpows
Copy link

grimpows commented Feb 6, 2018

by just remplacing the createContext function in 2.0.0

public function createContext($code, $cachename = null){
        if ($cachename) {
            $cacheItem = $this->cache->getItem($cachename);
            if ($cacheItem->isHit()) {
                $snapshot = $cacheItem->get();
            } else {
                $snapshot = \V8Js::createSnapshot($code);
                $cacheItem->set($snapshot);
                $this->cache->save($cacheItem);
            }
        } else {
            $snapshot = \V8Js::createSnapshot($code);
        }
}

by the one in 0.8.8

 public function createContext($code)
    {
        $this->context = $this->v8->compileString($code);
        $this->v8->executeScript($this->context);
    }
  • i know that snapshot apear to be really nice, but actually every time \V8Js::createSnapshot($code); is called, that throw a v8 error like the one reported on ReactBundle ...
    if you get a SSR working with \V8Js::createSnapshot($code); please give the version or additional optional requierement you setted up for V8

here is my dockerfile for get a exemple of my V8js installation (used https://github.com/phpv8/v8js/blob/php7/README.Linux.md )

FROM php:7.2-fpm

# install the PHP extensions we need
RUN apt-get update && apt-get install -y \
    openssl \
    git \
	unzip \
	build-essential \
	curl \
	python \
	libglib2.0-dev



# Install depot_tools first (needed for source checkout)
RUN cd /tmp &&\
	git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git


RUN cd /tmp &&\
	export PATH=`pwd`/depot_tools:"$PATH" && \
	fetch v8 &&\
	cd v8

RUN cd /tmp &&\
	export PATH=`pwd`/depot_tools:"$PATH" && \
	cd v8 &&\
	git checkout 6.6.155 &&\
	gclient sync

RUN apt-get install -y ninja-build

RUN cd /tmp && \
	export PATH=`pwd`/depot_tools:"$PATH" && \
	cd v8 &&\
	tools/dev/v8gen.py -vv x64.release -- is_component_build=true &&\
	ninja -C out.gn/x64.release/

RUN mkdir -p /opt/v8/lib && \
	mkdir -p /opt/v8/include && \
	cd /tmp/v8 && \
	cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin \
  	out.gn/x64.release/icudtl.dat /opt/v8/lib/ &&\
 	cp -R include/* /opt/v8/include/

RUN	apt-get install -y patchelf &&\
	for A in /opt/v8/lib/*.so; do patchelf --set-rpath '$ORIGIN' $A; done

RUN cd /tmp && \
	git clone https://github.com/phpv8/v8js.git && \
	cd v8js &&\
	phpize &&\
	./configure --with-v8js=/opt/v8 LDFLAGS="-lstdc++" &&\
	make &&\
	make test &&\
	make install

RUN	echo extension=v8js.so > /usr/local/etc/php/conf.d/v8js.ini

RUN docker-php-ext-install pdo_mysql \
	&& docker-php-ext-enable pdo_mysql

EXPOSE 9000

both thrown an error while the snapshoot call

  • note that everything working with node installed instead of V8js for SSR or with skipping \V8Js::createSnapshot($code); during SSR

  • i just tryed

<?php
$code = "var test = 3; console.log(test);";
$snapshot = \V8Js::createSnapshot($code);
$v8 =  new \V8Js('PHP', [], [], true, $snapshot);
?>

this do not throw an error, that appear the error should come from react or any dependency.
I get the same error when i'm using your symfony 4 react sandbox (phpExecJs ^2.0.0)

@jeremy-richard
Copy link

Hi, there. I'm dealing with the same issue when using Limenius/ReactBundle to do SSR with my Symfony 4 + Create React App.
The problem occurs only with Webpack development build. Everything works fine with the production build.

Here's some info :

  • PHP Version 7.1.13
  • v8js Version | 2.1.0
  • V8 Engine Compiled Version | 6.6.0

The index.js looks like this

import ReactOnRails from 'react-on-rails';
import App from './App';

ReactOnRails.register({ App });

And App.jsx

import React from 'react';

function App() {
  return <h1>Hello, world!</h1>;
}

export default App;

Do you need more info to solve this issue ?

@grimpows
Copy link
Author

grimpows commented Feb 7, 2018

can you show me your serverside config, for me production build dont work too

@jeremy-richard
Copy link

It's based on facebook/create-react-app so the configurations are the one used by react-scripts.

https://github.com/facebook/create-react-app/tree/next/packages/react-scripts/config
https://github.com/facebook/create-react-app/tree/next/packages/react-scripts/scripts

@grimpows
Copy link
Author

grimpows commented Feb 7, 2018

i'm using symfony/encore like the symfony_react_sandbox and last time i tried both prod and dev throw me the error.

var Encore = require('@symfony/webpack-encore');
var webpack = require('webpack')

Encore
    // directory where all compiled assets will be stored
    .setOutputPath('app/Resources/webpack')
    // what's the public path to this directory (relative to your project's document root dir)
    .setPublicPath('/')
    // empty the outputPath dir before each build
    .cleanupOutputBeforeBuild()
    // will output as web/build/app.js
    .addEntry('server-bundle', ['babel-polyfill', 
                                './client/client.js'
                            ])                 
    // allow legacy applications to use $/jQuery as a global variable
//    .enableReactPreset()

if(Encore.isProduction()){
    Encore.addPlugin(new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production')
    }))
}


// export the final configuration
//module.exports = Encore.getWebpackConfig();

var config = Encore.getWebpackConfig();

config.watchOptions = { poll: true, ignored: /node_modules/ };

module.exports = config;

@grimpows
Copy link
Author

grimpows commented Feb 7, 2018

i guess this should come from non declared global variable that react should use, but seem your configuration load them somewhere for prod ... if i could get prod working with V8js, i could use node during dev x')

@jeremy-richard
Copy link

OK, I'll keep you posted if I find a solution to have the development build working.
I try to avoid difference between development and producton, so I have to use V8, or node, on both environnements.
NodeJS for dev and V8JS for prod isn't a reliable solution for me.

Thank you.

@grimpows
Copy link
Author

grimpows commented Feb 9, 2018

if i can make production work, i guess we can add a snapshot parameter who is default to false, and be true if we are on production env (with app.php in sf3) or APP_EVN=prod (on sf4), i'm pretty sure the v0.8.8 function createContext work for you too, i'm trying to make my prod working with v8 and i'll try make a PR for this, and hoping nacmartin will see our issues x').

@grimpows
Copy link
Author

grimpows commented Feb 9, 2018

try replace directly in your vendor the createContext function from phpexecJs (vendor/nacmartin/PhpExecJs/Runtime/V8jsRuntime.php) by the exemple from v0.8.8 i writed above to be sure that we get the same issue. (ofc revert change after this or simply re-install your vendor by deleting vendor folder and re-launching a composer install after)

@FaritSlv
Copy link
Contributor

#13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants