Permalink
Browse files

Improve particle caching by including js and css into cache

  • Loading branch information...
1 parent 8eddeb4 commit 7c8e039be012c01885d7f95bf4250a895d9c4399 @mahagr mahagr committed Jan 4, 2017
@@ -21,6 +21,7 @@
use Gantry\Component\Stylesheet\CssCompilerInterface;
use Gantry\Component\Theme\ThemeDetails;
use Gantry\Framework\Base\Gantry;
+use Gantry\Framework\Document;
use Gantry\Framework\Services\ConfigServiceProvider;
use RocketTheme\Toolbox\File\PhpFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
@@ -596,6 +597,7 @@ protected function renderContent($item)
$subtype = $item->subtype;
$enabled = $gantry['config']->get("particles.{$subtype}.enabled", 1);
+ $cached = false;
if (!$enabled) {
return '';
@@ -610,33 +612,49 @@ protected function renderContent($item)
$cached = true;
break;
case 'config_matches':
- if (is_array($particle['caching']['values'])) {
- $values = $particle['caching']['values'];
+ if (isset($particle['caching']['values'])) {
+ $values = (array) $particle['caching']['values'];
$compare = array_intersect_key($particle, $values);
$cached = ($values === $compare);
}
break;
}
}
- if (!empty($cached)) {
+ /** @var Document $document */
+ $document = $gantry['document'];
+
+ if ($cached) {
/** @var UniformResourceLocator $locator */
$locator = $gantry['locator'];
$key = md5(json_encode($particle));
$filename = $locator->findResource("gantry-cache://theme/html/{$key}.php", true, true);
$file = PhpFile::instance($filename);
if ($file->exists()) {
- $html = $file->content()['html'];
+ $content = $file->content();
+
+ // Set HTML and assets from the cache.
+ $html = $content['html'];
+ $document->appendHeaderTags($content['assets']);
}
}
if (!isset($html)) {
+ if (isset($file)) {
+ // Create new document context for assets.
+ $document->push();
+ }
+
$context = $this->getContext(['segment' => $item, 'enabled' => 1, 'particle' => $particle, 'prepare_layout' => true]);
$html = trim($this->render("@nucleus/content/{$item->type}.html.twig", $context));
if (isset($file)) {
- $file->save(['html' => $html]);
+ // Restore upper level context and save HTML and assets into the cache.
+ $assets = $document->pop();
+ $document->appendHeaderTags($assets);
+ $file->save(['html' => $html, 'assets' => $assets]);
}
+
}
return !strstr($html, '@@DEFERRED@@') ? $html : null;
@@ -3,6 +3,10 @@ description: Displays Content Tabs.
type: particle
icon: fa-table
+configuration:
+ caching:
+ type: static
+
form:
fields:
enabled:
@@ -3,6 +3,10 @@ description: Display Owl Carousel.
type: particle
icon: fa-sliders
+configuration:
+ caching:
+ type: static
+
form:
fields:
enabled:

0 comments on commit 7c8e039

Please sign in to comment.