Skip to content

Commit

Permalink
Add SVG push support and excluding favicons (#24)
Browse files Browse the repository at this point in the history
* Add SVG push support

* Update AddHttp2ServerPush.php

* Adding page with SVG object

* Adding tests for SVG in <img> and <object> tags

* Adding SVG <img>

* Update pageWithSVGObject.html

* Excluding favicons

When preloading favicon files (e.g. regular favicon or Apple touch icon), Chrome complains with the following message:

> The resource https://example.net/favicon.png was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.

The syntax for icon files is like:

```
<link rel="icon" href="/favicon.png">
<link rel="apple-touch-icon-precomposed" href="/favicon-apple.png"/>
```

So excluding anything that contains `icon` somewhere in the `rel` attribute would exclude these from being preloaded.

* Add files via upload

* Adding test for favicons
  • Loading branch information
multiwebinc authored and JacobBennett committed Jan 23, 2019
1 parent ac84d27 commit 79b1a8d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function fetchLinkableNodes($response)
{
$crawler = $this->getCrawler($response);

return collect($crawler->filter('link, script[src], img[src]')->extract(['src', 'href']));
return collect($crawler->filter('link:not([rel*="icon"]), script[src], img[src], object[data]')->extract(['src', 'href', 'data']));
}

/**
Expand All @@ -107,6 +107,7 @@ private function buildLinkHeaderString($url)
'.JPG' => 'image',
'.JPEG' => 'image',
'.PNG' => 'image',
'.SVG' => 'image',
'.TIFF' => 'image',
];

Expand Down
26 changes: 23 additions & 3 deletions tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace JacobBennett\Http2ServerPush\Test;

// TODO: test for invalid file types like .svg

use Illuminate\Http\Request;
use JacobBennett\Http2ServerPush\Middleware\AddHttp2ServerPush;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -57,7 +55,19 @@ public function it_will_return_an_image_link_header_for_images()

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

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

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

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

/** @test */
Expand Down Expand Up @@ -93,6 +103,16 @@ public function it_will_not_return_a_push_header_for_inline_js()
$this->assertFalse($this->isServerPushResponse($response));
}

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

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

$this->assertFalse($this->isServerPushResponse($response));
}

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

<head>
<title>Page title</title>
<link rel="apple-touch-icon-precomposed" href="/favicon-apple.png"/>
<link rel="icon" href="favicon.png">
</head>

<body>
</body>

</html>
1 change: 1 addition & 0 deletions tests/fixtures/pageWithImages.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<img src="/images/photo.jpeg" alt="">
<img src="/images/logo.png" alt="">
<img src="/images/noidea.tiff" alt="">
<img src="/images/vector.svg" alt="">
</body>

</html>
11 changes: 11 additions & 0 deletions tests/fixtures/pageWithSVGObject.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>

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

<body>
<object data="/images/vector.svg" type="image/svg+xml"></object>
</body>

</html>

0 comments on commit 79b1a8d

Please sign in to comment.