Skip to content

Fix/cache requests to remote asset checks#118

Merged
zlateska merged 4 commits intomainfrom
fix/cache-requests-to-remote-asset-checks
Nov 7, 2024
Merged

Fix/cache requests to remote asset checks#118
zlateska merged 4 commits intomainfrom
fix/cache-requests-to-remote-asset-checks

Conversation

@BrianHenryIE
Copy link
Copy Markdown
Contributor

Proposed changes

Currently, ‎CSSUtilities::is_valid_remote_file() is not cached, resulting in eight remote calls when loading the WordPress editor as each of the CSS and JS's location and version are determined, once through _wp_get_iframed_editor_assets() and once through wp_common_block_scripts_and_styles().

This PR:

Type of Change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Video

Checklist

  • I have read the CONTRIBUTING doc
  • I have viewed my change in a web-browser
  • Linting and tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Copy link
Copy Markdown
Contributor

@zlateska zlateska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case where a status code of 200 is stored in the transient, if the files become unavailable at the remote URL, the local files (both CSS and JS) will not be loaded. We could consider the following options:

  1. Reduce the transient expiration time to 1 hour: This would result in less time with broken CSS and JS, but at the cost of increased requests compared to checking once a day.
  2. Use JavaScript to determine if the files have loaded: Dynamically load them if they are unavailable. This approach could potentially remove the need to validate the remote file in advance, but it would be more complex to develop.

return false;
}
// Reverse the url because transient key length is limited and truncated and the unique part of the URL is its end.
$transient_key = 'nfd_css_utilities_valid_' . strrev($url );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use md5 instead of strrev for generating the transient key? md5 provides a fixed-length, unique hash that fits well within transient key length limits.

$transient_key = 'nfd_css_utilities_valid_' . md5($url);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zlateska I trust your judgement and think md5 is a good fit here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too keen on MD5. We really only have two unique values here so don't need a true hash (it's computationally expensive) and although I'm reversing the string, it still lets us see which is the css and which is the js transient. Since it's a private function, we could comment the function with the possible inputs and use one of:

  • pathinfo( $url, PATHINFO_EXTENSION ) => css
  • basename( $url ) => utilities.css

@BrianHenryIE
Copy link
Copy Markdown
Contributor Author

@zlateska

if the files become unavailable at the remote URL, the local files (both CSS and JS) will not be loaded.

In the case of a single site admin, the file should be cached locally for that 24 hours, then if the files are not available next time they're polled, the local version will be used. Where this would be problem would be the second admin (or browser) trying to load the script a few hours later and it's not available remotely.

I chose one-day for the expiry time because that's what was currently in use for the nfd_utilities_version transient. I don't feel strongly about it – one hour seems fine to me, realistically it will only be checked once or a few times per day, nobody is editing their site each of all 24 hours! The data module caches capabilities for 4 hours, I don't know how measured that decision was.

@zlateska zlateska merged commit 8d49600 into main Nov 7, 2024
@zlateska zlateska deleted the fix/cache-requests-to-remote-asset-checks branch November 15, 2024 16:09
@0aveRyan 0aveRyan restored the fix/cache-requests-to-remote-asset-checks branch November 21, 2024 00:50
@0aveRyan 0aveRyan deleted the fix/cache-requests-to-remote-asset-checks branch December 6, 2024 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants