Download the PHP package backstage/laravel-minify-html-middleware without Composer

On this page you can find all versions of the php package backstage/laravel-minify-html-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-minify-html-middleware

Laravel HTML Minify Middleware

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A lightweight and efficient Laravel middleware package that automatically minifies HTML responses, reducing page size and improving load times. The package intelligently removes unnecessary whitespace, comments, and optimizes your HTML output while preserving functionality.

Features

What Gets Minified?

The package includes three built-in transformers:

  1. Remove Comments: Strips HTML comments while preserving framework-specific comments (Livewire, Knockout.js)
  2. Remove Whitespace: Eliminates unnecessary whitespace between tags and multiple spaces
  3. Trim Scripts: Removes extra whitespace from within <script> tags

Before Minification:

After Minification:

Requirements

Installation

Install the package via Composer:

The package will automatically register its service provider.

Publish Configuration

Publish the configuration file to customize the transformers:

This will create a config/minify-html.php file with the following default configuration:

Usage

Global Middleware

To minify all HTML responses in your application, add the middleware to the global middleware stack in bootstrap/app.php (Laravel 11+):

For Laravel 10, add to app/Http/Kernel.php:

Route Middleware

To apply minification to specific routes or route groups, register the middleware with an alias:

Laravel 11+ (bootstrap/app.php):

Laravel 10 (app/Http/Kernel.php):

Then apply it to specific routes:

Conditional Minification

The middleware automatically determines if a response should be minified based on several conditions:

This ensures that only actual HTML page responses are minified, avoiding issues with API responses or partial HTML fragments.

Configuration

Customizing Transformers

You can customize which transformers are applied by modifying the config/minify-html.php file:

Disabling Specific Transformers

Remove or comment out any transformer you don't want to use:

Creating Custom Transformers

You can create your own HTML transformers by implementing a simple transform method:

Then add it to your configuration:

Example: Uppercase Title Transformer

Built-in Transformers Explained

RemoveComments

Removes HTML comments while preserving special comments needed by frameworks:

RemoveWhitespace

Intelligently removes whitespace while preserving content in:

The transformer:

  1. Temporarily hides protected elements
  2. Removes multiple spaces, tabs, and newlines
  3. Removes spaces between tags (> < becomes ><)
  4. Restores protected elements

TrimScripts

Removes leading and trailing whitespace from within <script> tags without affecting functionality:

Performance Benefits

HTML minification can reduce your HTML file size by 10-30% on average, depending on your code formatting:

Example size reduction:

Framework Compatibility

Livewire

The package preserves Livewire comments and attributes, ensuring full compatibility:

Inertia.js

Works seamlessly with Inertia.js responses as they are JSON and automatically skipped.

Alpine.js

Alpine.js directives and attributes are preserved:

Troubleshooting

HTML Not Being Minified

  1. Check your response type: Ensure the response has an Accept: text/html header
  2. Verify middleware is registered: Confirm the middleware is in your global or route middleware
  3. Check request method: Only GET and HEAD requests are processed
  4. DOCTYPE declaration: Make sure your HTML includes <!DOCTYPE html> early in the response

Broken Layout or Functionality

  1. Whitespace-dependent CSS: If your layout relies on whitespace between inline elements, add those elements to the ignoreElements array in a custom transformer
  2. Template literals in scripts: Complex JavaScript might need special handling
  3. Pre-formatted content: Ensure <pre> and <textarea> tags are being preserved

Debugging

Temporarily disable transformers one by one to identify which transformation is causing issues:

Testing

Run the package tests:

Run tests with coverage:

Run static analysis:

Fix code style:

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-minify-html-middleware with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0||^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package backstage/laravel-minify-html-middleware contains the following files

Loading the files please wait ...