Server-Side Rendering (SSR)
- Laravel Starter Kits
- Add Server Entry-Point
- Client-Side Hydration
- Setup Vite
- Update NPM Script
- Running the SSR Server
- Disabling SSR
- Deployment
Server-side rendering pre-renders your JavaScript pages on the server, allowing your visitors to receive fully rendered HTML when they visit your application. Since fully rendered HTML is served by your application, it's also easier for search engines to index your site.
Server-side rendering uses Node.js to render your pages in a background process; therefore, Node must be available on your server for server-side rendering to function properly.
Laravel Starter Kits
If you are using Laravel Starter Kits, Inertia SSR is supported through a build command:
1npm run build:ssr1npm run build:ssr
Add Server Entry-Point
To manually configure SSR without a Laravel starter kit, first create an SSR entry point file within your Laravel project.
1touch resources/js/ssr.js1touch resources/js/ssr.js
1touch resources/js/ssr.jsx1touch resources/js/ssr.jsx
1touch resources/js/ssr.js1touch resources/js/ssr.js
This file is going to look very similar to your app entry point, except it's not going to run in the browser, but rather in Node.js. Here's a complete example.
1import { createInertiaApp } from '@inertiajs/vue3'2import createServer from '@inertiajs/vue3/server'3import { renderToString } from 'vue/server-renderer'4import { createSSRApp, h } from 'vue'56createServer(page =>7 createInertiaApp({8 page,9 render: renderToString,10 resolve: name => {11 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })12 return pages[`./Pages/${name}.vue`]13 },14 setup({ App, props, plugin }) {15 return createSSRApp({16 render: () => h(App, props),17 }).use(plugin)18 },19 }),20)1import { createInertiaApp } from '@inertiajs/vue3'2import createServer from '@inertiajs/vue3/server'3import { renderToString } from 'vue/server-renderer'4import { createSSRApp, h } from 'vue'56createServer(page =>7 createInertiaApp({8 page,9 render: renderToString,10 resolve: name => {11 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })12 return pages[`./Pages/${name}.vue`]13 },14 setup({ App, props, plugin }) {15 return createSSRApp({16 render: () => h(App, props),17 }).use(plugin)18 },19 }),20)
1import { createInertiaApp } from '@inertiajs/react'2import createServer from '@inertiajs/react/server'3import ReactDOMServer from 'react-dom/server'45createServer(page =>6 createInertiaApp({7 page,8 render: ReactDOMServer.renderToString,9 resolve: name => {10 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })11 return pages[`./Pages/${name}.jsx`]12 },13 setup: ({ App, props }) => <App {...props} />,14 }),15)1import { createInertiaApp } from '@inertiajs/react'2import createServer from '@inertiajs/react/server'3import ReactDOMServer from 'react-dom/server'45createServer(page =>6 createInertiaApp({7 page,8 render: ReactDOMServer.renderToString,9 resolve: name => {10 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })11 return pages[`./Pages/${name}.jsx`]12 },13 setup: ({ App, props }) => <App {...props} />,14 }),15)
1import { createInertiaApp } from '@inertiajs/svelte'2import createServer from '@inertiajs/svelte/server'3import { render } from 'svelte/server'45createServer(page =>6 createInertiaApp({7 page,8 resolve: name => {9 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })10 return pages[`./Pages/${name}.svelte`]11 },12 setup({ App, props }) {13 return render(App, { props })14 },15 }),16)1import { createInertiaApp } from '@inertiajs/svelte'2import createServer from '@inertiajs/svelte/server'3import { render } from 'svelte/server'45createServer(page =>6 createInertiaApp({7 page,8 resolve: name => {9 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })10 return pages[`./Pages/${name}.svelte`]11 },12 setup({ App, props }) {13 return render(App, { props })14 },15 }),16)
When creating this file, be sure to add anything that's missing from your app.js file that makes sense to run in SSR mode, such as plugins or custom mixins.
Clustering
By default, the SSR server will run on a single thread. Clustering starts multiple Node servers on the same port, requests are then handled by each thread in a round-robin way.
You may enable clustering by passing a second argument of options to createServer.
1import { createInertiaApp } from '@inertiajs/vue3'2import createServer from '@inertiajs/vue3/server'3import { renderToString } from 'vue/server-renderer'4import { createSSRApp, h } from 'vue'56createServer(page =>7 createInertiaApp({8 // ...9 }),10 { cluster: true },11)1import { createInertiaApp } from '@inertiajs/vue3'2import createServer from '@inertiajs/vue3/server'3import { renderToString } from 'vue/server-renderer'4import { createSSRApp, h } from 'vue'56createServer(page =>7 createInertiaApp({8 // ...9 }),10 { cluster: true },11)
1import { createInertiaApp } from '@inertiajs/react'2import createServer from '@inertiajs/react/server'3import ReactDOMServer from 'react-dom/server'45createServer(page =>6 createInertiaApp({7 // ...8 }),9 { cluster: true },10)1import { createInertiaApp } from '@inertiajs/react'2import createServer from '@inertiajs/react/server'3import ReactDOMServer from 'react-dom/server'45createServer(page =>6 createInertiaApp({7 // ...8 }),9 { cluster: true },10)
1import { createInertiaApp } from '@inertiajs/svelte'2import createServer from '@inertiajs/svelte/server'3import { render } from 'svelte/server'45createServer(page =>6 createInertiaApp({7 // ...8 }),9 { cluster: true },10)1import { createInertiaApp } from '@inertiajs/svelte'2import createServer from '@inertiajs/svelte/server'3import { render } from 'svelte/server'45createServer(page =>6 createInertiaApp({7 // ...8 }),9 { cluster: true },10)
Client-Side Hydration
Since your website will be server-side rendered, you should also update your app.js file to use hydration instead of normal rendering. This allows VueReactSvelte to pick up the server-rendered HTML and make it interactive without re-rendering it.
1import { createApp, h } from 'vue' // [!code --]2import { createSSRApp, h } from 'vue' // [!code ++]3import { createInertiaApp } from '@inertiajs/vue3'45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })8 return pages[`./Pages/${name}.vue`]9 },10 setup({ el, App, props, plugin }) {11 createApp({ render: () => h(App, props) }) // [!code --]12 createSSRApp({ render: () => h(App, props) }) // [!code ++]13 .use(plugin)14 .mount(el)15 },16})1import { createApp, h } from 'vue' // [!code --]2import { createSSRApp, h } from 'vue' // [!code ++]3import { createInertiaApp } from '@inertiajs/vue3'45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })8 return pages[`./Pages/${name}.vue`]9 },10 setup({ el, App, props, plugin }) {11 createApp({ render: () => h(App, props) }) // [!code --]12 createSSRApp({ render: () => h(App, props) }) // [!code ++]13 .use(plugin)14 .mount(el)15 },16})
1import { createInertiaApp } from '@inertiajs/react'2import { createRoot } from 'react-dom/client' // [!code --]3import { hydrateRoot } from 'react-dom/client' // [!code ++]45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })8 return pages[`./Pages/${name}.jsx`]9 },10 setup({ el, App, props }) {11 createRoot(el).render(<App {...props} />) // [!code --]12 hydrateRoot(el, <App {...props} />) // [!code ++]13 },14})1import { createInertiaApp } from '@inertiajs/react'2import { createRoot } from 'react-dom/client' // [!code --]3import { hydrateRoot } from 'react-dom/client' // [!code ++]45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })8 return pages[`./Pages/${name}.jsx`]9 },10 setup({ el, App, props }) {11 createRoot(el).render(<App {...props} />) // [!code --]12 hydrateRoot(el, <App {...props} />) // [!code ++]13 },14})
1import { createInertiaApp } from '@inertiajs/svelte'2import { mount } from 'svelte' // [!code --]3import { hydrate, mount } from 'svelte' // [!code ++]45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })8 return pages[`./Pages/${name}.svelte`]9 },10 setup({ el, App, props }) {11 mount(App, { target: el, props }) // [!code --]12 if (el.dataset.serverRendered === 'true') { // [!code ++:5]13 hydrate(App, { target: el, props })14 } else {15 mount(App, { target: el, props })16 }17 },18})1import { createInertiaApp } from '@inertiajs/svelte'2import { mount } from 'svelte' // [!code --]3import { hydrate, mount } from 'svelte' // [!code ++]45createInertiaApp({6 resolve: name => {7 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })8 return pages[`./Pages/${name}.svelte`]9 },10 setup({ el, App, props }) {11 mount(App, { target: el, props }) // [!code --]12 if (el.dataset.serverRendered === 'true') { // [!code ++:5]13 hydrate(App, { target: el, props })14 } else {15 mount(App, { target: el, props })16 }17 },18})
Setup Vite
Next, update your Vite configuration to build the new SSR entry point by adding a ssr property to Laravel's Vite plugin configuration in your vite.config.js file.
1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.js'],5 ssr: 'resources/js/ssr.js', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.js'],5 ssr: 'resources/js/ssr.js', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})
1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.jsx'],5 ssr: 'resources/js/ssr.jsx', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.jsx'],5 ssr: 'resources/js/ssr.jsx', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})
1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.js'],5 ssr: 'resources/js/ssr.js', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})1export default defineConfig({2 plugins: [3 laravel({4 input: ['resources/js/app.js'],5 ssr: 'resources/js/ssr.js', // [!code ++]6 refresh: true,7 }),8 // ...9 ],10})
Update NPM Script
Next, update the build script in your package.json file to also build your SSR entry point.
1"scripts": {2 "dev": "vite",3 "build": "vite build" // [!code --]4 "build": "vite build && vite build --ssr" // [!code ++]5},1"scripts": {2 "dev": "vite",3 "build": "vite build" // [!code --]4 "build": "vite build && vite build --ssr" // [!code ++]5},
Now you can build both your client-side and server-side bundles.
1npm run build1npm run build
Running the SSR Server
Now that you have built both your client-side and server-side bundles, you should be able run the Node-based Inertia SSR server using the following command.
1php artisan inertia:start-ssr1php artisan inertia:start-ssr
You may use the --runtime option to specify which runtime you want to use. This allows you to switch from the default Node.js runtime to Bun.
1php artisan inertia:start-ssr --runtime=bun1php artisan inertia:start-ssr --runtime=bun
With the server running, you should be able to access your app within the browser with server-side rendering enabled. In fact, you should be able to disable JavaScript entirely and still navigate around your application.
Disabling SSR
Sometimes you may wish to disable server-side rendering for certain pages or routes in your application. You may do so by setting the inertia.ssr.enabled configuration value to false for the current request, typically in a service provider or middleware.
1if (request()->is('admin/*')) {2 config(['inertia.ssr.enabled' => false]);3}1if (request()->is('admin/*')) {2 config(['inertia.ssr.enabled' => false]);3}
Deployment
When deploying your SSR enabled app to production, you'll need to build both the client-side (app.js) and server-side bundles (ssr.js), and then run the SSR server as a background process, typically using a process monitoring tool such as Supervisor.
1php artisan inertia:start-ssr1php artisan inertia:start-ssr
To stop the SSR server, for instance when you deploy a new version of your website, you may utilize the inertia:stop-ssr Artisan command. Your process monitor (such as Supervisor) should be responsible for automatically restarting the SSR server after it has stopped.
1php artisan inertia:stop-ssr1php artisan inertia:stop-ssr
By default, the command fails when the SSR server is not running. You may pass the --graceful option to return a successful exit code instead, making it ideal for deployment scripts where the SSR server is not yet running.
1php artisan inertia:stop-ssr --graceful1php artisan inertia:stop-ssr --graceful
You may use the inertia:check-ssr Artisan command to verify that the SSR server is running. This can be helpful after deployment and works well as a Docker health check to ensure the server is responding as expected.
1php artisan inertia:check-ssr1php artisan inertia:check-ssr
By default, a check is performed to ensure the server-side bundle exists before dispatching a request to the SSR server. In some cases, such as when your app runs on multiple servers or is containerized, the web server may not have access to the SSR bundle. To disable this check, you may set the inertia.ssr.ensure_bundle_exists configuration value to false.
Laravel Cloud
To run the SSR server on Laravel Cloud, you may use Cloud's native support for Inertia SSR.
Laravel Forge
To run the SSR server on Forge, you may enable it via the Inertia SSR toggle in your site's application panel. Forge will create the required daemon and, optionally, update your deploy script to restart the SSR server on each deployment.