Skip to content

Commit 79b1a8d

Browse files
multiwebincJacobBennett
authored andcommitted
Add SVG push support and excluding favicons (#24)
* 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
1 parent ac84d27 commit 79b1a8d

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

src/Middleware/AddHttp2ServerPush.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function fetchLinkableNodes($response)
8787
{
8888
$crawler = $this->getCrawler($response);
8989

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

9393
/**
@@ -107,6 +107,7 @@ private function buildLinkHeaderString($url)
107107
'.JPG' => 'image',
108108
'.JPEG' => 'image',
109109
'.PNG' => 'image',
110+
'.SVG' => 'image',
110111
'.TIFF' => 'image',
111112
];
112113

tests/AddHttp2ServerPushTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace JacobBennett\Http2ServerPush\Test;
44

5-
// TODO: test for invalid file types like .svg
6-
75
use Illuminate\Http\Request;
86
use JacobBennett\Http2ServerPush\Middleware\AddHttp2ServerPush;
97
use Symfony\Component\HttpFoundation\Response;
@@ -57,7 +55,19 @@ public function it_will_return_an_image_link_header_for_images()
5755

5856
$this->assertTrue($this->isServerPushResponse($response));
5957
$this->assertStringEndsWith("as=image", $response->headers->get('link'));
60-
$this->assertCount(6, explode(",", $response->headers->get('link')));
58+
$this->assertCount(7, explode(",", $response->headers->get('link')));
59+
}
60+
61+
/** @test */
62+
public function it_will_return_an_image_link_header_for_svg_objects()
63+
{
64+
$request = new Request();
65+
66+
$response = $this->middleware->handle($request, $this->getNext('pageWithSVGObject'));
67+
68+
$this->assertTrue($this->isServerPushResponse($response));
69+
$this->assertStringEndsWith("as=image", $response->headers->get('link'));
70+
$this->assertCount(1, explode(",", $response->headers->get('link')));
6171
}
6272

6373
/** @test */
@@ -93,6 +103,16 @@ public function it_will_not_return_a_push_header_for_inline_js()
93103
$this->assertFalse($this->isServerPushResponse($response));
94104
}
95105

106+
/** @test */
107+
public function it_will_not_return_a_push_header_for_icons()
108+
{
109+
$request = new Request();
110+
111+
$response = $this->middleware->handle($request, $this->getNext('pageWithFavicon'));
112+
113+
$this->assertFalse($this->isServerPushResponse($response));
114+
}
115+
96116
/** @test */
97117
public function it_will_return_limit_count_of_links()
98118
{

tests/fixtures/pageWithFavicon.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
<link rel="apple-touch-icon-precomposed" href="/favicon-apple.png"/>
6+
<link rel="icon" href="favicon.png">
7+
</head>
8+
9+
<body>
10+
</body>
11+
12+
</html>

tests/fixtures/pageWithImages.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<img src="/images/photo.jpeg" alt="">
1212
<img src="/images/logo.png" alt="">
1313
<img src="/images/noidea.tiff" alt="">
14+
<img src="/images/vector.svg" alt="">
1415
</body>
1516

1617
</html>

tests/fixtures/pageWithSVGObject.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
</head>
6+
7+
<body>
8+
<object data="/images/vector.svg" type="image/svg+xml"></object>
9+
</body>
10+
11+
</html>

0 commit comments

Comments
 (0)