Skip to content

Commit

Permalink
Use "as=fetch" in case no extension is matched (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
biscofil authored Aug 17, 2020
1 parent 0e4705a commit 5cc4186
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ private function buildLinkHeaderString($url)
return Str::contains(strtoupper($url), $extension);
});

if ($url && !$type) {
$type = 'fetch';
}

if(!preg_match('%^(https?:)?//%i', $url)) {
$basePath = $this->getConfig('base_path', '/');
Expand Down
12 changes: 12 additions & 0 deletions tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public function it_will_return_an_image_link_header_for_svg_objects()
$this->assertCount(1, explode(",", $response->headers->get('link')));
}

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

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

$this->assertTrue($this->isServerPushResponse($response));
$this->assertStringContainsString('</api/resource>; rel=preload', $response->headers->get('link'));
$this->assertStringEndsWith("as=script", $response->headers->get('link'));
}

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

<head>
<title>Page title</title>
<link rel="preload" href="/api/resource" as="fetch">
</head>

<body>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
</body>

</html>

0 comments on commit 5cc4186

Please sign in to comment.