Skip to content

Commit ac24645

Browse files
author
Tom Schlick
committed
auto link items from an elixir /build/rev-manifest.json file
1 parent a75ac09 commit ac24645

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

config/server-push.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
'default_links' => [
77
'styles' => [
88

9-
],
9+
],
1010

1111
'scripts' => [
1212

13-
],
13+
],
1414

1515
'images' => [
1616

17-
],
17+
],
1818
],
1919

20+
// Auto link all files that are built with elixir
21+
'autolink_elixir' => true,
2022
];

src/HttpPush.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ class HttpPush
1818
* @param string $resourcePath
1919
* @param string $type
2020
*/
21-
public function queueResource(string $resourcePath, string $type)
21+
public function queueResource(string $resourcePath, string $type = null)
2222
{
23+
if (!$type) {
24+
$type = static::getTypeByExtension($resourcePath);
25+
}
26+
2327
$this->resources[] = [
2428
'path' => $resourcePath,
2529
'type' => $type,
@@ -36,7 +40,7 @@ public function generateLinks() : array
3640
$links = [];
3741

3842
foreach ($this->resources as $row) {
39-
$links[] = '<'.$row['path'].'>; rel=preload; as='.$row['type'];
43+
$links[] = '<' . $row['path'] . '>; rel=preload; as=' . $row['type'];
4044
}
4145

4246
return $links;
@@ -57,4 +61,27 @@ public function clear()
5761
{
5862
$this->resources = [];
5963
}
64+
65+
/**
66+
* @param string $resourcePath
67+
* @return string
68+
*/
69+
public static function getTypeByExtension(string $resourcePath) : string
70+
{
71+
$parts = explode('.', $resourcePath);
72+
$extension = end($parts);
73+
switch ($extension) {
74+
case 'css':
75+
return 'style';
76+
break;
77+
78+
case 'js':
79+
return 'script';
80+
break;
81+
82+
default:
83+
return 'image';
84+
break;
85+
}
86+
}
6087
}

src/ServiceProvider.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ServiceProvider extends BaseServiceProvider
1515
public function boot()
1616
{
1717
$this->publishes([
18-
__DIR__.'/../config/server-push.php' => config_path('server-push.php'),
18+
__DIR__ . '/../config/server-push.php' => config_path('server-push.php'),
1919
], 'config');
2020
}
2121

@@ -31,15 +31,15 @@ public function register()
3131
});
3232

3333
$this->registerDefaultLinks();
34+
$this->registerElixirLinks();
3435
}
3536

3637
/**
3738
* Load the default links from the config.
3839
*/
3940
protected function registerDefaultLinks()
4041
{
41-
$this->mergeConfigFrom(__DIR__.'/../config/server-push.php', 'server-push');
42-
42+
$this->mergeConfigFrom(__DIR__ . '/../config/server-push.php', 'server-push');
4343
$instance = app('server-push');
4444

4545
foreach (config('server-push.default_links', []) as $type => $paths) {
@@ -49,4 +49,20 @@ protected function registerDefaultLinks()
4949
}
5050
}
5151
}
52+
53+
protected function registerElixirLinks()
54+
{
55+
$this->mergeConfigFrom(__DIR__ . '/../config/server-push.php', 'server-push');
56+
$instance = app('server-push');
57+
58+
if (config('server-push.autolink_elixir')) {
59+
$revPath = public_path() . '/build/rev-manifest.json';
60+
if (file_exists($revPath)) {
61+
$revMap = json_decode($revPath, true);
62+
foreach (array_values($revMap) as $path) {
63+
$instance->queueResource('/' . ltrim($path, '/'));
64+
}
65+
}
66+
}
67+
}
5268
}

tests/HttpPushTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ public function test_clear_resources()
5454
$this->instance->clear();
5555
$this->assertTrue(empty($this->instance->resources));
5656
}
57+
58+
public function test_get_type_by_extension()
59+
{
60+
$this->assertEquals('script', (HttpPush::getTypeByExtension('/build/app.js')));
61+
$this->assertEquals('script', (HttpPush::getTypeByExtension('app.min.js')));
62+
63+
$this->assertEquals('style', (HttpPush::getTypeByExtension('/assets/main.css')));
64+
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/logo.png')));
65+
$this->assertEquals('image', (HttpPush::getTypeByExtension('/assets/header.gif')));
66+
}
5767
}

0 commit comments

Comments
 (0)