anthonygore / vue-js-laravel-ssr

Source code for the article "Server-Side Rendering With Laravel & Vue.js 2.5"
https://vuejsdevelopers.com/2017/11/06/vue-js-laravel-server-side-rendering/
70 stars 18 forks source link

AppController $this->render() returns "undefined" #1

Open floodedcodeboy opened 6 years ago

floodedcodeboy commented 6 years ago

Hi,

So your tutorial came around at a great time, thank you for putting it together and sharing. ! I'm busy working on a project that requires SSR and Laravel and I'm not that wet behind the ears when it comes to SSR.

I have followed your tutorial to the "tee", making only slight adjustments here and there for paths (which I've been very careful to double check to make sure they're not the source of this issue) . I've installed V8 and V8js successfully (this was probably the most tiresome part).

The issue that I'm having is this inside the AppController.php when I call $this->render() it returns 'undefined' which is not what I was expecting.

I've been over your tutorial at least ten times - checking all my files making sure they match up with yours, making sure that entry-client.js and entry-server.js can both be read (they can) and I still can't see why this is happening... I've tried other things inside the render() function and can confirm that v8js does work! ... I wonder... might you have any ideas?

I'm not sure that this helps, but something else I tried was removing the following lines from the `render()' function :

$v8->executeString($renderer_source);
$v8->executeString($app_source);

leaving only:

$v8->executeString($js);

and then outputting that in the get() function which again returned 'undefined'.

I'm completely out of ideas. Any help would be massively appreciated. Thanks in advance.

<?php
namespace App\Http\Controllers;

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();

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

    public function get() {
        $ssr = $this->render();
        dd($ssr);
    }
}
anthonygore commented 6 years ago

Hey,

With V8Js you'll need to use the print method otherwise render will always return undefined. Perhaps try $v8->executeString('print("test");')?

If you still can't figure it out, link me to your code repo and I'll see if I can debug it.

floodedcodeboy commented 6 years ago

Hey, sorry I've just seen this.

I'll give the print method a try.. but that said; I've used the code from your tutorial.

I'll update you on my findings.

Thanks!