0
    <?php

    namespace App\Http\Controllers\Auth;

    use Illuminate\Support\Str;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Model\User;
    use Illuminate\Support\Facades\Auth;
    use Socialize;
    use Redirect;
    use Input;
    use Mail;
    use App\Mail\verifyEmail;
    use Carbon\Carbon;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\Facades\File;

    class CustomAuthController extends Controller
    {
    // Register form

    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]);
      }
}

我跟着https://dzone.com/articles/server-side-rendering-with-laravel-amp-vuejs-25并安装了 v8js。但错误是 $v8->executeString($app_source); 出错“V8Js::compileString():9272: ReferenceError: window is not defined”

如何修复这个错误。我不知道

=================================================

更新我可以运行它。我通过删除更改 app.js 中的代码

/** after i delete it.i can run with server side rendering
  require('./bootstrap');
  require('./bulma-carousel.min')
  window.Vue = require('vue');
  can you tell me why ?*/



  import App from './components/App.vue';
  import VeeValidate from 'vee-validate';
  import Vue from 'vue';
  import router from './router'

  Vue.use(VeeValidate);
  /**
   * Next, we will create a fresh Vue application instance and attach it to
   * the page. Then, you may begin adding components to this application
   * or customize the JavaScript scaffolding to fit your unique needs.
   */

  Vue.component('index-component', require('./components/index.vue'));

  export function createApp() {
      return new Vue({
        render: h => h(App)
      });
  }
4

1 回答 1

4

问题很简单。您请求的引导文件可能window在某处使用元素。同样在这一行上window.Vue = require('vue');,您正在使用window全局对象。Window 作为全局对象在浏览器中可用,但是在服务器上运行的 javascript 没有该window对象。因此错误。这就是我们需要为服务器和客户端制作单独的 javascript 文件的原因。尝试咨询您实现的软件包,了解它们对服务器端渲染的支持。

尝试一一禁用各种导入,并找到破坏您对 SSR 支持的那些并修复它们或找到替代方案。

于 2018-06-24T10:15:48.050 回答