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. Inertia’s SSR server requires Node.js 22 or higher.
The recommended way to configure SSR is with the @inertiajs/vite plugin. This approach handles SSR configuration automatically, including development mode SSR without a separate Node.js server.
1
Install the Vite plugin
npm install @inertiajs/vite
2
Configure Vite
Add the Inertia plugin to your vite.config.js file. The plugin will automatically detect your SSR entry point.
vite.config.js
import inertia from '@inertiajs/vite'import laravel from 'laravel-vite-plugin'import { defineConfig } from 'vite'export default defineConfig({ plugins: [ laravel({ input: ['resources/js/app.js'], refresh: true, }), inertia(), ],})
The Vite plugin handles SSR automatically during development. There is no need to build your SSR bundle or start a separate Node.js server. Simply run your Vite dev server as usual:
npm run dev
The Vite plugin exposes a server endpoint that Laravel uses for rendering, complete with HMR support.
By default, the SSR server runs on a single thread. You may enable clustering to start multiple Node servers on the same port, with requests handled by each thread in a round-robin fashion.
By default, the SSR server binds to 0.0.0.0, making it accessible on all network interfaces. You may restrict it to a specific interface using the host option.
The Vite plugin reuses your app.js entry point for SSR by default, so no separate file is needed. Most customizations may be handled using the withApp callback.For more control, such as providing a manual setup callback, you may create a separate resources/js/ssr.js entry point and update your app.js to use client-side hydration.
You may pass ssr: false to the Inertia plugin to disable its automatic SSR handling and manage the SSR build yourself. You should also add the ssr property to the Laravel Vite plugin configuration so it knows about your entry point.
You may pass the cluster option to createServer to start multiple Node servers on the same port, with requests handled by each thread in a round-robin fashion.
By default, the SSR server binds to 0.0.0.0, making it accessible on all network interfaces. You may pass the host option to createServer to restrict it to a specific interface.
The SSR server is only required in production. During development, the Vite plugin handles SSR automatically.
Once you have built both your client-side and server-side bundles, you may start the SSR server using the following Artisan command.
php artisan inertia:start-ssr
By default, the SSR server uses node as its runtime. You may change this by setting the runtime option in your config/inertia.php file. An absolute path to the runtime binary is also supported.
The --runtime flag on the Artisan command overrides the configured value for a single invocation.
php artisan inertia:start-ssr --runtime=bun
You may also enable the ensure_runtime_exists option to verify the runtime binary exists before attempting to start the SSR server. The command will exit with an error if the binary cannot be found.
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.
When SSR rendering fails, Inertia gracefully falls back to client-side rendering. The Vite plugin logs detailed error information to the console, including the component name, request URL, source location, and a tailored hint to help you resolve the issue.Common SSR errors are automatically classified. Browser API errors (such as referencing window or document in server-rendered code) include guidance on moving the code to a lifecycle hook. Component resolution errors suggest checking file paths and casing.Inertia also dispatches an SsrRenderFailed event on the server. You may listen for this event to log failures or send them to an error tracking service.
use Illuminate\Support\Facades\Log;use Inertia\Ssr\SsrRenderFailed;Event::listen(SsrRenderFailed::class, function (SsrRenderFailed $event) { Log::warning('SSR failed', $event->toArray());});
Since Inertia gracefully falls back to client-side rendering, SSR failures may go unnoticed. Your tests pass because the client-side render succeeds, but your users never receive server-rendered HTML. This is especially common in E2E tests with tools like Laravel Dusk or Pest Browser Testing.You may set the throw_on_error option in your config/inertia.php file to throw an exception instead of falling back silently, allowing you to catch SSR issues early.
This option is not recommended for production, as it will cause SSR failures to return an error response instead of falling back to client-side rendering.
You may set the environment variable in your phpunit.xml to enable this only during testing.
SSR has two layers: the Vite plugin serves SSR during development and builds the SSR bundle for production, while the Laravel adapter dispatches rendering requests to the SSR server. To fully disable SSR, you should disable both.
vite.config.js
inertia({ ssr: false,})
config/inertia.php
'ssr' => [ 'enabled' => false,],
You may also prevent the Laravel adapter from dispatching SSR requests programmatically using the Inertia::disableSsr() method. This is useful when you want to keep SSR in your build but disable it during tests or in specific environments.
use Inertia\Inertia;Inertia::disableSsr();
A boolean or closure may be provided to disable SSR conditionally.
You may use the $withoutSsr property on your Inertia middleware to disable SSR for specific route patterns.
use Inertia\Middleware;class HandleInertiaRequests extends Middleware{ /** * Defines the routes that should not use SSR. * * @var array<int, string> */ protected $withoutSsr = [ 'admin/*', 'dashboard', ];}
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.
php 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.
php artisan inertia:stop-ssr
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.
php 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.
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.