- How do I install voku/html-min in a Laravel application?
- Run composer require voku/html-min. The package is framework-agnostic and ships no Laravel service provider, so there is nothing to register. Instantiate voku\helper\HtmlMin in a middleware, command, or service and call minify() on an HTML string. ext-dom must be available in your PHP runtime.
- Which Laravel and PHP versions does voku/html-min support?
- voku/html-min has no Laravel coupling, so it works with any Laravel version that runs on a compatible PHP runtime. Check the package composer.json on Packagist for the current PHP constraint and confirm ext-dom is enabled. Pin a tested version in composer.json rather than relying on a floating range.
- How do I build Laravel middleware that minifies HTML responses?
- Create a middleware that only acts on status 200 responses with a text/html Content-Type. Exclude StreamedResponse, BinaryFileResponse, and JsonResponse, then call $response->setContent($minifier->minify($response->getContent())) inside a try/catch that fails open. Symfony setContent() does not update Content-Length, so remove or recompute that header. Add config flags for enabled, only_routes, except_routes, and except_when_authenticated.
- Is it safe to minify every HTML response globally in Laravel?
- Global middleware on all HTML is strongly discouraged. Scope minification to static marketing, CMS, or otherwise cacheable routes, and exclude interactive components. Apps using Livewire, Alpine.js, Filament, or signed and hashed HTML payloads need per-route review before rollout. Budget real QA time because the exclusion list is the main integration cost.
- Will voku/html-min break Livewire or Alpine.js hydration?
- It can. Livewire morphdom compares the server-rendered DOM with its in-memory tree, and removing inter-element whitespace text nodes is a documented source of hydration mismatches. Treat interactive components as out of scope and limit minification to static pages. If you must try it, test every Livewire component and Alpine-driven partial.
- How much faster does HTML minification make a Laravel site compared with gzip or Brotli?
- Expect only a single-digit percentage reduction in compressed transfer size on typical Laravel pages. The risk and per-request CPU cost are real, so the payoff is often marginal or negative. Measure with RUM or APM data before adding it. If payload size is the goal, enable gzip/Brotli and edge minification at your CDN first.
- Does voku/html-min preserve pre, textarea, inline scripts and SVG content?
- The library parses HTML5 into a DOMDocument and reserializes it, and it explicitly protects inline CSS, inline JS, and conditional comments regardless of settings. Whitespace-significant elements like pre and textarea are generally handled by the parser, but you should validate your own corpus. Foreign content such as SVG and MathML, template scripts, and invalid-but-tolerated markup deserve explicit tests.
- Does voku/html-min change my markup structure or attribute order?
- Yes, by design. It is a parse-and-reserialize minifier, not a regex whitespace trimmer, so output is the DOM parser normalization of your markup. Attribute ordering, self-closing tag style, and tolerated invalid markup can shift. That is low visual risk but non-zero for anything that hashes, diffs, or snapshots HTML.
- Is voku/html-min actively maintained and what is the bus factor?
- It has a single maintainer, low release cadence, and a small star count, so plan for limited support. The codebase is small, MIT licensed, covered by CI, and easy to fork or vendor if needed. Pin an exact version and keep an in-repo fork as a contingency. The low popularity score reflects notability, not necessarily code quality.
- What are alternatives to voku/html-min for Laravel HTML minification?
- The first alternative is edge minification at a CDN such as Cloudflare, Fastly, or Akamai, which removes application risk and PHP CPU cost. Server-level gzip or Brotli compression should already be enabled and often delivers most of the benefit. Within PHP, other HTML minifier packages exist, but they carry similar DOM-reserialization risks. Compare them on your own HTML corpus before choosing.