Skip to content

Commit 657c74c

Browse files
committed
feat: added ability to push image assets by parsing response for img with src and common extensions
1 parent e2142a0 commit 657c74c

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

src/Middleware/AddHttp2ServerPush.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function handle(Request $request, Closure $next)
3333
return $response;
3434
}
3535

36-
$this->fetchLinkableNodes($response);
36+
$this->generateAndAttachLinkHeaders($response);
3737

3838
return $response;
3939
}
@@ -43,20 +43,16 @@ public function handle(Request $request, Closure $next)
4343
*
4444
* @return $this
4545
*/
46-
protected function fetchLinkableNodes(Response $response)
46+
protected function generateAndAttachLinkHeaders(Response $response)
4747
{
48-
$crawler = $this->getCrawler($response);
49-
50-
$nodes = $crawler->filter('link, script[src]')->extract(['src', 'href']);
51-
52-
$headers = collect($nodes)->flatten(1)
53-
->filter()
48+
$headers = $this->fetchLinkableNodes($response)
49+
->flatten(1)
5450
->map(function ($url) {
5551
return $this->buildLinkHeaderString($url);
5652
})->filter()
5753
->implode(',');
5854

59-
if (! empty(trim($headers))) {
55+
if (!empty(trim($headers))) {
6056
$this->addLinkHeader($response, $headers);
6157
}
6258

@@ -80,26 +76,52 @@ protected function getCrawler(Response $response)
8076
}
8177

8278
/**
79+
* Get all nodes we are interested in pushing
80+
*
81+
* @param $response
82+
*
83+
* @return \Illuminate\Support\Collection
84+
*/
85+
protected function fetchLinkableNodes($response)
86+
{
87+
$crawler = $this->getCrawler($response);
88+
89+
return collect($crawler->filter('link, script[src], img[src]')->extract(['src', 'href']));
90+
}
91+
92+
/**
93+
* Build out header string based on asset extension
94+
*
8395
* @param String $url
96+
*
8497
* @return string
8598
*/
8699
private function buildLinkHeaderString($url)
87100
{
88101
$linkTypeMap = [
89-
'css' => 'style',
90-
'js' => 'script',
102+
'.CSS' => 'style',
103+
'.JS' => 'script',
104+
'.BMP' => 'image',
105+
'.GIF' => 'image',
106+
'.JPG' => 'image',
107+
'.JPEG' => 'image',
108+
'.PNG' => 'image',
109+
'.TIFF' => 'image',
91110
];
92111

93-
$type = collect($linkTypeMap)->first(function($extension) use($url) {
94-
return str_contains($url, $extension);
112+
$type = collect($linkTypeMap)->first(function ($extension) use ($url) {
113+
return str_contains(strtoupper($url), $extension);
95114
});
96115

97116
return is_null($type) ? null : "<{$url}>; rel=preload; as={$type}";
98117

99118
}
100119

101120
/**
121+
* Add Link Header
122+
*
102123
* @param Response $response
124+
*
103125
* @param $link
104126
*/
105127
private function addLinkHeader(Response $response, $link)

tests/AddHttp2ServerPushTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
class AddHttp2ServerPushTest extends \PHPUnit_Framework_TestCase
1212
{
13+
1314
public function setUp()
1415
{
1516
$this->middleware = new AddHttp2ServerPush();
1617
}
1718

18-
// /** @test */
19+
/** @test */
1920
public function it_will_not_modify_a_response_with_no_server_push_assets()
2021
{
2122
$request = new Request();
@@ -47,6 +48,18 @@ public function it_will_return_a_js_link_header_for_js()
4748
$this->assertStringEndsWith("as=script", $response->headers->get('link'));
4849
}
4950

51+
/** @test */
52+
public function it_will_return_a_image_link_header_for_images()
53+
{
54+
$request = new Request();
55+
56+
$response = $this->middleware->handle($request, $this->getNext('pageWithImages'));
57+
58+
$this->assertTrue($this->isServerPushResponse($response));
59+
$this->assertStringEndsWith("as=image", $response->headers->get('link'));
60+
$this->assertCount(6, explode(",", $response->headers));
61+
}
62+
5063
/** @test */
5164
public function it_returns_well_formatted_link_headers()
5265
{

tests/fixtures/pageWithImages.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
</head>
6+
7+
<body>
8+
<img src="/images/mspaint.bmp" alt="">
9+
<img src="/images/cats.gif" alt="">
10+
<img src="/images/photo.jpg" alt="">
11+
<img src="/images/photo.jpeg" alt="">
12+
<img src="/images/logo.png" alt="">
13+
<img src="/images/noidea.tiff" alt="">
14+
</body>
15+
16+
</html>

0 commit comments

Comments
 (0)