Geeklog-Core / geeklog

Geeklog - The Secure CMS.
https://www.geeklog.net
25 stars 19 forks source link

Plugins are loading in CSS files on Pages not Needed Causing Google Page Insight Issues #1118

Closed eSilverStrike closed 2 years ago

eSilverStrike commented 2 years ago

Render-blocking resources are being caused as plugins like the Polls and Forums are automatically loading their css files (and javascript files) for every page of the website when the plugin is activated.

In reality they should only be loaded on their own pages or if one of their own blocks is being displayed (that needs these assets) or an autotag is being used in some content.

COM_createHTMLDocument should request PLG_getHeaderCode much later in the function after the blocks have been rendered. This way these blocks, plugin pages, autotags etc can set flags to show if other assets (css, javascript) need to be loaded or not in plugin_getheadercode_foo.

Things to consider is:

Once these changes have happened plugins like the Polls, Forum, Downloads, Media Gallery, etc.. should be updated to take advantage of these changes so they only load the assets when needed.

eSilverStrike commented 2 years ago

Okay I have updated COM_createHTMLDocument moving the creation of the header code to the bottom of the function.

I have done some testing and setup some flags for the forum plugin so it knows when plugin_getheadercode_forum function is called by COM_createHTMLDocument if it needs to set the forum.css file to load.

Plugin content that may need css and javascript files set by $_SCRIPT can be displayed on a plugins own page, in a plugins own blocks, and by a plugins autotag inserted into other content or template.

Everything seems to work fine for the most part with the variable flag that signals the css file needs to be loaded except when it comes to Geeklog caching. Template caching is fine as if these files are cached autotags are always run.

The problem I found comes down to caching for content like articles and blocks. When articles or blocks are cached autotags are only run when the cache is generated. So if for example an article has the forum autotag in it. plugin_autotags_forum function will obviously run when the article is viewed and not cached but if it is cached it is only run when the cache is generated. So this means plugin_autotags_forum is not always run so the varible flag cannot be set to indicate if the css file is needed when plugin_getheadercode_forum is run later.

One good thing is that the caching problem just affects autotags so if the plugin doesn't have any or if the autotags themselves do not need anything from the css or javascript files that a plugin could set, then things should be good.

So for example the forum autotag does not need css so I can move forward on this fix.

For the Poll Plugin though, some of its autotags need css from the files so this fix would not work (unless the admin knows not to use a poll autotag in an article or block that gets cached). One work around/solution to this is you could always inline the css or javascript with the autotag.