phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.83k stars 200 forks source link

V8Js::compileString():14026: ReferenceError: window is not defined #359

Closed kkyu27 closed 6 years ago

kkyu27 commented 6 years ago
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class AppController extends Controller
{
  private function render() {
   $renderer_source = File::get(base_path('node_modules/vue-server-renderer/basic.js'));
   $app_source = File::get(public_path('js/entry-server.js'));

   $v8 = new \V8Js();

   ob_start();

   $v8->executeString('var process = { env: { VUE_ENV: "server", NODE_ENV: "production" }}; this.global = { process: process };');
   $v8->executeString($renderer_source);
   $v8->executeString($app_source);

   return ob_get_clean();
 }

 public function get() {
   $ssr = $this->render();
   return view('app', ['ssr' => $ssr]);
 }
}

I followed https://dzone.com/articles/server-side-rendering-with-laravel-amp-vuejs-25 this page and installed V8Js. But $v8->executeString($app_source); make an error that V8Js::compileString():14026: ReferenceError: window is not defined.

I have no idea how can I handle this error..

stesie commented 6 years ago

Looks like things are (meanwhile) wrong with the tutorial -- haven't tried it out myself.

Yet the JS source tries to access window which is a browser API thing and not JS core itself. So you need to modify the JS code you execute via executeString to not directly access window. Either you don't perform the access at all or you provide a shim object.

kkyu27 commented 6 years ago

thank you, I realized what is the problem.

dinhhoainam23894 commented 6 years ago

kkyu27 How did you solve this problem? I also have problems like this . tks

kkyu27 commented 6 years ago

@dinhhoainam23894 hey, check your vue.js or app.js

the cause was using Window object like bootstrap before serverisde sentence. try annotation each line then you are gonna see ssr is worked!