Persistent Layouts in Inertiajs: How to use them and what are the benefits?

Updated: Feb 17, 2025

Persistent Layouts in Inertiajs: How to use them and what are the benefits?

Persistent layouts in Inertia.js is a feature that allows you to keep certain parts of your application's UI consistent across multiple pages without having to make a full page request each time. This can lead to significant performance improvements and a better user experience.

To use persistent layouts in Inertia.js, you need to create a layout file that contains the parts of your UI that you want to keep consistent. This layout file should be defined in your resources/js/pages/layouts directory and should extend the AppLayout component that comes with Inertia.js.

Here's an example of what a layout file might look like:

import React from 'react';
import App from '@components/App';

export default function Layout({ children }) {
  return (
    <div className="min-h-screen bg-gray-100">
      <App>
        <header className="bg-gray-800 text-white p-4">
          {/* Your header content goes here */}
        </header>
        <main className="p-4">{children}</main>
        <footer className="bg-gray-800 text-white p-4">
          {/* Your footer content goes here */}
        </footer>
      </App>
    </div>
  );
}

In this example, we've defined a layout file that includes a header and footer that will be displayed on every page of our application. The children prop is where we'll render the content of each page.

To use this layout file, you need to wrap each page component with it. Here's an example of what a page component might look like:

import React from 'react';
import Layout from '@components/Layout';

export default function Home() {
  return (
    <Layout>
      <h1 className="text-3xl font-bold">Welcome to Inertia.js!</h1>
      <p className="text-xl">This is the home page.</p>
    </Layout>
  );
}

In this example, we've wrapped the Home component with the Layout component that we defined earlier. This means that the header and footer will be displayed on the home page, as well as any other pages that use the same layout.

The benefits of using persistent layouts in Inertia.js are numerous. Here are a few:

  1. Faster page loads: By keeping certain parts of your UI consistent across multiple pages, you can reduce the amount of data that needs to be fetched and rendered on each page. This can lead to faster page loads and a better user experience.
  2. Improved performance: Persistent layouts can also improve the overall performance of your application by reducing the number of network requests that need to be made. This can lead to smoother scrolling and faster navigation between pages.
  3. Consistent UI: Persistent layouts can help ensure that your UI remains consistent across multiple pages, which can make your application feel more cohesive and easier to use.
  4. Reduced development time: By defining a layout file that can be used across multiple pages, you can save time on development and maintenance by avoiding the need to duplicate UI components and logic across multiple files.

Overall, persistent layouts are a powerful feature of Inertia.js that can help improve the performance, consistency, and development experience of your application. By defining a layout file that can be used across multiple pages, you can reduce the amount of data that needs to be fetched and rendered on each page, improve the overall performance of your application, and ensure that your UI remains consistent and easy to use.