Inertia.js is a framework that allows you to build single-page applications using server-side rendering (SSR) and client-side rendering (CSR) together. It combines the best of both worlds by leveraging the benefits of SSR for initial page load and SEO, and CSR for subsequent interactions.
To implement SSR and CSR with Laravel and Inertia.js, you need to follow these steps:
-
Install the necessary dependencies:
- Laravel: Make sure you have Laravel installed on your system.
- Inertia.js: Install the Inertia.js package using Composer:
composer require inertiajs/inertia-laravel. - Inertia.js Vue Adapter: Install the Inertia.js Vue adapter using npm or yarn:
npm install @inertiajs/inertia @inertiajs/inertia-vue.
-
Set up your Laravel routes:
- Define your routes in the
routes/web.phpfile. You can use theInertia\Inertiafacade to return Inertia responses. For example:use Inertia\Inertia; Route::get('/', function () { return Inertia::render('Welcome'); });
- Define your routes in the
-
Create your Vue components:
- Create your Vue components in the
resources/js/Pagesdirectory. These components will be used to render your pages. - Each component should extend the
@inertiadirective and define apropsproperty to receive data from the server. For example:<template> <div> <h1>{{ title }}</h1> <p>{{ message }}</p> </div> </template> <script> export default { props: { title: String, message: String, }, }; </script>
- Create your Vue components in the
-
Render your Inertia pages:
- In your Laravel routes, use the
Inertia::rendermethod to render your Inertia pages. Pass the component name and any data you want to send to the component. For example:Route::get('/', function () { return Inertia::render('Welcome', [ 'title' => 'Welcome to My App', 'message' => 'This is an example message.', ]); });
- In your Laravel routes, use the
-
Set up your main layout:
- Create a main layout file in the
resources/js/Layoutsdirectory. This layout will be used as the base for all your pages. - Use the
@inertiadirective to render the Inertia pages within the layout. For example:<template> <div> <header> <!-- Your header content --> </header> <main> <inertia-link href="/">Home</inertia-link> <inertia-link href="/about">About</inertia-link> <inertia> <!-- Your Inertia page content will be rendered here --> </inertia> </main> <footer> <!-- Your footer content --> </footer> </div> </template>
- Create a main layout file in the
-
Update your Laravel views:
- Update your Laravel views to use the main layout file. For example, in your
welcome.blade.phpfile:@extends('layouts.app') @section('content') <div id="app"></div> @endsection
- Update your Laravel views to use the main layout file. For example, in your
-
Compile your assets:
- Compile your JavaScript and CSS assets using Laravel Mix. Run the following command:
npm run devoryarn dev.
- Compile your JavaScript and CSS assets using Laravel Mix. Run the following command:
-
Start your Laravel development server:
- Start your Laravel development server using the
php artisan servecommand.
- Start your Laravel development server using the
With these steps, you should be able to implement SSR and CSR with Laravel and Inertia.js. The server will render the initial page load using SSR, and subsequent interactions will be handled by CSR.