Plugins and caching

@Perlkonig Your plugin is using grav-plugin-shortcode-core to process the shortcode embedded in the page.

Inside your GChartShortcode::process() function you call addInlineJS to add Google’s charting scripts.
ShortCode_Core calls your GChartShortcode::process() function inside Grav’s onPageContentProcessed event. This event is only called once when cache is enabled.

On first run, the shortcode in the page is perfectly replaced by the placeholder for the chart. Also your inline javascript gets added to the Assets manager and added to the <head> of the page.

I’m just a Grav newbee and not that knowledgable on caching, so the following might be wrong.

While debugging the processing of a page, I noticed that Assets::js() is called on every page request. So assets are probably not cached and need to be added to the Asset manager at each request. Since your code is only called once on first page load, the required Google Charts script is no longer added. You can see in the generated page on subsequent loads that the scripts are missing.

When I change your script a little, the script is embedded into the page and also cached.

// $this->grav['assets']->addInlineJs($code);
...
return $output . "<script>$code</script>";

Now, the chart is shown on each and every page request. Cached or not…

1 Like