2017 © Pedro Peláez
 

library laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

image

spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  • Wednesday, April 25, 2018
  • by Spatie
  • Repository
  • 9 Watchers
  • 123 Stars
  • 3,915 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 3 Open issues
  • 12 Versions
  • 76 % Grown

The README.md

Server side rendering JavaScript in your Laravel application

Latest Version on Packagist GitHub Workflow Status Total Downloads, (*1)

Making server side rendering a bit less hard in Laravel., (*2)

<html>
    <head>
        <title>My server side rendered app</title>
        <script defer src="{{ mix('app-client.js') }}">
    </head>
    <body>
        {!! ssr('js/app-server.js') !!}
    </body>
</html>

This package is a Laravel bridge for the spatie/server-side-rendering library. Before getting started, dig through the readme to learn about the underlying concepts and caveats. This readme also assumes you already have some know-how about building server rendered JavaScript apps., (*3)

Vue and React example apps are available at spatie/laravel-server-side-rendering-examples if you want to see it in action., (*4)

Support us

, (*5)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*6)

We 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. We publish all received postcards on our virtual postcard wall., (*7)

Installation

You can install the package via composer:, (*8)

composer require spatie/laravel-server-side-rendering

The service provider and Ssr alias will be automatically registered., (*9)

You can optionally publish the config file if you want to tweak things., (*10)

php artisan vendor:publish --provider="Spatie\Ssr\SsrServiceProvider" --tag="config"

Usage

Prerequisites

First you'll need to pick an engine to execute your scripts. The server-side-rendering library ships with V8 and Node engines. By default, the package is configured to use node, since you probably already have that installed on your system., (*11)

Set up the NODE_PATH environment variable in your .env file to get started:, (*12)

NODE_PATH=/path/to/my/node

You'll also need to ensure that a storage/app/ssr folder exists, or change the ssr.node.temp_path config value to something else., (*13)

If you'd rather use the V8 engine, you can skip the previous two steps. You'll need to have the v8js extension installed though., (*14)

Configuration

Besides the above, no configuration's required. If you need to tweak things anyway, the config file is well documented., (*15)

Setting up your scripts

You'll need to build two scripts: a server script and a client script. Refer to your frontend-framework-of-choice's documentation on how to build those., (*16)

mix.js('resources/js/app-client.js', 'public/js')
   .js('resources/js/app-server.js', 'public/js');

The server script should be passed to the ssr function, the client script should be loaded manually. The package assumes you're using Laravel Mix, and will resolve the path for you. You can opt out of this behaviour by setting mix to false in the config file., (*17)

{!! ssr('js/app-server.js') !!}
<script src="{{ mix('js/app-client.js') }}">

Your server script should call a dispatch function to send the rendered html back to the view. Here's a quick example of a set of Vue scripts for a server-rendered app. Read the spatie/server-side-rendering readme for a full explanation of how everything's tied together., (*18)

// resources/js/app.js

import Vue from 'vue';
import App from './components/App';

export default new Vue({
    render: h => h(App),
});
// resources/js/app-client.js

import app from './app';

app.$mount('#app');
// resources/js/app-server.js

import app from './app';
import renderVueComponentToString from 'vue-server-renderer/basic';

renderVueComponentToString(app, (err, html) => {
    if (err) {
        throw new Error(err);
    }

    dispatch(html);
});

Rendering an app in your view

The package exposes an ssr helper to render your app., (*19)

<html>
    <head>
        <title>My server side rendered app</title>
        <script defer src="{{ mix('js/app-client.js') }}"></script>
    </head>
    <body>
        {!! ssr('js/app-server.js')->render() !!}
    </body>
</html>

A facade is available too., (*20)

<html>
    <head>
        <title>My server side rendered app</title>
        <script defer src="{{ mix('js/app-client.js') }}"></script>
    </head>
    <body>
        {!! Ssr::entry('js/app-server.js')->render() !!}
    </body>
</html>

Rendering options can be chained after the function or facade call., (*21)

<html>
    <head>
        <title>My server side rendered app</title>
        <script defer src="{{ mix('js/app-client.js') }}"></script>
    </head>
    <body>
        {!! ssr('js/app-server.js')->context('user', $user)->render() !!}
    </body>
</html>

Available options are documented at spatie/server-side-rendering., (*22)

Testing

bash composer test, (*23)

Changelog

Please see CHANGELOG for more information what has changed recently., (*24)

Contributing

Please see CONTRIBUTING for details., (*25)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*26)

Credits

License

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

The Versions

25/04 2018

dev-master

9999999-dev https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

25/04 2018

dev-analysis-XardnZ

dev-analysis-XardnZ https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

25/04 2018

dev-analysis-861jna

dev-analysis-861jna https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

28/03 2018

dev-analysis-87l0Z0

dev-analysis-87l0Z0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

28/03 2018

0.2.5

0.2.5.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

27/03 2018

dev-analysis-q5Q0pv

dev-analysis-q5Q0pv https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

27/03 2018

0.2.4

0.2.4.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

09/03 2018

0.2.3

0.2.3.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

02/02 2018

0.2.2

0.2.2.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

31/01 2018

0.2.1

0.2.1.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

31/01 2018

0.2.0

0.2.0.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering

18/01 2018

0.1.0

0.1.0.0 https://github.com/spatie/laravel-server-side-rendering

Server side rendering JavaScript in your Laravel application

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie ssr laravel-server-side-rendering