Skip to content

Commit

Permalink
feat: added ability to push image assets by parsing response for img …
Browse files Browse the repository at this point in the history
…with src and common extensions
  • Loading branch information
JacobBennett committed Jul 31, 2016
1 parent e2142a0 commit 657c74c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
48 changes: 35 additions & 13 deletions src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(Request $request, Closure $next)
return $response;
}

$this->fetchLinkableNodes($response);
$this->generateAndAttachLinkHeaders($response);

return $response;
}
Expand All @@ -43,20 +43,16 @@ public function handle(Request $request, Closure $next)
*
* @return $this
*/
protected function fetchLinkableNodes(Response $response)
protected function generateAndAttachLinkHeaders(Response $response)
{
$crawler = $this->getCrawler($response);

$nodes = $crawler->filter('link, script[src]')->extract(['src', 'href']);

$headers = collect($nodes)->flatten(1)
->filter()
$headers = $this->fetchLinkableNodes($response)
->flatten(1)
->map(function ($url) {
return $this->buildLinkHeaderString($url);
})->filter()
->implode(',');

if (! empty(trim($headers))) {
if (!empty(trim($headers))) {
$this->addLinkHeader($response, $headers);
}

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

/**
* Get all nodes we are interested in pushing
*
* @param $response
*
* @return \Illuminate\Support\Collection
*/
protected function fetchLinkableNodes($response)
{
$crawler = $this->getCrawler($response);

return collect($crawler->filter('link, script[src], img[src]')->extract(['src', 'href']));
}

/**
* Build out header string based on asset extension
*
* @param String $url
*
* @return string
*/
private function buildLinkHeaderString($url)
{
$linkTypeMap = [
'css' => 'style',
'js' => 'script',
'.CSS' => 'style',
'.JS' => 'script',
'.BMP' => 'image',
'.GIF' => 'image',
'.JPG' => 'image',
'.JPEG' => 'image',
'.PNG' => 'image',
'.TIFF' => 'image',
];

$type = collect($linkTypeMap)->first(function($extension) use($url) {
return str_contains($url, $extension);
$type = collect($linkTypeMap)->first(function ($extension) use ($url) {
return str_contains(strtoupper($url), $extension);
});

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

}

/**
* Add Link Header
*
* @param Response $response
*
* @param $link
*/
private function addLinkHeader(Response $response, $link)
Expand Down
15 changes: 14 additions & 1 deletion tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

class AddHttp2ServerPushTest extends \PHPUnit_Framework_TestCase
{

public function setUp()
{
$this->middleware = new AddHttp2ServerPush();
}

// /** @test */
/** @test */
public function it_will_not_modify_a_response_with_no_server_push_assets()
{
$request = new Request();
Expand Down Expand Up @@ -47,6 +48,18 @@ public function it_will_return_a_js_link_header_for_js()
$this->assertStringEndsWith("as=script", $response->headers->get('link'));
}

/** @test */
public function it_will_return_a_image_link_header_for_images()
{
$request = new Request();

$response = $this->middleware->handle($request, $this->getNext('pageWithImages'));

$this->assertTrue($this->isServerPushResponse($response));
$this->assertStringEndsWith("as=image", $response->headers->get('link'));
$this->assertCount(6, explode(",", $response->headers));
}

/** @test */
public function it_returns_well_formatted_link_headers()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/pageWithImages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>

<head>
<title>Page title</title>
</head>

<body>
<img src="/images/mspaint.bmp" alt="">
<img src="/images/cats.gif" alt="">
<img src="/images/photo.jpg" alt="">
<img src="/images/photo.jpeg" alt="">
<img src="/images/logo.png" alt="">
<img src="/images/noidea.tiff" alt="">
</body>

</html>

0 comments on commit 657c74c

Please sign in to comment.