Skip to content

Commit 1fb2d73

Browse files
Sommerregenrhukster
authored andcommitted
Feature: Better assets pipelining (#917)
* Add smart assets pipeline generation * Drop cache key * Fix absolute path generation in `buildLocalLink` * Add unit test case
1 parent 3529d19 commit 1fb2d73

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

system/src/Grav/Common/Assets.php

+36-17
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ public function addCss($asset, $priority = null, $pipeline = true, $group = null
282282
return $this;
283283
}
284284

285+
$modified = 0;
285286
if (!$this->isRemoteLink($asset)) {
287+
$modified = $this->getLastModificationTime($asset);
286288
$asset = $this->buildLocalLink($asset);
287289
}
288290

@@ -295,8 +297,9 @@ public function addCss($asset, $priority = null, $pipeline = true, $group = null
295297
'asset' => $asset,
296298
'priority' => intval($priority ?: 10),
297299
'order' => count($this->css),
298-
'pipeline' => (bool)$pipeline,
299-
'group' => $group ?: 'head'
300+
'pipeline' => (bool) $pipeline,
301+
'group' => $group ?: 'head',
302+
'modified' => $modified
300303
];
301304

302305
// check for dynamic array and merge with defaults
@@ -343,7 +346,9 @@ public function addJs($asset, $priority = null, $pipeline = true, $loading = nul
343346
return $this;
344347
}
345348

349+
$modified = 0;
346350
if (!$this->isRemoteLink($asset)) {
351+
$modified = $this->getLastModificationTime($asset);
347352
$asset = $this->buildLocalLink($asset);
348353
}
349354

@@ -356,9 +361,10 @@ public function addJs($asset, $priority = null, $pipeline = true, $loading = nul
356361
'asset' => $asset,
357362
'priority' => intval($priority ?: 10),
358363
'order' => count($this->js),
359-
'pipeline' => (bool)$pipeline,
364+
'pipeline' => (bool) $pipeline,
360365
'loading' => $loading ?: '',
361-
'group' => $group ?: 'head'
366+
'group' => $group ?: 'head',
367+
'modified' => $modified
362368
];
363369

364370
// check for dynamic array and merge with defaults
@@ -671,14 +677,11 @@ protected function pipelineCss($group = 'head')
671677
// temporary list of assets to pipeline
672678
$temp_css = [];
673679

674-
/** @var Cache $cache */
675-
$cache = Grav::instance()['cache'];
676-
677680
// clear no-pipeline assets lists
678681
$this->css_no_pipeline = [];
679682

680683
// Compute uid based on assets and timestamp
681-
$uid = md5(json_encode($this->css) . $this->css_minify . $this->css_rewrite . $group . $cache->getKey());
684+
$uid = md5(json_encode($this->css) . $this->css_minify . $this->css_rewrite . $group);
682685
$file = $uid . '.css';
683686
$inline_file = $uid . '-inline.css';
684687

@@ -756,14 +759,11 @@ protected function pipelineJs($group = 'head')
756759
// temporary list of assets to pipeline
757760
$temp_js = [];
758761

759-
/** @var Cache $cache */
760-
$cache = Grav::instance()['cache'];
761-
762762
// clear no-pipeline assets lists
763763
$this->js_no_pipeline = [];
764764

765765
// Compute uid based on assets and timestamp
766-
$uid = md5(json_encode($this->js) . $this->js_minify . $group . $cache->getKey());
766+
$uid = md5(json_encode($this->js) . $this->js_minify . $group);
767767
$file = $uid . '.js';
768768
$inline_file = $uid . '-inline.js';
769769

@@ -1100,18 +1100,37 @@ protected function isRemoteLink($link)
11001100
/**
11011101
* Build local links including grav asset shortcodes
11021102
*
1103-
* @param string $asset the asset string reference
1103+
* @param string $asset the asset string reference
1104+
* @param bool $absolute build absolute asset link
11041105
*
1105-
* @return string the final link url to the asset
1106+
* @return string the final link url to the asset
11061107
*/
1107-
protected function buildLocalLink($asset)
1108+
protected function buildLocalLink($asset, $absolute = false)
11081109
{
11091110
try {
1110-
$asset = Grav::instance()['locator']->findResource($asset, false);
1111+
$asset = Grav::instance()['locator']->findResource($asset, $absolute);
11111112
} catch (\Exception $e) {
11121113
}
11131114

1114-
return $asset ? $this->base_url . ltrim($asset, '/') : false;
1115+
$uri = $absolute ? $asset : $this->base_url . ltrim($asset, '/');
1116+
return $asset ? $uri : false;
1117+
}
1118+
1119+
/**
1120+
* Get the last modification time of asset
1121+
*
1122+
* @param string $asset the asset string reference
1123+
*
1124+
* @return string the last modifcation time or false on error
1125+
*/
1126+
protected function getLastModificationTime($asset)
1127+
{
1128+
$file = GRAV_ROOT . $asset;
1129+
if (Grav::instance()['locator']->isStream($asset)) {
1130+
$file = $this->buildLocalLink($asset, true);
1131+
}
1132+
1133+
return file_exists($file) ? filemtime($file) : false;
11151134
}
11161135

11171136
/**

tests/unit/Grav/Common/AssetsTest.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function testAddingAssets()
4040
'priority' => 10,
4141
'order' => 0,
4242
'pipeline' => true,
43-
'group' => 'head'
43+
'group' => 'head',
44+
'modified' => false
4445
], reset($array));
4546

4647
$this->assets->add('test.js');
@@ -53,7 +54,8 @@ public function testAddingAssets()
5354
'priority' => 10,
5455
'order' => 0,
5556
'pipeline' => true,
56-
'group' => 'head'
57+
'group' => 'head',
58+
'modified' => false
5759
], reset($array));
5860

5961
//test addCss(). Test adding asset to a separate group
@@ -68,7 +70,8 @@ public function testAddingAssets()
6870
'priority' => 10,
6971
'order' => 0,
7072
'pipeline' => true,
71-
'group' => 'head'
73+
'group' => 'head',
74+
'modified' => false
7275
], reset($array));
7376

7477
//test addCss() adding asset to a separate group, and with an alternate rel attribute
@@ -90,7 +93,8 @@ public function testAddingAssets()
9093
'order' => 0,
9194
'pipeline' => true,
9295
'loading' => '',
93-
'group' => 'head'
96+
'group' => 'head',
97+
'modified' => false
9498
], reset($array));
9599

96100
//Test CSS Groups
@@ -107,7 +111,8 @@ public function testAddingAssets()
107111
'priority' => 10,
108112
'order' => 0,
109113
'pipeline' => true,
110-
'group' => 'footer'
114+
'group' => 'footer',
115+
'modified' => false
111116
], reset($array));
112117

113118
//Test JS Groups
@@ -125,7 +130,8 @@ public function testAddingAssets()
125130
'order' => 0,
126131
'pipeline' => true,
127132
'loading' => '',
128-
'group' => 'footer'
133+
'group' => 'footer',
134+
'modified' => false
129135
], reset($array));
130136

131137
//Test async / defer
@@ -141,7 +147,8 @@ public function testAddingAssets()
141147
'order' => 0,
142148
'pipeline' => true,
143149
'loading' => 'async',
144-
'group' => 'head'
150+
'group' => 'head',
151+
'modified' => false
145152
], reset($array));
146153

147154
$this->assets->reset();
@@ -156,7 +163,8 @@ public function testAddingAssets()
156163
'order' => 0,
157164
'pipeline' => true,
158165
'loading' => 'defer',
159-
'group' => 'head'
166+
'group' => 'head',
167+
'modified' => false
160168
], reset($array));
161169

162170
//Test adding media queries

0 commit comments

Comments
 (0)