Skip to content

Commit 5cc4186

Browse files
authored
Use "as=fetch" in case no extension is matched (#46)
1 parent 0e4705a commit 5cc4186

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Middleware/AddHttp2ServerPush.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ private function buildLinkHeaderString($url)
140140
return Str::contains(strtoupper($url), $extension);
141141
});
142142

143+
if ($url && !$type) {
144+
$type = 'fetch';
145+
}
143146

144147
if(!preg_match('%^(https?:)?//%i', $url)) {
145148
$basePath = $this->getConfig('base_path', '/');

tests/AddHttp2ServerPushTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ public function it_will_return_an_image_link_header_for_svg_objects()
9898
$this->assertCount(1, explode(",", $response->headers->get('link')));
9999
}
100100

101+
/** @test */
102+
public function it_will_return_a_fetch_link_header_for_fetch()
103+
{
104+
$request = new Request();
105+
106+
$response = $this->middleware->handle($request, $this->getNext('pageWithFetchPreload'));
107+
108+
$this->assertTrue($this->isServerPushResponse($response));
109+
$this->assertStringContainsString('</api/resource>; rel=preload', $response->headers->get('link'));
110+
$this->assertStringEndsWith("as=script", $response->headers->get('link'));
111+
}
112+
101113
/** @test */
102114
public function it_returns_well_formatted_link_headers()
103115
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
<link rel="preload" href="/api/resource" as="fetch">
6+
</head>
7+
8+
<body>
9+
10+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
11+
</body>
12+
13+
</html>

0 commit comments

Comments
 (0)