keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

update output buffering timing to start earlier #137

Closed coreykn closed 4 years ago

coreykn commented 4 years ago

Updated output buffering to start earlier on the init hook instead of template_redirect. This was done to increase the compatibility with third parties (#78 and #120). Previously anything that added data to the HTML output before template_redirect was not being captured in our buffer. This means in some cases cached pages were incomplete and missing data. The output buffer does not start unless the template used is the directory index. (This is currently set to index.php but a filter will be added in case the directory index has been renamed and set in the server configuration file.) This will improve performance by not starting an output buffer when not needed. All other checks still need to be done in Cache_Enabler::_bypass_cache() because that data being checked is not available at init yet. If a page is expired the stale cached page will now be delivered (through PHP handling) while the cached page is being updated. Changing the output buffering timing in this way will allow PHP to automatically close the output buffer started by Cache Enabler instead of trying to clean our specific output buffer at a different hook fired later (because we would not be certain if the output buffer being cleaned is the one we started).

Updated regular expressions used for checking whether or not the trailing slash exists. This was updated to ensure file extensions that could potentially be cached (e.g. .xml) are also ignored (as briefly explained in 626aa59). This will make checking for trailing slashes more strict and prevent potential unwanted cache bypassing.

Added Cache_Enabler::_is_cacheable() to check whether or not the content of a page should even be cached. This will ensure invalid page content is not processed. This means Cache_Enabler::_is_sitemap() is no longer required and has been removed. Furthermore, checking if the output buffer is empty is not required when a page is only cacheable if it has the required HTML tags. This method was inspired by Autoptimize and is credited to @futtta (thank you).

Closes #78 and closes #120