Code Monkey home page Code Monkey logo

laravel-react-middleware's Introduction

Laravel React Compiler Middleware

Latest Stable Version Build Status Coverage Status

This is a Laravel middleware used to help with React Server-side Rendering. This middleware makes an HTTP request to a local Node.js server compiling the React application. It then injects the returned HTML into your Laravel view. Use Blade Templates as you would normally.

How it Works

First install Node.js on your production server or on a server on the same local network. Then write a small Node.js application to render your React application. For simple applications, you can use ReactDOM.server.renderToString(App), for more complex React applications you may need to do something more like this.

On the Laravel side, build a Blade or plain PHP layout file as usual.

<!-- Stored in resources/views/layouts/master.blade.php -->

<html>
    <head>
        <title>{{ $title or 'Untitled Document' }}</title>
    </head>
    <body>
        <div id="app">
            {!! $content or '' !!}
        </div>
    </body>
</html>

Render your views in your controller method as usual.

return view('layouts.main', ['title' => 'Your title here.']);

Add this middleware to whichever routes contain a React app.

Route::get('/', ['middleware' => 'react', 'uses' => 'HomeController@index']);

From there, the React middleware will make a request to your Node.js compiler server and replace in the $content variable with the returned HTML.

Installation

Install this package via Composer.

$ composer require estey/laravel-react-middleware

Add the service provider to your config/app.php file.

'providers' => [
    ...
    Estey\ReactMiddleware\ReactCompilerServiceProvider::class,
];

Add the middleware class to your app/Http/Kernel.php file.

protected $routeMiddleware = [
    ...
    'react' => \Estey\ReactMiddleware\CompileReact::class,
];

Configuration

To publish the configuration file, run:

$ php artisan vendor:publish --provider="Estey\ReactMiddleware\ReactCompilerServiceProvider"

In the config file you can change the host and port of the compiler server and change the connection and response timeout times. The default behavior is for timeouts set be to 0, which wait indefinitely. I highly recommend that you change these to be more aggressive.


AJAX requests to this route will return the data array passed to your view as plain JSON. So the example above would return:

{ "title": "Your title here." }

To disable the JSON response on AJAX requests, pass "disable_json" to the first parameter.

Route::get('/', ['middleware' => 'react:disable_json', 'uses' => 'HomeController@index']);

Merge JSON Response

If your Node.js application responds with JSON instead of a plain HTML string, then the JSON will be parsed and merged into your view. So for example, if your Node.js application responds with:

{
  "content": "<div>Hello World</div>",
  "metaKeywords": "foo, bar, baz"
}

Then, both the $content variable and the $metaKeywords variable will be accessible in your view file.

laravel-react-middleware's People

Contributors

bradestey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-react-middleware's Issues

An example app

Forgive my ignorance but could it be possible that you can have an example app set up using the middleware and some instructions on node ? The link by the way doesnt work

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.