Laravel SSR: ReferenceError: window is not defined



我正在尝试在我的Laravel React应用程序中实现服务器端渲染(SSR)。

我遵循本教程并使用space/laravel-server-side-rendering。即使在按顺序执行了每一步之后,SSR也不能为我工作,并且抛出了以下错误

命令"node/var/www/html/project/storage/app/ssr/8dd09f6fe4766c697e76479c162340d8 "失败了。退出代码:1(一般错误)工作目录:/var/www/html/project/public输出:================错误输出:================/var/www/html/project/store/app/ssr/8dd09f6fe4766c697e76479c162340d8.js:4 (()=>{var e={5318:e=>{e.exports= &&e. __esmodule ?e:{default:e}},e.exports.default=e.exports,e.exports. __esmodule =!0},862:(e,t,r)=>{var n=r(8).default;function i(e){if("function"!=typeof WeakMap)返回null;var t=new WeakMap,r=new WeakMap;return(i=function(e){return e?r:t})(e)} .exports=function(e,t){if(!t&&e&&e. __esmodule)返回e;if(null=== =e||"object"!==n(e)&&"function"!=typeof e)return{default:e};var r=i(t);if(r&& r.s has(e))return r.g edescriptor (e);var o={},a=Object.defineProperty&& Object.prototype.hasOwnProperty.call(e,s)){var l=a?Object.getOwnPropertyDescriptor(e,s) == && and amp; and amp;(l.get||l.set)} ?Object.defineProperty(o,s,l):o[s]=e[s]}return .default=e,r&& &ampt(r){return"function"==typeof Symbol& "symbol"==typeof Symbol.iterator?(e.exports=t=function(e){return typeof e},e.exports;参考错误:窗口没有定义在/var/www/html/project/storage/app/ssr/8dd09f6fe4766c697e76479c162340d8.js:4:1021194在/var/www/html/project/storage/app/ssr/8dd09f6fe4766c697e76479c162340d8.js:4:1566017 at Object。(/var/www/html/project/storage/app/ssr/8dd09f6fe4766c697e76479c162340d8.js:4:1566021) at Module。在Object.Module._extensions. js (internal/modules/cjs/loader.js:789:10) at Module. js加载(internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module。_load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) (View:/var/www/html/project/resources/views/index.blade.php)

webpack.mix.js

mix.js('resources/js/app.js', 'public/js')
.react()
.js('resources/js/components/main-server.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');

index.blade.php

<div id="root">
{!! ssr('js/main-server.js')->render()!!}
</div>

SSR .php (SSR配置文件)

<?php
return [
/*
* Enable or disable the server renderer. Enabled in production by default.
*/
'enabled' => env('APP_ENV') === 'production',
/*
* When server side rendering goes wrong, nothing will be rendered so the
* client script can render everything from scratch without errors. If
* `debug` is enabled, an exception will be thrown when the JavaScript can't
* be executed.
*/
'debug' => env('APP_DEBUG', true),
/*
* Set to true if you're using Laravel Mix, then you can pass a script
* identifier to `ssr` instead of a full path.
*/
'mix' => true,
/*
* The engine class is used to execute JavaScript. Node requires you to set
* up some extra configuration below. If you want to use the V8 engine, make
* sure the v8js php extension is available.
*/
'engine' => env('SSR_ENGINE', SpatieSsrEnginesNode::class),
/*
* Extra setup for the Node engine.
*/
'node' => [
'node_path' => env('NODE_PATH', '/usr/local/bin/node'),
'temp_path' => env('SSR_TEMP_PATH', storage_path('app/ssr')),
],
/*
* Context is used to pass data to the server script. Fill this array with
* data you *always* want to send to the server script. Context can contain
* anything that's json serializable.
*/
'context' => [],
/*
* Env is used to fill `process.env` when the server script is executed.
* Fill this array with data you *always* want to send to the server script.
* The env array is only allowed to be a single level deep, and can only
* contain primitive values like numbers, strings or booleans.
*
* By default, env is prefilled with some necessary values for server side
* rendering Vue applications.
*/
'env' => [
'NODE_ENV' => 'production',
'VUE_ENV' => 'server',
],
];

如果需要更多的信息,请告诉我。

你需要设置:

"启用"=比;真的,

。..

"引擎"=比;env (SSR_ENGINE, Spatie Ssr 引擎V8::类)


你仍然需要检查所有的浏览器对象,比如window文档和

最新更新