Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Better assets pipelining #917

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions system/src/Grav/Common/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ public function addCss($asset, $priority = null, $pipeline = true, $group = null
return $this;
}

$modified = 0;
if (!$this->isRemoteLink($asset)) {
$modified = $this->getLastModificationTime($asset);
$asset = $this->buildLocalLink($asset);
}

Expand All @@ -295,8 +297,9 @@ public function addCss($asset, $priority = null, $pipeline = true, $group = null
'asset' => $asset,
'priority' => intval($priority ?: 10),
'order' => count($this->css),
'pipeline' => (bool)$pipeline,
'group' => $group ?: 'head'
'pipeline' => (bool) $pipeline,
'group' => $group ?: 'head',
'modified' => $modified
];

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

$modified = 0;
if (!$this->isRemoteLink($asset)) {
$modified = $this->getLastModificationTime($asset);
$asset = $this->buildLocalLink($asset);
}

Expand All @@ -356,9 +361,10 @@ public function addJs($asset, $priority = null, $pipeline = true, $loading = nul
'asset' => $asset,
'priority' => intval($priority ?: 10),
'order' => count($this->js),
'pipeline' => (bool)$pipeline,
'pipeline' => (bool) $pipeline,
'loading' => $loading ?: '',
'group' => $group ?: 'head'
'group' => $group ?: 'head',
'modified' => $modified
];

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

/** @var Cache $cache */
$cache = Grav::instance()['cache'];

// clear no-pipeline assets lists
$this->css_no_pipeline = [];

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

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

/** @var Cache $cache */
$cache = Grav::instance()['cache'];

// clear no-pipeline assets lists
$this->js_no_pipeline = [];

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

Expand Down Expand Up @@ -1100,18 +1100,37 @@ protected function isRemoteLink($link)
/**
* Build local links including grav asset shortcodes
*
* @param string $asset the asset string reference
* @param string $asset the asset string reference
* @param bool $absolute build absolute asset link
*
* @return string the final link url to the asset
* @return string the final link url to the asset
*/
protected function buildLocalLink($asset)
protected function buildLocalLink($asset, $absolute = false)
{
try {
$asset = Grav::instance()['locator']->findResource($asset, false);
$asset = Grav::instance()['locator']->findResource($asset, $absolute);
} catch (\Exception $e) {
}

return $asset ? $this->base_url . ltrim($asset, '/') : false;
$uri = $absolute ? $asset : $this->base_url . ltrim($asset, '/');
return $asset ? $uri : false;
}

/**
* Get the last modification time of asset
*
* @param string $asset the asset string reference
*
* @return string the last modifcation time or false on error
*/
protected function getLastModificationTime($asset)
{
$file = GRAV_ROOT . $asset;
if (Grav::instance()['locator']->isStream($asset)) {
$file = $this->buildLocalLink($asset, true);
}

return file_exists($file) ? filemtime($file) : false;
}

/**
Expand Down
24 changes: 16 additions & 8 deletions tests/unit/Grav/Common/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function testAddingAssets()
'priority' => 10,
'order' => 0,
'pipeline' => true,
'group' => 'head'
'group' => 'head',
'modified' => false
], reset($array));

$this->assets->add('test.js');
Expand All @@ -53,7 +54,8 @@ public function testAddingAssets()
'priority' => 10,
'order' => 0,
'pipeline' => true,
'group' => 'head'
'group' => 'head',
'modified' => false
], reset($array));

//test addCss(). Test adding asset to a separate group
Expand All @@ -68,7 +70,8 @@ public function testAddingAssets()
'priority' => 10,
'order' => 0,
'pipeline' => true,
'group' => 'head'
'group' => 'head',
'modified' => false
], reset($array));

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

//Test CSS Groups
Expand All @@ -107,7 +111,8 @@ public function testAddingAssets()
'priority' => 10,
'order' => 0,
'pipeline' => true,
'group' => 'footer'
'group' => 'footer',
'modified' => false
], reset($array));

//Test JS Groups
Expand All @@ -125,7 +130,8 @@ public function testAddingAssets()
'order' => 0,
'pipeline' => true,
'loading' => '',
'group' => 'footer'
'group' => 'footer',
'modified' => false
], reset($array));

//Test async / defer
Expand All @@ -141,7 +147,8 @@ public function testAddingAssets()
'order' => 0,
'pipeline' => true,
'loading' => 'async',
'group' => 'head'
'group' => 'head',
'modified' => false
], reset($array));

$this->assets->reset();
Expand All @@ -156,7 +163,8 @@ public function testAddingAssets()
'order' => 0,
'pipeline' => true,
'loading' => 'defer',
'group' => 'head'
'group' => 'head',
'modified' => false
], reset($array));

//Test adding media queries
Expand Down