壓縮 HTML 加速網頁響應—— renatomarinho/laravel-page-speed,擴展包可以自動的優化網站,將HTML加速輸出顯示在客戶端瀏覽器。
項目地址:https://github.com/renatomarinho/laravel-page-speed
安裝命令:
$ composer require renatomarinho/laravel-page-speed
擴展包中使用了中間件
//app/Http/Kernel.php
protected $middleware = [
...
\RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class, //轉變html中的行內樣式
\RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class, //去除html中多余的屬性
\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class, //去除html中所有的協議部分http或https,
\RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class, //檢測html中所有的鏈接,并進行預加載
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class, //去除html中所有的注釋信息
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class, //去除html標簽中的引號
\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class, //去除html中所有的換行、空格等信息
]
注意:響應式中間件的讀取順序是從下向上。
我們將中間件注冊在web,所有通過web訪問的數據都可以進行壓縮
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
//壓縮 HTML 加速網頁響應—— renatomarinho/laravel-page-speed
\RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];