Laravel Starter Kits
If you are using Laravel Starter Kits, Inertia SSR is supported through a build command:Vite Plugin Setup
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.
Configure Vite
Add the Inertia plugin to your You may also configure SSR options explicitly.You may pass
vite.config.js file. The plugin will automatically detect your SSR entry point.vite.config.js
vite.config.js
false to opt out of the plugin’s automatic SSR handling, for example if you prefer to configure SSR manually but still want to use the other features of the Vite plugin.vite.config.js
Development Mode
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:The
vite build --ssr and php artisan inertia:start-ssr commands are for production only. You should not run them during development.Production
For production, build both bundles and start the SSR server.Custom SSR Entry Point
The Vite plugin reuses yourapp.js entry point for both browser and SSR rendering by default, so no separate file is needed. The plugin detects the data-server-rendered attribute to decide whether to hydrate or mount, and the setup and resolve callbacks are optional.
If you need custom SSR logic (such as Vue plugins that should only run on the server), you may create a separate resources/js/ssr.js file.
app.js file that makes sense to run in SSR mode, such as plugins or custom mixins.
Manual Setup
If you prefer not to use the Vite plugin, you may configure SSR manually.Create an SSR entry point
Create an SSR entry point file within your Laravel project.This file will look similar to your app entry point, but it runs in Node.js instead of the browser. Here’s a complete example.
Clustering
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.vite.config.js
cluster option to createServer instead.
Running the SSR Server
The SSR server is only required in production. During development, the Vite plugin handles SSR automatically.
--runtime option to specify which runtime you want to use. This allows you to switch from the default Node.js runtime to Bun.
Client-Side Hydration
Error Handling
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 referencingwindow 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.
Throwing on Error
For CI or E2E testing, you may prefer SSR failures to throw an exception instead of falling back silently. Set thethrow_on_error option in your config/inertia.php file.
phpunit.xml to catch SSR errors during testing.
Disabling SSR
Sometimes you may wish to disable server-side rendering for certain pages or routes in your application.Per-Route via Middleware
You may use the$withoutSsr property on your Inertia middleware to disable SSR for specific route patterns.
Via Facade
You may also disable SSR for specific routes using theInertia::withoutSsr() method, typically called from a service provider.
Per-Request
You may disable SSR for the current request by setting theinertia.ssr.enabled configuration value to false.
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.
inertia:stop-ssr Artisan command. Your process monitor (such as Supervisor) should be responsible for automatically restarting the SSR server after it has stopped.
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.
inertia.ssr.ensure_bundle_exists configuration value to false.