Git Product home page Git Product logo

jsrunner's Introduction

Server side rendering JavaScript in your PHP application

Latest Version on Packagist Build Status Total Downloads

use Lame\JSRunner\Renderer;
use Lame\JSRunner\V8Engine as V8;

$engine = new V8();

$renderer = new Renderer($engine);

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

// <div>My server rendered app!</div>
  • Works with any JavaScript framework that allows for server side rendering
  • Runs with or without the V8Js PHP extension
  • Requires minimal configuration This readme assumes you already have some know-how about building server rendered JavaScript apps.

Who's this package for?

  • Projects that want to call JavaScript from PHP seamlessly.
  • Bilingual(PHP and JS/TS) projects that want to render their JS/TS on the server side.

Installation

You can install the package via composer:

composer require lanre/jsrunner

Usage

Engines

An engine executes a JS script on the server. This library ships with three engines: V8Engine which wraps some V8Js calls, so you'll need to install a PHP extension for this one, BunEngine and NodeEngine which build a bun/node script (respectively) at runtime and executes it in a new process. An engine can run a script, or an array of multiple scripts.

V8Engine is a lightweight wrapper around the V8Js class. You'll need to install the v8js extension to use this engine.

BunEngine and NodeEngine write a temporary file with the necessary scripts to render your app, and executes it

in the appropriate environment. You'll need to have node.js (for NodeEngine) or bun (for BunEngine) installed to use this engine.

Rendering options

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

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

enable(): $this

Enables server side rendering. This is the default state.

disable(): $this

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

enableIf(bool $enabled = true): $this

Conditionally enables server side rendering.

debug(bool $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

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

context($context, $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 & value parameters, or as an array.

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

env($env, $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

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('<div id="app"></div>');

resolveEntryWith(callable $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

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

jsrunner's People

Contributors

oplanre avatar renovate[bot] avatar

Stargazers

Joey ๐Ÿ˜Ž avatar

Watchers

 avatar

jsrunner's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

composer
composer.json
  • php ^8.1
  • symfony/process ^7.0
  • phpunit/phpunit ^11.0
github-actions
.github/workflows/php.yml
  • actions/checkout v4
  • oven-sh/setup-bun v1
  • shivammathur/setup-php 2.30.0
  • actions/cache v4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.