{"id":13828351,"url":"https://github.com/spatie/server-side-rendering","last_synced_at":"2025-05-14T05:06:25.344Z","repository":{"id":41233147,"uuid":"117815473","full_name":"spatie/server-side-rendering","owner":"spatie","description":"Server side rendering JavaScript in a PHP application","archived":false,"fork":false,"pushed_at":"2025-04-24T08:38:12.000Z","size":76,"stargazers_count":613,"open_issues_count":0,"forks_count":36,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-04-24T09:35:47.592Z","etag":null,"topics":["javascript","php","server-side-rendering","ssr"],"latest_commit_sha":null,"homepage":"https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-17T09:37:07.000Z","updated_at":"2025-04-24T08:38:03.000Z","dependencies_parsed_at":"2024-01-18T05:23:36.836Z","dependency_job_id":"3d312436-a9fb-46f7-a60d-60868f609a28","html_url":"https://github.com/spatie/server-side-rendering","commit_stats":{"total_commits":67,"total_committers":14,"mean_commits":4.785714285714286,"dds":0.5223880597014925,"last_synced_commit":"1001957085c9523530de0214ceeecf481dbc49ee"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fserver-side-rendering","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fserver-side-rendering/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fserver-side-rendering/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fserver-side-rendering/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/server-side-rendering/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076649,"owners_count":22010611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","php","server-side-rendering","ssr"],"created_at":"2024-08-04T09:02:42.610Z","updated_at":"2025-05-14T05:06:25.300Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"# Server side rendering JavaScript in your PHP application\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/server-side-rendering.svg?style=flat-square)](https://packagist.org/packages/spatie/server-side-rendering)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/spatie/server-side-rendering/run-tests.yml?branch=main\u0026label=Tests)](https://github.com/spatie/server-side-rendering/actions?query=workflow%3ATests+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/server-side-rendering.svg?style=flat-square)](https://packagist.org/packages/spatie/server-side-rendering)\n\n```php\nuse Spatie\\Ssr\\Renderer;\nuse Spatie\\Ssr\\Engines\\V8;\n\n$engine = new V8();\n\n$renderer = new Renderer($engine);\n\necho $renderer\n    -\u003eentry(__DIR__.'/../../public/js/app-server.js')\n    -\u003erender();\n\n// \u003cdiv\u003eMy server rendered app!\u003c/div\u003e\n```\n\n- Works with any JavaScript framework that allows for server side rendering\n- Runs with or without the V8Js PHP extension\n- Requires minimal configuration\n\nIf you're building a Laravel app, check out the [laravel-server-side-rendering](https://github.com/spatie/laravel-server-side-rendering) package instead.\n\nThis readme assumes you already have some know-how about building server rendered JavaScript apps.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/server-side-rendering.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/server-side-rendering)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Who's this package for?\n\nServer 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.\n\nIn case you're in need of a refresher...\n\n- 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\n- SSR is recommended if you need to tailor your app for crawlers that can't execute JavaScript (SEO)\n- SSR adds a meaningful amount of complexity to your application\n- SSR can increase response times and the overall load on your server\n\nWhen 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:\n\n- Access to your application's session \u0026 state, which you normally don't if your SPA is consuming a stateless api\n- Reduced infrastructure complexity because you don't need to maintain a node server\n\nIf 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).\n\nAs 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.\n\nIf you're still sure you want to keep going, please continue!\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/server-side-rendering\n```\n\n## Usage\n\n### Your JavaScript app's architecture\n\nThis 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.\n\n### Engines\n\nAn 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.\n\nThe `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.\n\nThe `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.\n\n### Rendering options\n\nYou can chain any amount of options before rendering the app to control how everything's going to be displayed.\n\n```php\necho $renderer\n    -\u003edisabled($disabled)\n    -\u003econtext('user', $user)\n    -\u003eentry(__DIR__.'/../../public/js/app-server.js')\n    -\u003erender();\n```\n\n#### `enabled(bool $enabled = true): $this`\n\nEnables or disables server side rendering. When disabled, the client script and the fallback html will be rendered instead.\n\n#### `debug(bool $debug = true): $this`\n\nWhen 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.\n\n#### `entry(string $entry): $this`\n\nThe path to your server script. The contents of this script will be run in the engine.\n\n#### `context($context, $value = null): $this`\n\nContext 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.\n\n```php\necho $renderer\n    -\u003eentry(__DIR__.'/../../public/js/app-server.js')\n    -\u003econtext('user', ['name' =\u003e 'Sebastian'])\n    -\u003erender();\n```\n\n```js\n// app-server.js\n\nstore.user = context.user // { name: 'Sebastian' }\n\n// Render the app...\n```\n\nContext can be passed as key \u0026 value parameters, or as an array.\n\n```php\n$renderer-\u003econtext('user', ['name' =\u003e 'Sebastian']);\n```\n\n```php\n$renderer-\u003econtext(['user' =\u003e ['name' =\u003e 'Sebastian']]);\n```\n\n#### `env($env, $value = null): $this`\n\nEnv variables are placed in `process.env` when the server script is executed. Env variables must be primitive values like numbers, strings or booleans.\n\n```php\n$renderer-\u003eenv('NODE_ENV', 'production');\n```\n\n```php\n$renderer-\u003eenv(['NODE_ENV' =\u003e 'production']);\n```\n\n#### `fallback(string $fallback): $this`\n\nSets 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.\n\n```php\n$renderer-\u003efallback('\u003cdiv id=\"app\"\u003e\u003c/div\u003e');\n```\n\n#### `resolveEntryWith(callable $resolver): $this`\n\nAdd 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.\n\n```php\necho $renderer\n    -\u003eresolveEntryWith(function (string $entry): string {\n        return __DIR__.\"/../../public/js/{$entry}-server.js\";\n    })\n    -\u003eentry('app')\n    -\u003erender();\n```\n\n### Testing\n\n```bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n### Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Postcardware\n\nYou'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.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fserver-side-rendering","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fserver-side-rendering","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fserver-side-rendering/lists"}