spatie / server-side-rendering

Server side rendering JavaScript in a PHP application
https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php
MIT License
602 stars 34 forks source link

Error when "this" is not defined #62

Closed brandonsoccer22 closed 1 week ago

brandonsoccer22 commented 8 months ago

PR #49 seems to have introduced a bug when "this" is not defined.

Also for reference, I am compiling my code with Vite instead of Webpack.

Here is the error:

Error Output:
================
file://storage/app/ssr/816a2e2ab307342d248eac819f1c3211.js:3
};(function () { if (this.process != null) { return; } this.process = { env: {}, argv: [] }; }).call(null);process.env.NODE_ENV = "production";process.env.VUE_ENV = "server";var context = {"url":"\/"};function ih(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var Wc={exports:{}},xo={},Qc={exports:{}},I={};/**
                          ^

TypeError: Cannot read properties of null (reading 'process')
    at file://storage/app/ssr/816a2e2ab307342d248eac819f1c3211.js:3:27
    at file://storage/app/ssr/816a2e2ab307342d248eac819f1c3211.js:3:97
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12)

Node.js v21.6.0.

I was able to fix this error by making the following change to /src/Renderer.php on Line 191:

'(function () { if (!this || this.process != null) { return; } this.process = { env: {}, argv: [] }; }).call(null)',

Basically, return early if !this

zacwebb commented 4 months ago

Same thing happening to me when using Vite

spatie-bot commented 1 week ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.