sebastiangperez's avatar

Laravel and Inertia hang an application

Hi , i made a website using laravel and inertia and for some reason , seems like when you reload the page o if you do it quickly , seems to hang the server or takes too long to load, any advice ? im using hostinger and not a vps.

1 like
1 reply
LaryAI's avatar
Level 58

Certainly! Here are some steps and considerations to help diagnose and potentially resolve your issue with Laravel and Inertia causing slow reloads or appearing to hang, especially on shared hosting like Hostinger:

  1. Check Server Resources
    Shared hosting usually has limited CPU and RAM. If your app or requests are resource-intensive (e.g., heavy queries, large responses), this can slow things down, especially under rapid requests.

  2. Debugging Tools
    Use Laravel's debug tools to trace slowdowns:

    • Enable debug mode temporarily (APP_DEBUG=true in .env) to see error messages.
    • Use Laravel Debugbar to inspect queries and performance locally.
  3. Optimize Database Queries
    Inefficient or unoptimized queries can block your app:

    • Eager load relationships with ->with() in Eloquent.
    • Use pagination for large data sets.
    • Check indexing in your tables.
    // Example: Eager load 'posts' with each user
    $users = User::with('posts')->paginate(10);
    
  4. Optimize Inertia Responses
    Inertia sends data as JSON—make sure you're not sending huge datasets or unnecessary fields.

    // Only send required data
    return Inertia::render('Dashboard', [
        'user' => Auth::user()->only(['id', 'name', 'email']),
    ]);
    
  5. Caching

    • Use config, route, and view caching with the following Artisan commands:
      php artisan config:cache
      php artisan route:cache
      php artisan view:cache
      
    • Cache heavy calculations or database results where possible.
  6. Asset Optimization

    • Make sure your JavaScript and CSS are compiled & minified (npm run prod).
    • Serve assets via a CDN if possible.
  7. Check for Session/File Locking
    If you store sessions or files on disk (common in shared hosting), simultaneous requests might lock and wait:

    • Switch session driver to redis or database if supported.
    • Consider moving file uploads/storage to S3 or similar.
  8. Test Locally

    • Replicate the issue locally to see if it's code-related or solely a hosting problem.
  9. Monitor Logs

    • Check storage/logs/laravel.log for any errors or slow queries.
    • Use Laravel Telescope (on non-production) for deep request inspection.
  10. Upgrade Hosting if Needed

    • If optimizations don't fix the issue, your app may have outgrown shared hosting. Consider upgrading to VPS or a Laravel-specific host.

Summary:
Start by checking for slow queries, optimize what your app returns via Inertia, use caching, and review logs. Shared hosting adds bottlenecks, so sometimes moving to a VPS is the most effective solution.

If you can provide more details (log entries, code samples, specific slow page, etc.), I can give even more targeted advice!

Please or to participate in this conversation.