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.
1
Install the Vite plugin
2
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 or disable SSR entirely.vite.config.js
3
Update your build script
Update the
build script in your package.json to build both bundles.package.json
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:Production
For production, build both bundles and start the SSR server.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
Host
By default, the SSR server binds to0.0.0.0, making it accessible on all network interfaces. You may restrict it to a specific interface using the host option.
vite.config.js
Manual Setup
The Vite plugin reuses yourapp.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.
SSR Entry Point
app.js file that makes sense to run in SSR mode, such as plugins or custom mixins.
Client-Side Hydration
Opting Out of the Vite Plugin
You may passssr: 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.
Clustering
You may pass thecluster option to createServer to start multiple Node servers on the same port, with requests handled by each thread in a round-robin fashion.
Host
By default, the SSR server binds to0.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.
Running the SSR Server
The SSR server is only required in production. During development, the Vite plugin handles SSR automatically.
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. Components that depend on browser APIs may also be wrapped in WhenMounted. Component resolution errors suggest checking file paths and casing.
Throwing on Error
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.Configuring the SSR Request
Disabling SSR
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
Excluding Routes from SSR
Sometimes you may wish to skip server-side rendering for certain routes while keeping SSR enabled for the rest of your application.Per-Request
You may disable SSR for a single page or request.Via Middleware
In Laravel PHP, you may use the$withoutSsr property on your Inertia middleware to disable SSR for specific route patterns.
Laravel PHP
Via Facade
In Laravel PHP, you may also exclude specific routes using theInertia::withoutSsr() method, typically called from a service provider.
Laravel PHP
Client-Only Components
v3.8.0+ Sometimes, a component relies on browser APIs such aswindow or document and cannot be rendered on the server. Instead of disabling SSR for the entire page, you may wrap the component in WhenMounted, which renders its children only after mounting in the browser. The fallback is rendered on the server and during hydration instead.
WhenMounted only skips rendering on the server. The wrapped component is still part of your SSR bundle, so its module must be safe to import on the server.