0
0
Laravelframework~8 mins

Including sub-views (@include) in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Including sub-views (@include)
MEDIUM IMPACT
This affects the server-side rendering time and the size of the HTML sent to the browser, impacting the initial page load speed.
Rendering a page with reusable components using sub-views
Laravel
@includeWhen($showHeader, 'header')
@includeWhen($showSidebar, 'sidebar')
@include('content')
@include('footer')
Conditionally includes only necessary sub-views, reducing server work and HTML size.
📈 Performance Gainreduces server render time by up to 40%; smaller HTML reduces LCP
Rendering a page with reusable components using sub-views
Laravel
@include('header')
@include('sidebar')
@include('content')
@include('footer')
@include('comments')
@include('related-posts')
Including many sub-views causes multiple file reads and template compilations, increasing server response time and HTML size.
📉 Performance Costblocks server rendering for 50-150ms depending on number of includes; increases HTML size by 10-30% per sub-view
Performance Comparison
PatternServer Render TimeHTML SizeLCP ImpactVerdict
Many unconditional @include callsHigh (multiple file reads)Large (more HTML)Slower LCP[X] Bad
Conditional @include callsMedium (fewer files)Smaller (less HTML)Faster LCP[OK] Good
Rendering Pipeline
When Laravel renders a view with @include, it reads and compiles each sub-view file on the server, then combines their HTML output into one response sent to the browser.
Server-side Rendering
Network Transfer
Browser Parsing
⚠️ BottleneckServer-side Rendering due to multiple file reads and template compilations
Core Web Vital Affected
LCP
This affects the server-side rendering time and the size of the HTML sent to the browser, impacting the initial page load speed.
Optimization Tips
1Limit the number of @include sub-views to reduce server rendering time.
2Use conditional includes (@includeWhen) to avoid rendering unnecessary parts.
3Cache compiled views to speed up repeated requests.
Performance Quiz - 3 Questions
Test your performance knowledge
How does including many sub-views with @include affect page performance?
AImproves browser rendering speed by splitting HTML
BReduces network requests by loading sub-views separately
CIncreases server render time and HTML size, slowing initial load
DHas no impact on performance
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and check the size and timing of the main HTML document.
What to look for: Look for large HTML size and long server response time indicating slow server rendering due to many includes.