Skip to content

Commit

Permalink
Adding support to push Fonts (#19)
Browse files Browse the repository at this point in the history
* Adding support to push fonts like woff, woff2, otf, ttf

* Fixing README

* Adding comma to config/server-push
  • Loading branch information
regiszanandrea authored and tomschlick committed May 23, 2018
1 parent 6f79139 commit 6c11537
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ protected $middleware = [
## Usage

Now when you enable it on a route it will automatically include the resources in your elixir `/build/rev-manifest.json` file.
To add a resource manually you may use `pushStyle($pathOfCssFile)`, `pushScript($pathOfJsFile)`, or `pushImage($pathOfImageFile)` from anywhere in your project.
To add a resource manually you may use `pushStyle($pathOfCssFile)`, `pushScript($pathOfJsFile)`, `pushFont($pathOfFontFile)` or `pushImage($pathOfImageFile)` from anywhere in your project.
4 changes: 4 additions & 0 deletions config/server-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
'images' => [

],

'fonts' => [

],
],

// Auto link all files from your built manifest
Expand Down
4 changes: 4 additions & 0 deletions src/HttpPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public static function getTypeByExtension(string $resourcePath) : string
switch ($extension) {
case 'css': return 'style';
case 'js': return 'script';
case 'ttf': return 'font';
case 'otf': return 'font';
case 'woff': return 'font';
case 'woff2': return 'font';
default: return 'image';
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ function pushStyle(string $resourcePath)
return app('server-push')->queueResource($resourcePath, 'style');
}
}

if (!function_exists('pushFont')) {
/**
* @param string $resourcePath
*/
function pushFont(string $resourcePath)
{
return app('server-push')->queueResource($resourcePath, 'font');
}
}
38 changes: 38 additions & 0 deletions tests/HttpPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public function test_add_resources_to_queue()
$this->assertEquals($expected, $this->instance->resources);
}

public function test_add_fonts_to_queue()
{
$this->instance->queueResource('Roboto.woff', 'font');

$expected = [
[
'path' => 'Roboto.woff',
'type' => 'font',
],
];
$this->assertEquals($expected, $this->instance->resources);
}

public function test_add_external_resources_to_queue()
{
$this->instance->queueResource('https://example.com/style.css', 'style');
Expand All @@ -52,13 +65,33 @@ public function test_add_external_resources_to_queue()
$this->assertEquals($expected, $this->instance->resources);
}

public function test_add_external_font_to_queue()
{
$this->instance->queueResource('https://example.com/Roboto.woff2', 'font');

$expected = [
[
'path' => 'https://example.com/Roboto.woff2',
'type' => 'font',
],
];
$this->assertEquals($expected, $this->instance->resources);
}

public function test_resource_generates_link_string()
{
$this->instance->queueResource('/assets/app.js.min', 'script');

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

public function test_resource_generates_font_link_string()
{
$this->instance->queueResource('/assets/font/Roboto.woff', 'font');

$this->assertEquals('</assets/font/Roboto.woff>; rel=preload; as=font', $this->instance->generateLinks()[0]);
}

public function test_clear_resources()
{
$this->instance->queueResource('/assets/app.js.min', 'script');
Expand All @@ -76,5 +109,10 @@ public function test_get_type_by_extension()
$this->assertEquals('style', (HttpPush::getTypeByExtension('/assets/main.css')));
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/logo.png')));
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/header.gif')));

$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.otf')));
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.woff')));
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.woff2')));
$this->assertEquals('font', (HttpPush::getTypeByExtension('/assets/Roboto.ttf')));
}
}

0 comments on commit 6c11537

Please sign in to comment.