Advanced Server-Side Rendering With Laravel & Vue: Multi-Page App
A few weeks ago I wrote a tutorial on the new Vue server-side rendering capabilities for Laravel. That tutorial mostly focused on the set up of SSR in a Laravel environment and so I only had time to demonstrate a simple “Hello World” app with no significant features.
Now I want to build on that previous tutorial and demonstrate how to server render a Vue app that includes multiple pages with Vue Router, since most of your Laravel projects will have more than one page.
You can get the completed code for this tutorial here on Github.
Note: this article was originally posted here on the Vue.js Developers blog on 2017/11/27
This tutorial will extend the app I built in the previous article, Server-Side Rendering With Laravel & Vue.js 2.5. Make sure you’re familiar with how it works and have a suitable development environment set up i.e. with the php-v8js extension installed.
If you don’t have that code, clone it and set it up:
$ git clone https://github.com/anthonygore/vue-js-laravel-ssr
$ cd vue-js-laravel-ssr
$ cp .env.example .env
$ composer install
$ npm i
Then install Vue Router:
$ npm i --save-dev vue-router
We’ll begin by creating a file for our router configuration that exports an instance of the router for use in the app.
I’ve made up some sample routes with each displaying a component generated from the method
pageComponent. This factory method returns a simple component that does nothing more than display the name of the page. This is all we need to prove SSR routing works.
In the main app file we’ll now import the router module and add it to the app, just as you would in any Vue project. The app instance is then exported for use in the client and server entry files.
Note that our Vue Router instance is in history mode, so routes will fallback to the server when a sub-page is refreshed or loaded from the navigation bar.
This means that any route that we created in the front-end app also needs to be created on the server side. They can all point to the same controller method
Now we need to set up multi-page SSR in the controller. This is a modification of the logic in the base app, so make sure you’re familiar with how that worked.
To SSR a multi-page app, we need to tell the Vue server app (as defined in entry-server.js) what the current URL being requested is. This will ensure that when the app loads in the sandbox, it’s displaying the correct page component.
To do this we pass the URL i.e.
$request->path() through to the
render method from the
url that will be accessible from the Vue server app when it runs in the sandbox.
Vue server app
The last major step is to modify the Vue server app so that we can programmatically set the URL rather than waiting for a user to do it.
The logic for doing this is inside the Promise callback function. Here’s what it does:
- The router is set to the correct URL by pushing the global variable
- When the router is ready, we see if any page components are being displayed as a result of this push, telling us the route is valid. If not, we throw a 404. If so, we return the app instance.
A Promise is used because the router loads asynchronously. Once this Promise resolves, we can use the server renderer method
renderVueComponentToString to SSR the instance and finally use
The SSR logic for the multi-page app is now complete. Let’s create some router links in the page so we can test the app in a browser:
Loading the home page look like this:
The real test is visiting a route in the navigation bar so the server routes handle the request and hopefully SSR the app. To do so, visit
http://localhost:9000/about and inspect the source markup. As you can see, it includes the rendered app at the correct URL:
Take A Free Vue.js Introduction Course! Learn what Vue is, what kind of apps you can build with it, how it compares to React & Angular, and more in this 30-minute video introduction. Enroll for free!