symfony/web-link
Symfony WebLink component manages typed links between resources and serializes them to HTTP Link headers. Use it to advertise preload, prefetch, and other resource hints for faster navigation and HTTP/2 push, following HTML5 and W3C specs.
symfony/web-link package aligns perfectly with Laravel’s performance-focused architecture, particularly for HTTP/2 push, preload, and prefetch strategies. It enables resource hinting (e.g., preload, prefetch, dns-prefetch) to reduce render-blocking and improve perceived load times.rel types (via HTML5 link type extensions), allowing Laravel to integrate with domain-specific link relations (e.g., API pagination, microservice discovery).Link headers, a critical feature for Laravel applications leveraging HTTP/2 (e.g., Varnish, Nginx, or Symfony’s HTTP/2 middleware).App\Http\Middleware\AddLinkHeaders) to dynamically generate Link headers per request.Illuminate\Http\Response to include Link header serialization via a macro (e.g., Response::macro('withLinkHeaders', ...)).LinkProvider as a singleton in Laravel’s IoC container for centralized management.symfony/http-foundation) ensures seamless integration with minimal friction.Illuminate\Http\Events\RequestHandled) to conditionally add headers based on routes, user roles, or asset types.^7.4) to align with Laravel’s PHP support.Link header, leading to malformed or conflicting values.LinkProvider interfaces with weights) or use Laravel’s Pipes to merge headers.preload/prefetch for broader compatibility; log unsupported environments for monitoring.Link headers may be cached aggressively, causing stale resources if not invalidated properly.Vary: Link headers or cache-key versioning (e.g., ?v=1.2.3) for dynamic links.Link header generation affect response times under high traffic? (Benchmark with/without the component.)preload vs. prefetch? (Rule-based or data-driven?)preload?)Link header effectiveness across browsers?rel types or updates to W3C specs? (Centralized config or per-feature PRs?)Link headers globally (e.g., app/Http/Middleware/AddLinkHeaders.php).LinkProvider implementations as bindings (e.g., AppServiceProvider::boot()).Illuminate\Http\Response for fluent API (e.g., response()->withLinkHeaders($provider)).symfony/http-foundation) to avoid duplication.symfony/web-link alongside other Symfony packages (e.g., HttpClient for parsing incoming Link headers).spatie/laravel-http2) to enable push capabilities.symfony/web-link to composer.json and test basic preload headers in a single route.// app/Http/Middleware/AddLinkHeaders.php
public function handle(Request $request, Closure $next) {
$response = $next($request);
$linkProvider = app(GenericLinkProvider::class)
->withLink(new Link('preload', asset('css/app.css'), ['as' => 'style']));
$response->headers->set('Link', (new HttpHeaderSerializer())->serialize($linkProvider->getLinks()));
return $response;
}
LinkProvider services (e.g., DashboardLinkProvider, AuthLinkProvider).View Composers to inject asset-dependent links (e.g., preload JS for admin pages).http2_push directive) and validate with tools like HTTP/2 Push Analyzer.preload for non-HTTP/2 environments.Link header usage with Laravel Telescope or custom metrics.preload vs. prefetch for non-critical assets).preload: Supported in all modern browsers (Chrome, Firefox, Safari, Edge).prefetch: Universal support; use for non-critical assets.dns-prefetch: Supported but less impactful; use for third-party domains.Link headers. Some may require configuration to pass headers upstream.LinkProvider interfaces to concrete implementations in AppServiceProvider.$this->app->bind(LinkProviderInterface::class, function ($app) {
return (new GenericLinkProvider())
->withLink(new Link('preconnect', 'https://cdn.example.com'));
});
AddLinkHeaders middleware after StartSession but before TrimStrings to ensure headers are set early.Route::bind() or RouteServiceProvider to attach providers to specific routes.LinkProvider implementations.Link headers (e.g., assertHeaderContains('Link', 'preload')).symfony/web-link to a minor version (e.g., ^7.4) to avoid unexpected breaks.rel Types:
rel values in a LINK_REL_TYPES constant or config file.// config/link_relations.php
return [
'api.pagination.next' => 'next',
'api.pagination.prev' => 'prev',
];
LinkHeaderValidator to catch malformed headers early (e.g., duplicate rel values).How can I help you explore Laravel packages today?