PHPackages                             guansunyata/server-side-rendering - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. guansunyata/server-side-rendering

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

guansunyata/server-side-rendering
=================================

Server side rendering JavaScript in your PHP application

0.3.1(7y ago)11431MITPHPPHP ^7.0

Since Jan 18Pushed 7y ago1 watchersCompare

[ Source](https://github.com/guAnsunyata/server-side-rendering)[ Packagist](https://packagist.org/packages/guansunyata/server-side-rendering)[ Docs](https://github.com/guansunyata/server-side-rendering)[ RSS](/packages/guansunyata-server-side-rendering/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (7)Dependencies (2)Versions (20)Used By (1)

Server side rendering JavaScript in your PHP application
========================================================

[](#server-side-rendering-javascript-in-your-php-application)

[![Latest Version on Packagist](https://camo.githubusercontent.com/440e9f787c7a56bfb273ed8ae98a24b98e007eaf50ff2b15b2a26ecbc3d79989/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f7365727665722d736964652d72656e646572696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/server-side-rendering)[![Build Status](https://camo.githubusercontent.com/11df66ecbecf39bc9a8b28f2a7a33c70bdea0a88f47bf4af50fe65ff66245292/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f7365727665722d736964652d72656e646572696e672f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/server-side-rendering)[![SensioLabsInsight](https://camo.githubusercontent.com/4f53b6dcfb7c41afddd5b2bbe4e49d0b9b2bb94a221b718b1dc51afeecb37fc4/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f7878787878787878782e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/xxxxxxxxx)[![Quality Score](https://camo.githubusercontent.com/beecdbba9ea732ffe6aab0eef81e6d84aa9bbca8e3b8da4f8cef906cf0805c2b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f7365727665722d736964652d72656e646572696e672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/server-side-rendering)[![Total Downloads](https://camo.githubusercontent.com/09afa3403e6eeb866041388c1ce63efbf361c750f978e9b9e97cd93f6f386442/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f7365727665722d736964652d72656e646572696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/server-side-rendering)

```
use Spatie\Ssr\Renderer;
use Spatie\Ssr\Engines\V8;

$engine = new V8();

$renderer = new Renderer($engine);

echo $renderer
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->render();

// My server rendered app!
```

- Works with any JavaScript framework that allows for server side rendering
- Runs with or without the V8Js PHP extension
- Requires minimal configuration

If you're building a Laravel app, check out the [laravel-server-side-rendering](https://github.com/spatie/laravel-server-side-rendering) package instead.

This readme assumes you already have some know-how about building server rendered JavaScript apps.

Who's this package for?
-----------------------

[](#whos-this-package-for)

Server side rendering (SSR) can be hard, and non-trivial to enable in your JavaScript application. Before using this library, make sure you know what you're getting in to. Alex Grigoryan has a [pretty concise article](https://medium.com/walmartlabs/the-benefits-of-server-side-rendering-over-client-side-rendering-5d07ff2cefe8) on the benefits and caveats of SSR. Anthony Gore also has a great article on [server side rendering a Vue application in Laravel](https://vuejsdevelopers.com/2017/11/06/vue-js-laravel-server-side-rendering/), which inspired this library.

In case you're in need of a refresher...

- SSR reduces the time until the [first meaningful paint](https://www.quora.com/What-does-First-Meaningful-Paint-mean-in-Web-Performance), providing a better experience for your users
- SSR is recommended if you need to tailor your app for crawlers that can't execute JavaScript (SEO)
- SSR adds a meaningful amount of complexity to your application
- SSR can increase response times and the overall load on your server

When you've got an answer to the "Do I need SSR?" question, ask yourself if you need SSR in a PHP application. Benefits of rendering your app in a PHP runtime are:

- Access to your application's session &amp; state, which you normally don't if your SPA is consuming a stateless api
- Reduced infrastructure complexity because you don't need to maintain a node server

If you're building a SPA that connects to an external API, and the PHP runtime doesn't provide any extra value, you're probably better off using a battle tested solution like [Next.js](https://github.com/zeit/next.js/) or [Nuxt.js](https://nuxtjs.org).

As a final disclaimer, judging by the amount—well, lack—of people blogging about rendering JavaScript applications in PHP, this whole setup is uncharted territory. There may be more unknown caveats lurking around the corner.

If you're still sure you want to keep going, please continue!

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/server-side-rendering
```

Usage
-----

[](#usage)

### Your JavaScript app's architecture

[](#your-javascript-apps-architecture)

This guide assumes you already know how to build a server-rendered application. If you're looking for reading material on the subject, Vue.js has a [very comprehensive guide](https://ssr.vuejs.org/en/) on SSR. It's Vue-specific, but the concepts also translate to other frameworks like React.

### Engines

[](#engines)

An engine executes a JS script on the server. This library ships with two engines: a `V8` engine which wraps some `V8Js` calls, so you'll need to install a PHP extension for this one, and a `Node` engine which builds a node script at runtime and executes it in a new process. An engine can run a script, or an array of multiple scripts.

The `V8` engine is a lightweight wrapper around the `V8Js` class. You'll need to install the [v8js extension](https://github.com/phpv8/v8js) to use this engine.

The `Node` engine writes a temporary file with the necessary scripts to render your app, and executes it in a node.js process. You'll need to have [node.js](https://nodejs.org) installed to use this engine.

### Rendering options

[](#rendering-options)

You can chain any amount of options before rendering the app to control how everything's going to be displayed.

```
echo $renderer
    ->disabled($disabled)
    ->context('user', $user)
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->render();
```

#### `enabled(bool $enabled = true): $this`

[](#enabledbool-enabled--true-this)

Enables or disables server side rendering. When disabled, the client script and the fallback html will be rendered instead.

#### `debug(bool $debug = true): $this`

[](#debugbool-debug--true-this)

When debug is enabled, JavaScript errors will cause a php exception to throw. Without debug mode, the client script and the fallback html will be rendered instead so the app can be rendered from a clean slate.

#### `entry(string $entry): $this`

[](#entrystring-entry-this)

The path to your server script. The contents of this script will be run in the engine.

#### `context($context, $value = null): $this`

[](#contextcontext-value--null-this)

Context is passed to the server script in the `context` variable. This is useful for hydrating your application's state. Context can contain anything that json-serializable.

```
echo $renderer
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->context('user', ['name' => 'Sebastian'])
    ->render();
```

```
// app-server.js

store.user = context.user // { name: 'Sebastian' }

// Render the app...
```

Context can be passed as key &amp; value parameters, or as an array.

```
$renderer->context('user', ['name' => 'Sebastian']);
```

```
$renderer->context(['user' => ['name' => 'Sebastian']]);
```

#### `env($env, $value = null): $this`

[](#envenv-value--null-this)

Env variables are placed in `process.env` when the server script is executed. Env variables must be primitive values like numbers, strings or booleans.

```
$renderer->env('NODE_ENV', 'production');
```

```
$renderer->env(['NODE_ENV' => 'production']);
```

#### `fallback(string $fallback): $this`

[](#fallbackstring-fallback-this)

Sets the fallback html for when server side rendering fails or is disabled. You can use this to render a container for the client script to render the fresh app in.

```
$renderer->fallback('');
```

#### `resolveEntryWith(callable $resolver): $this`

[](#resolveentrywithcallable-resolver-this)

Add a callback to transform the entry when it gets resolved. It's useful to do this when creating the renderer so you don't have to deal with complex paths in your views.

```
echo $renderer
    ->resolveEntryWith(function (string $entry): string {
        return __DIR__."/../../public/js/{$entry}-server.js";
    })
    ->entry('app')
    ->render();
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

Support us
----------

[](#support-us)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~20 days

Recently: every ~3 days

Total

16

Last Release

2729d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9b5cead4cebadfe7dca95f383c8914a8db7bd2d8c19151d90a0f17966e6c5b78?d=identicon)[guansunyata](/maintainers/guansunyata)

---

Top Contributors

[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (37 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (8 commits)")[![robinvdvleuten](https://avatars.githubusercontent.com/u/238295?v=4)](https://github.com/robinvdvleuten "robinvdvleuten (2 commits)")[![guAnsunyata](https://avatars.githubusercontent.com/u/11704825?v=4)](https://github.com/guAnsunyata "guAnsunyata (2 commits)")[![vmitchell85](https://avatars.githubusercontent.com/u/1248035?v=4)](https://github.com/vmitchell85 "vmitchell85 (1 commits)")[![edbizarro](https://avatars.githubusercontent.com/u/84926?v=4)](https://github.com/edbizarro "edbizarro (1 commits)")[![jdreesen](https://avatars.githubusercontent.com/u/424602?v=4)](https://github.com/jdreesen "jdreesen (1 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (1 commits)")

---

Tags

spatieSSRserver-side-rendering

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/guansunyata-server-side-rendering/health.svg)

```
[![Health](https://phpackages.com/badges/guansunyata-server-side-rendering/health.svg)](https://phpackages.com/packages/guansunyata-server-side-rendering)
```

###  Alternatives

[spatie/server-side-rendering

Server side rendering JavaScript in your PHP application

631780.3k3](/packages/spatie-server-side-rendering)[spatie/laravel-package-tools

Tools for creating Laravel packages

935125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M626](/packages/spatie-laravel-data)[spatie/typescript-transformer

This is my package typescript-transformer

3706.5M16](/packages/spatie-typescript-transformer)[spatie/macroable

A trait to dynamically add methods to a class

72659.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
