Skip to content

Commit 6c11537

Browse files
regiszanandreatomschlick
authored andcommitted
Adding support to push Fonts (#19)
* Adding support to push fonts like woff, woff2, otf, ttf * Fixing README * Adding comma to config/server-push
1 parent 6f79139 commit 6c11537

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ protected $middleware = [
3333
## Usage
3434

3535
Now when you enable it on a route it will automatically include the resources in your elixir `/build/rev-manifest.json` file.
36-
To add a resource manually you may use `pushStyle($pathOfCssFile)`, `pushScript($pathOfJsFile)`, or `pushImage($pathOfImageFile)` from anywhere in your project.
36+
To add a resource manually you may use `pushStyle($pathOfCssFile)`, `pushScript($pathOfJsFile)`, `pushFont($pathOfFontFile)` or `pushImage($pathOfImageFile)` from anywhere in your project.

config/server-push.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
'images' => [
1616

1717
],
18+
19+
'fonts' => [
20+
21+
],
1822
],
1923

2024
// Auto link all files from your built manifest

src/HttpPush.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public static function getTypeByExtension(string $resourcePath) : string
7474
switch ($extension) {
7575
case 'css': return 'style';
7676
case 'js': return 'script';
77+
case 'ttf': return 'font';
78+
case 'otf': return 'font';
79+
case 'woff': return 'font';
80+
case 'woff2': return 'font';
7781
default: return 'image';
7882
}
7983
}

src/helpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ function pushStyle(string $resourcePath)
2929
return app('server-push')->queueResource($resourcePath, 'style');
3030
}
3131
}
32+
33+
if (!function_exists('pushFont')) {
34+
/**
35+
* @param string $resourcePath
36+
*/
37+
function pushFont(string $resourcePath)
38+
{
39+
return app('server-push')->queueResource($resourcePath, 'font');
40+
}
41+
}

tests/HttpPushTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public function test_add_resources_to_queue()
3939
$this->assertEquals($expected, $this->instance->resources);
4040
}
4141

42+
public function test_add_fonts_to_queue()
43+
{
44+
$this->instance->queueResource('Roboto.woff', 'font');
45+
46+
$expected = [
47+
[
48+
'path' => 'Roboto.woff',
49+
'type' => 'font',
50+
],
51+
];
52+
$this->assertEquals($expected, $this->instance->resources);
53+
}
54+
4255
public function test_add_external_resources_to_queue()
4356
{
4457
$this->instance->queueResource('https://example.com/style.css', 'style');
@@ -52,13 +65,33 @@ public function test_add_external_resources_to_queue()
5265
$this->assertEquals($expected, $this->instance->resources);
5366
}
5467

68+
public function test_add_external_font_to_queue()
69+
{
70+
$this->instance->queueResource('https://example.com/Roboto.woff2', 'font');
71+
72+
$expected = [
73+
[
74+
'path' => 'https://example.com/Roboto.woff2',
75+
'type' => 'font',
76+
],
77+
];
78+
$this->assertEquals($expected, $this->instance->resources);
79+
}
80+
5581
public function test_resource_generates_link_string()
5682
{
5783
$this->instance->queueResource('/assets/app.js.min', 'script');
5884

5985
$this->assertEquals('</assets/app.js.min>; rel=preload; as=script', $this->instance->generateLinks()[0]);
6086
}
6187

88+
public function test_resource_generates_font_link_string()
89+
{
90+
$this->instance->queueResource('/assets/font/Roboto.woff', 'font');
91+
92+
$this->assertEquals('</assets/font/Roboto.woff>; rel=preload; as=font', $this->instance->generateLinks()[0]);
93+
}
94+
6295
public function test_clear_resources()
6396
{
6497
$this->instance->queueResource('/assets/app.js.min', 'script');
@@ -76,5 +109,10 @@ public function test_get_type_by_extension()
76109
$this->assertEquals('style', (HttpPush::getTypeByExtension('/assets/main.css')));
77110
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/logo.png')));
78111
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/header.gif')));
112+
113+
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.otf')));
114+
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.woff')));
115+
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.woff2')));
116+
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.ttf')));
79117
}
80118
}

0 commit comments

Comments
 (0)