Repository navigation
Let the browser cache the styler CSS until a style is saved - #3774
Conversation
The styler loads the CSS for every style from admin-ajax.php with the plugin version, and admin-ajax.php sends no-cache headers, so the full stylesheet was downloaded on every styler load. When the frmpro_css transient expired, the CSS for every style was rebuilt on every request even though the frmpro_css option still held it. Fall back to the frmpro_css option before rebuilding. Version the styler URL by frm_last_style_update, which changes on every style save, and let the browser cache the response for a day only when the requested version matches and the CSS came from the transient. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 38 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
The styler enqueues the saved CSS on admin_init before the style save runs, so the page shown after a save still linked the old version. The browser then served the CSS from before the save out of its cache. Enqueue it again with the new version once the save is done. The version is the save time, which only changes once a second. Two saves in the same second kept the same version while the CSS changed, so a browser could keep stale CSS for a day. A save in the same second as the last one now moves the version one past it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Verified on the local dev site, which has 89 styles and a 1.16 MB frmpro_css response. Before, on master, the styler loaded the CSS with the plugin version as ver and no-store headers, so all of it downloaded on every load, about 130ms to first byte each time. After, the response has Cache-Control private, max-age=86400 with no Pragma header, and a reload is served from the browser cache with no network request. I found two problems and pushed fixes in commit 533c16f. First, the styler enqueues the CSS on admin_init at priority 10 and the style save runs at priority 11. The page shown right after saving a color still linked the old ver, and the browser served the CSS from before the save, so the preview was stale for up to a day. save_style() now enqueues it again with the new version. After the fix the page shown after the save linked the new ver, fetched it from the server and had the new color. Second, frm_last_style_update only has one second resolution, so two saves in the same second kept the same ver while the CSS changed. A save in the same second as the last one now moves the version one past it. Both have tests that fail without the fix. With the transient deleted, the response was served from the frmpro_css option in about 120ms with the no-cache headers. Its body matched the option by SHA-256 and the transient was not recreated, so nothing was rebuilt. Front end forms still load formidableforms.css with the new ver and render styled. I restored the style color and ran one more save to bring the transient back. The style post, the CSS option, the transient and formidableforms.css are byte for byte identical to before. I did not exercise Reset to default. test_FrmStyle and test_FrmStylesController pass with 27 tests, and the scoped phpcs, php-cs-fixer, rector, mago, phpstan and psalm checks pass. The debug log had nothing new from Formidable. 🤖 Generated with Claude Code |
There was a problem hiding this comment.
Request changes, for one test that fails in the full styles run. Everything else holds up; the caching design is sound, and the other findings are non-blocking.
Blocking: test_FrmStyle::test_save_settings_stores_padded_timestamp_version (the one this PR rewrote) fails whenever an earlier test has saved a style in the same second. Inline comment has the cause and a fix. CI skipped PHPUnit, so nothing else would have caught it.
Tests run at 533c16f (wordpress-develop harness, PR copied in, restored after), php@8.4 vendor/bin/phpunit -c phpunit.xml:
tests/phpunit/styles/test_FrmStyle.php→ OK (15 tests, 90 assertions);test_FrmStylesController.php→ 12 tests, 1 failure,test_front_head, which also fails on master in this harness (WP 7wp-commandsnotice).--filter Style→ 87 tests, 2 failures:test_front_headand the timestamp test above, 3 runs out of 3. On harness master the same filter has onlytest_front_head.- Mutations: removing the
save_style()re-enqueue fails the new re-enqueue test; revertingget_next_css_version()to plaingmdate()fails the new same-second test;should_cache_saved_css()returning true fails the new version-match test.
What I checked by reading: the option fallback is safe because plugin upgrades re-save the default style (FrmMigrate → update('default')), which rewrites the option and bumps the version. The maybe_hide_sample_form_error_message rule depends on the referer, but only the styler page enqueues this URL, so the cached copy always has the styler referer.
Non-blocking notes:
- Save ordering in
FrmStyle::save_settings(), inline. - The new tests never exercise
maybe_send_saved_css_cache_headers()(theCache-Control/Expiresemission) or the "option fallback sends no cache headers" rule. I did not either; I take the live header result from your comment. - Not run: browser/curl header check and the Reset to default flow (you did not exercise it either).
| // A save in the same second as the previous one moves the version one past it. | ||
| $this->assertLessThanOrEqual( max( (int) $after, $previous + 1 ), (int) $version ); |
There was a problem hiding this comment.
Blocking: this upper bound fails when the version runs ahead of the clock. get_next_css_version() returns last + 1 for a save in the same second, so after a few saves in one second the stored version is ahead of $after.
I ran phpunit -c phpunit.xml --filter Style three times at this head and got the same failure each time:
previous=20261201002700 before=20261201002700 after=20261201002700 version=20261201002702
Failed asserting that 20261201002702 is equal to 20261201002701 or is less than 20261201002701.
The version ended two past $previous, not one (something else saved between the read and the assert, which I did not trace). The test passes alone and as a whole file, because there the previous save is in an earlier second. In the full suite, earlier tests save styles in the same second, so it is order-dependent.
What the test needs to prove is that the version is a 14-digit value no older than $before and strictly newer than $previous. Drop the upper bound:
| // A save in the same second as the previous one moves the version one past it. | |
| $this->assertLessThanOrEqual( max( (int) $after, $previous + 1 ), (int) $version ); | |
| // The version is the current time, or past the previous version after a save in the same second. | |
| $this->assertGreaterThan( $previous, (int) $version ); |
I did not run this suggestion. test_save_settings_changes_version_on_every_save already covers the same-second case.
| public function save_settings() { | ||
| $filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; | ||
| update_option( 'frm_last_style_update', gmdate( 'YmdHis' ) ); | ||
| update_option( 'frm_last_style_update', self::get_next_css_version() ); |
There was a problem hiding this comment.
Non-blocking: the version is bumped first, but the transient is only replaced later in this method (clear_cache(), then the CSS is built, then set_transient()). A request for the new ver that lands between this line and clear_cache() still finds the old CSS in the transient, matches the new version in should_cache_saved_css(), and is sent max-age=86400. The browser then keeps the old CSS under the new URL for a day. The window is small (an is_file() check), so this is a low-probability case, not a blocker.
Moving this update_option to the end of the method, after set_transient(), closes it: a new version can then only ever be served with the new CSS. The worst case flips to new CSS under the old version, which the next save corrects. get_next_css_version() reads the old value, so compute $version at the top and write it at the end.
Drop the upper bound in the padded timestamp test, since the version can run ahead of the clock after saves in the same second. Update frm_last_style_update after the CSS option and transient so a request for the new version can never be cached with the old CSS. Move the cache header decision into get_saved_css_cache_headers() so the Cache-Control/Expires values and the option fallback rule are tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/styler-css-caching
|
|
Overall Grade |
Security Reliability Complexity Hygiene |
Code Review Summary
| Analyzer | Status | Updated (UTC) | Details |
|---|---|---|---|
| PHP | Dec 1, 2026 6:56p.m. | Review ↗ | |
| JavaScript | Dec 1, 2026 6:56p.m. | Review ↗ |
Important
AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.
|
Addressed the latest review in a557889 (merged with origin/master and the remote branch, head fb90f75):
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3774 +/- ##
============================================
- Coverage 48.77% 45.88% -2.89%
- Complexity 14330 14356 +26
============================================
Files 239 239
Lines 48151 48232 +81
============================================
- Hits 23484 22133 -1351
- Misses 24667 26099 +1432 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Approve at fb90f75ec (re-review; my Request Changes was at 533c16f3f), with one non-blocking note. The blocking test failure is fixed.
Blocking item, fixed: test_save_settings_stores_padded_timestamp_version now asserts assertGreaterThan( $previous ). --filter Style passes 3 runs out of 3 (91 tests, only the baseline test_front_head failure remains, which fails on harness master too).
Earlier notes, addressed:
- Save ordering:
frm_last_style_updateis written after the CSS option and transient. Moving it back to the top failstest_save_settings_updates_version_after_css. - Cache headers: the decision is in
get_saved_css_cache_headers(). Removing the transient check, or changingprivatetopublic, failstest_get_saved_css_cache_headers.
Tests run at fb90f75ec (wordpress-develop harness, PR copied in and restored; CI's PHPUnit jobs also ran and are green at this head): test_FrmStyle.php → 16 tests OK; test_FrmStylesController.php → 13 tests, 1 failure (test_front_head, baseline); --filter Style x3 → 91 tests, same single failure.
Non-blocking: dropping the update_option( 'frm_last_style_update', $version ) in the ! is_file( $filename ) early return of save_settings() survives every test. A test that points at a missing template (or a comment saying the path cannot be reached) would pin it.
Not exercised: a real response from admin-ajax.php?action=frmpro_css (the header() calls are not observable under PHPUnit CLI, so the headers rest on the author's check and the unit test of the decision), and Reset to default. Review skills not run (the source diff is about 140 lines, read by hand).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged origin/master cleanly (3fca7c7). |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The frmpro_css option was removed on master, so the saved CSS comes from the transient or the refill. Only a transient hit is sent cache headers, since a refill can return CSS that does not match the last save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Approve at 06c65c88d (re-review; my Approve was at fb90f75ec). No blocking items.
What changed since my last Approve: two master merges (including #3784, which made frmpro_css transient-only), 6899d450b (the test for the version bump in the missing-template early return, the note I left last round), and 06c65c88d (drops the frmpro_css option fallback; the cache headers now depend on whether the CSS was a transient hit). I re-read the whole PR diff (4 files, ~300 lines) against current master.
load_saved_css()reads the transient once, passesfalse !== $cssto the header decision, and only then callsmaybe_refill(). A refill or rebuild never gets the 1-day cache headers, so a stylesheet that did not match the last save cannot be kept by the browser. That is the right place for the rule now that the option fallback is gone.save_settings()still writesfrm_last_style_updatelast (and in the no-template early return), so a request for the new version cannot get the old CSS.get_next_css_version(): the same-second case moves the version one past the previous one. After...59that gives...60, which is not a real time but is still unique and below the next real timestamp, so it cannot collide.
Tests run at 06c65c88d (wordpress-develop harness, PR copied in, restored after; CI's PHPUnit jobs are also green at this head), php@8.4 vendor/bin/phpunit -c phpunit.xml --filter Style x2 → 116 tests, 1 failure each time: test_front_head (WP 7 wp-commands notice), which also fails on harness master.
Mutations, each caught:
- skipping the
save_style()re-enqueue →test_save_style_enqueues_saved_css_with_new_version - ignoring
$from_transient→test_get_saved_css_cache_headers - plain
gmdate()version →test_save_settings_changes_version_on_every_saveand the missing-template test should_cache_saved_css()always true →test_should_cache_saved_cssand the headers test
Not exercised: a real admin-ajax.php?action=frmpro_css response (the header() calls are not observable under PHPUnit CLI, so the headers rest on the author's measurements and the unit test of the decision), and Reset to default (the author did not run it either). Review skills not run; the source diff is small and was read by hand.
The styler now lets the browser cache the CSS it loads from
admin-ajax.php?action=frmpro_css, versioned by the last style update so a style save still loads fresh CSS.On a site with many styles the
frmpro_cssresponse is 1.16 MB. The styler enqueued it with the plugin version, andadmin-ajax.phpsendsnocache_headers(), so the whole file was downloaded on every styler load. The duplicatefrmpro_cssoption is gone (#3784), so the transient is the only cache, and a transient miss is refilled byFrmStyleCssCache::maybe_refill().Changes:
frm-custom-themewithfrm_last_style_updateasver(through the existingget_css_version(), which the front end fallback already uses). That option is updated by everysave_settings()call, which runs on style save, style reset, global settings save and plugin upgrade.Cache-Control: private, max-age=86400replaces the no-cache headers only whenverequals the currentfrm_last_style_updateand the CSS came from the transient. Requests without a matching version (Pro entry preview, Views editor, Coupons, the style reset reload which adds&key=to the old URL) keep the no-cache headers. A transient miss is not cached either, because the refill can return CSS whose hash does not match the last save, and a rebuild is not the saved CSS.admin_initat priority 10, and the style save runs later onadmin_initat priority 11. Sosave_style()enqueues it again after the save, and the page shown after a save links the new version instead of the copy the browser cached.FrmStyle::save_settings()stores the save time as the version, which only changes once a second. A save in the same second as the last one now moves the version one past it, so two quick saves never share a version while the CSS changes.FrmStyle::save_settings()now writes the version last, after the refill hash and the transient. A request for the new version can then only get the new CSS, never the previous CSS under a cacheable URL.get_url_to_custom_style()when the CSS file cannot be read) already usedfrm_last_style_updateas its version, so those requests become cacheable too.Testing
tests/phpunit/styles/test_FrmStylesController.phpfor the stylerver, the version after a save, the version match check, and the cache headers (including no cache headers after a transient miss), and totests/phpunit/styles/test_FrmStyle.phpfor a new version on every save, the version being written after the transient and the refill hash, and the version update without the CSS template. Theheader()calls themselves cannot be observed under PHPUnit CLI, so step 2 covers them.admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1&ver=<frm_last_style_update>response hasCache-Control: private, max-age=86400and noPragma: no-cache. Reload the page and the request is served from the browser cache.vervalue is new, the request goes to the server, and the preview shows the new color. Use Reset to default on a style and confirm the preview updates without a reload._transient_frmpro_cssand_transient_timeout_frmpro_cssoptions, then load the styler. The response keeps the no-cache headers.admin-ajax.php?action=frmpro_csswithoutverand confirm the no-cache headers are unchanged.🤖 Generated with Claude Code