Skip to content

Commit 2c7e7c2

Browse files
committed
fix: minify only html pages
1 parent 26e3624 commit 2c7e7c2

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ ubuntu-latest ]
12-
php: [ 8.0, 7.4 ]
12+
php: [ 8.0, 8.1 ]
1313
dependency-version: [ prefer-stable ]
1414

1515
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

src/HtmlMinifyMiddleware.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ public function handle(Request $request, \Closure $next)
2121
{
2222
$response = $next($request);
2323

24-
if ($response instanceof Response && $response->headers->contains('Content-Type', 'text/html')) {
24+
if ($response instanceof StreamedResponse || $response instanceof JsonResponse) {
25+
return $next($request);
26+
}
27+
28+
if ($response instanceof Response && $this->isValidHTMLResponse($response)) {
2529
$html = (new HtmlMinify($response->getContent()))->minifiedHtml();
2630

2731
return $response->setContent($html);
2832
}
2933

3034
return $next($request);
3135
}
36+
37+
protected function isValidHTMLResponse($response)
38+
{
39+
$contentType = $response->headers->get('Content-Type');
40+
41+
return stripos($contentType, 'text/html') !== false;
42+
}
3243
}

tests/TestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ protected function defineEnvironment($app): void
7272
Statamic::pushWebRoutes(function () {
7373
Route::namespace('\\Octoper\\HtmlMinify\\\Http\\Controllers')->group(function () {
7474
Route::get('/html-minify/test', function () {
75-
return file_get_contents(__DIR__.'/testPages/simpleMinify.html');
75+
return response(file_get_contents(__DIR__.'/testPages/simpleMinify.html'), '200', [
76+
'Content-Type' => 'text/html'
77+
]);
7678
})->middleware(HtmlMinifyMiddleware::class);
7779

7880
Route::get('/html-minify/test/remove-comments', function () {
79-
return file_get_contents(__DIR__.'/testPages/removeComments.html');
81+
return response(file_get_contents(__DIR__.'/testPages/removeComments.html'), '200', [
82+
'Content-Type' => 'text/html'
83+
]);
8084
})->middleware(HtmlMinifyMiddleware::class);
8185
});
8286
});

0 commit comments

Comments
 (0)