From 5cc4186edb213c65236d1fb0e2746f050184121b Mon Sep 17 00:00:00 2001 From: Filippo Bisconcin Date: Mon, 17 Aug 2020 15:37:06 +0200 Subject: [PATCH] Use "as=fetch" in case no extension is matched (#46) --- src/Middleware/AddHttp2ServerPush.php | 3 +++ tests/AddHttp2ServerPushTest.php | 12 ++++++++++++ tests/fixtures/pageWithFetchPreload.html | 13 +++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/fixtures/pageWithFetchPreload.html diff --git a/src/Middleware/AddHttp2ServerPush.php b/src/Middleware/AddHttp2ServerPush.php index 87cec1d..23db2ee 100644 --- a/src/Middleware/AddHttp2ServerPush.php +++ b/src/Middleware/AddHttp2ServerPush.php @@ -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', '/'); diff --git a/tests/AddHttp2ServerPushTest.php b/tests/AddHttp2ServerPushTest.php index 2ac18da..f7f5ce3 100644 --- a/tests/AddHttp2ServerPushTest.php +++ b/tests/AddHttp2ServerPushTest.php @@ -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('; rel=preload', $response->headers->get('link')); + $this->assertStringEndsWith("as=script", $response->headers->get('link')); + } + /** @test */ public function it_returns_well_formatted_link_headers() { diff --git a/tests/fixtures/pageWithFetchPreload.html b/tests/fixtures/pageWithFetchPreload.html new file mode 100644 index 0000000..e231c28 --- /dev/null +++ b/tests/fixtures/pageWithFetchPreload.html @@ -0,0 +1,13 @@ + + + + Page title + + + + + + + + +