spatie/laravel-mix-preload
Laravel package that adds a @preload Blade directive to automatically output preload/prefetch tags from your mix-manifest.json. Mark chunks by including “preload” or “prefetch” in the filename to optimize asset loading.
Installation:
composer require spatie/laravel-mix-preload
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\MixPreload\MixPreloadServiceProvider"
First Use Case:
Add @preload directive to your Blade template's <head> section:
<head>
@preload
</head>
Configure Mix Manifest:
Ensure your mix-manifest.json includes chunk names prefixed with preload- or prefetch-:
{
"/js/preload-heavy-library.js": "/js/preload-heavy-library.js",
"/css/prefetch-next-page.css": "/css/prefetch-next-page.css"
}
Verify Output:
The directive will auto-generate <link> tags for preload/prefetch based on the manifest.
Preloading Critical Resources:
preload- (e.g., preload-big-library.js) to preload heavy JS/CSS early.// webpack.mix.js
mix.js('resources/js/big-library.js', 'public/js/preload-big-library.js');
Prefetching Future Pages:
prefetch- prefix for CSS/JS needed on subsequent pages (e.g., prefetch-next-page.css).Conditional Preloading:
@if (Route::is('dashboard'))
@preload
@endif
Customizing Link Attributes:
as/crossorigin via config (config/mix-preload.php):
'preload' => [
'as' => 'script',
'crossorigin' => 'anonymous',
],
With Laravel Mix:
Ensure mix-manifest.json is generated and accessible (default: public/mix-manifest.json).
Use mix.version() in your Blade templates to leverage cache-busting.
With Inertia.js: Prefetch assets for next potential pages in your SPA:
@preload <!-- Prefetches assets for routes defined in your Inertia app -->
With Alpine.js/React: Dynamically trigger prefetching on route changes:
// Alpine.js example
document.addEventListener('alpine:init', () => {
Alpine.data('prefetcher', () => ({
prefetch(url) {
fetch(`/prefetch?url=${url}`)
.then(() => console.log('Prefetched:', url));
}
}));
});
Manifest Path Misconfiguration:
public/mix-manifest.json. If custom, set in config:
'manifest_path' => 'custom/path/to/manifest.json',
Incorrect Prefix Naming:
preload- or prefetch- prefixes are processed. Typos (e.g., pre-load-) will be ignored.mix-manifest.json entries.Caching Issues:
php artisan cache:clear
php artisan view:clear
mix.noVersion() in development to bypass cache.Resource Hints Overload:
Network tab) to check Preload requests.Cross-Origin Restrictions:
crossorigin may fail if the server lacks Access-Control-Allow-Origin headers.crossorigin="use-credentials" or omit it if not needed.@dump(Spatie\MixPreload\Facades\MixPreload::getPreloadLinks())
Link headers aren’t conflicting with preload tags (e.g., from CDNs).Custom Directives:
Extend the package to support @prefetch or @preconnect:
// app/Providers/AppServiceProvider.php
use Spatie\MixPreload\Facades\MixPreload;
Blade::directive('preconnect', function ($expression) {
return "<link rel='preconnect' href='{$expression}'>";
});
Dynamic Prefetching: Hook into Laravel events to prefetch assets dynamically:
// app/Providers/EventServiceProvider.php
public function boot()
{
event(new \Spatie\MixPreload\Events\PrefetchRequested($url));
}
Conditional Logic: Override the directive logic in a service provider:
Blade::directive('preload', function () {
if (auth()->check()) {
return MixPreload::renderPreloadLinks();
}
return '';
});
Asset Prioritization:
Use the priority attribute (Chrome-only) for high-priority resources:
// config/mix-preload.php
'preload' => [
'priority' => true, // Adds `priority` attribute
],
How can I help you explore Laravel packages today?