Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arrrssssss's avatar

Inertia Lazy Load Data

Hi,

In my applications Home vue component there is a Grid that load its data from the props.data of home component, When ever I visit home end point in laravel, I want to Inertia::render first load my Home component, and after that the data calculation finishes, lazy load data to Home component and then data became visible.

Should I use Inertia::lazy on Inertia::render, or the implementation have to pass through something like axios and API endpoint for that data in laravel ?

Regards

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve lazy loading of data in your Inertia.js application, you should use an API endpoint to fetch the data after the initial component has been rendered. This approach allows you to first render the Home component and then load the data asynchronously using Axios.

Here is a step-by-step solution:

  1. Create an API endpoint in Laravel to fetch the data:

    In your web.php or api.php routes file, define a route that returns the data you need:

    Route::get('/api/home-data', [HomeController::class, 'getData']);
    

    In your HomeController, create the getData method:

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class HomeController extends Controller
    {
        public function getData()
        {
            // Perform your data calculation here
            $data = [
                // Your data here
            ];
    
            return response()->json($data);
        }
    }
    
  2. Render the Home component without the data initially:

    In your route that renders the Home component, do not pass the data initially:

    use Inertia\Inertia;
    
    Route::get('/home', function () {
        return Inertia::render('Home');
    });
    
  3. Fetch the data in the Home component using Axios:

    In your Home.vue component, use the mounted lifecycle hook to fetch the data after the component has been rendered:

    <template>
        <div>
            <h1>Home</h1>
            <div v-if="loading">Loading...</div>
            <div v-else>
                <!-- Render your grid with the data here -->
                <Grid :data="data" />
            </div>
        </div>
    </template>
    
    <script>
    import axios from 'axios';
    import Grid from './Grid.vue';
    
    export default {
        components: {
            Grid
        },
        data() {
            return {
                data: null,
                loading: true
            };
        },
        mounted() {
            axios.get('/api/home-data')
                .then(response => {
                    this.data = response.data;
                    this.loading = false;
                })
                .catch(error => {
                    console.error('Error fetching data:', error);
                    this.loading = false;
                });
        }
    };
    </script>
    

In this solution, the Home component is rendered first, and then the data is fetched asynchronously using Axios. The loading state is used to show a loading indicator while the data is being fetched. Once the data is received, it is passed to the Grid component for rendering. This approach ensures that the initial render is fast and the data is loaded lazily.

Please or to participate in this conversation.