mnot/hinclude
Laravel package that adds Server Side Includes (hinclude) support, letting you compose pages from reusable fragments and render parts independently. Useful for micro-frontends, partial caching, and speeding up complex views by loading sections separately.
mnot/hinclude package enables declarative client-side inclusion (e.g., embedding external resources like HTML snippets, partials, or dynamic content) via HTTP headers (X-Include). This aligns well with:
fetch or similar) for inclusion logic. May conflict with SSR/SSG workflows.X-Include headers via Laravel’s App\Http\Middleware or Illuminate\Http\Response events.response()->include('partial')).@includeClient('header')).X-Include headers (provided by the package). Ensure compatibility with your JS bundle (e.g., Vite, Webpack).Access-Control-Allow-Origin if needed.| Risk Area | Mitigation Strategy |
|---|---|
| Performance Overhead | Benchmark inclusion latency (especially for high-cardinality includes). Cache responses aggressively. |
| Security | Validate included URLs to prevent SSRF or XSS (e.g., whitelist domains, sanitize content). |
| SEO/SSR Conflicts | Exclude includes during server-side rendering (e.g., via if (app()->runningInConsole())). |
| Browser Support | Test with legacy browsers if targeting non-modern audiences (e.g., polyfill fetch). |
| Package Maintenance | Monitor for updates (last commit: 2018). Fork if critical bugs arise. |
@include or partials) achieve the same goal with lower latency?X-Include headers.header('X-Include: ...')).hinclude alternatives like html-includes or custom Webpack loaders.eleventy or Hugo via custom shortcodes.require).X-Include headers.| Component | Compatibility Notes |
|---|---|
| Laravel Versions | Tested on PHP 7.2+ (package last updated for PHP 7.x). Use composer require with version constraints. |
| Frontend Frameworks | Works with vanilla JS, but may need adapters for React/Vue (e.g., custom hooks). |
| Caching Layers | Ensure proxies (Varnish, Cloudflare) forward X-Include headers. |
| CDNs | CDNs may strip custom headers; configure to preserve X-Include. |
// app/Http/Middleware/IncludeMiddleware.php
public function handle($request, Closure $next) {
$response = $next($request);
$response->header('X-Include', 'header,footer');
return $response;
}
<script src="https://unpkg.com/[email protected]/dist/hinclude.js"></script>
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [/* ... */],
optimizeDeps: { include: ['hinclude'] }
});
<div id="header" data-hinclude="/api/header"></div>
X-Include headers).mnot/hinclude for security updates (low priority; MIT license).fetch for IE11).hinclude events or Laravel’s Log::error).<div id="sidebar" data-hinclude="/api/sidebar" data-fallback="<p>Loading sidebar...</p>"></div>
Cache::remember for included content if sourced from Laravel.data-hinclude with loading="lazy").| Failure Scenario | Mitigation |
|---|---|
| JS Disabled | Provide server-rendered fallbacks or use noscript includes. |
| CORS Blocked | Configure Laravel’s CORS middleware or use JSONP if applicable. |
| API/Include Unavailable | Implement retry logic or exponential backoff in the JS library. |
| Header Stripped by Proxy | Whitelist X-Include in CDN/proxy configurations. |
| Content Mismatch | Validate included HTML structure (e.g., schema checks). |
## Using hinclude in Laravel
1. Add middleware to inject headers.
2. Mark HTML with `data-hinclude`.
3. Include the JS library.
php artisan make:include header /api
How can I help you explore Laravel packages today?