Skip to content

Commit

Permalink
Added: appended version parameter (timestamp) to font URLs to make su…
Browse files Browse the repository at this point in the history
…re cache is busted in browsers upon cache refresh.
  • Loading branch information
Dan0sz committed Jul 23, 2024
1 parent d704d72 commit 2f10719
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/Frontend/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class Process {
'elementor',
];

/** @var string $timestamp */
private $timestamp = '';

/**
* Break out early, e.g. if we want to parse other resources and don't need to
* set up all the hooks and filters.
Expand All @@ -83,20 +80,19 @@ class Process {
*/
private $break = false;

/**
* @var string $timestamp
*/
private $timestamp;

/**
* OMGF_Frontend_Functions constructor.
*
* @var $break bool
*/
public function __construct( $break = false ) {
$this->timestamp = OMGF::get_option( Settings::OMGF_CACHE_TIMESTAMP, '' );
$this->break = $break;

if ( ! $this->timestamp ) {
$this->timestamp = time(); // @codeCoverageIgnore

OMGF::update_option( Settings::OMGF_CACHE_TIMESTAMP, $this->timestamp ); // @codeCoverageIgnore
}
$this->timestamp = OMGF::get_option( Settings::OMGF_CACHE_TIMESTAMP, '' );

$this->init();
}
Expand Down
12 changes: 12 additions & 0 deletions src/Optimize/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function __construct() {
* @return void
*/
private function run() {
$this->generate_timestamp();

OMGF::update_option( Settings::OMGF_OPTIMIZE_HAS_RUN, true );

$front_html = $this->get_front_html( get_home_url() );
Expand All @@ -52,6 +54,16 @@ private function run() {
}
}

/**
* Generates a timestamp and stores it to the DB, which is appended to the stylesheet and fonts URLs.
*
* @see StylesheetGenerator::build_source_string()
* @see self::build_search_replace()
*/
private function generate_timestamp() {
OMGF::update_option( Settings::OMGF_CACHE_TIMESTAMP, time() ); // @codeCoverageIgnore
}

/**
* Wrapper for wp_remote_get() with preset params.
*
Expand Down
11 changes: 8 additions & 3 deletions src/StylesheetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ class StylesheetGenerator {
/** @var string $plugin */
private $plugin;

/** @var int $timestamp */
private $timestamp;

/**
* OMGF_GenerateStylesheet constructor.
*/
public function __construct(
$fonts,
string $plugin
) {
$this->fonts = $fonts;
$this->plugin = $plugin;
$this->fonts = $fonts;
$this->plugin = $plugin;
$this->timestamp = OMGF::get_option( Settings::OMGF_CACHE_TIMESTAMP );
}

/**
Expand Down Expand Up @@ -101,7 +105,8 @@ private function build_source_string( $sources, $type = 'url', $end_semi_colon =
$n = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? "\n" : '';

foreach ( $sources as $format => $url ) {
$source .= "$type('$url')" . ( ! is_numeric( $format ) ? "format('$format')" : '' );
$source_url = $url . '?ver=' . $this->timestamp;
$source .= "$type('$source_url')" . ( ! is_numeric( $format ) ? "format('$format')" : '' );

if ( $url === $last_src && $end_semi_colon ) {
$source .= ";$n";
Expand Down

0 comments on commit 2f10719

Please sign in to comment.