Skip to content

Let the browser cache the styler CSS until a style is saved - #3774

Merged
Crabcyborg merged 11 commits into
masterfrom
fix/styler-css-caching
Dec 1, 2026
Merged

Crabcyborg merged 11 commits into
masterfrom
fix/styler-css-caching

Conversation

@Crabcyborg

@Crabcyborg Crabcyborg commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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_css response is 1.16 MB. The styler enqueued it with the plugin version, and admin-ajax.php sends nocache_headers(), so the whole file was downloaded on every styler load. The duplicate frmpro_css option is gone (#3784), so the transient is the only cache, and a transient miss is refilled by FrmStyleCssCache::maybe_refill().

Changes:

  • The styler enqueues frm-custom-theme with frm_last_style_update as ver (through the existing get_css_version(), which the front end fallback already uses). That option is updated by every save_settings() call, which runs on style save, style reset, global settings save and plugin upgrade.
  • Cache-Control: private, max-age=86400 replaces the no-cache headers only when ver equals the current frm_last_style_update and 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.
  • The styler enqueues the CSS on admin_init at priority 10, and the style save runs later on admin_init at priority 11. So save_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.
  • The front end fallback (get_url_to_custom_style() when the CSS file cannot be read) already used frm_last_style_update as its version, so those requests become cacheable too.

Testing

  1. Added tests to tests/phpunit/styles/test_FrmStylesController.php for the styler ver, the version after a save, the version match check, and the cache headers (including no cache headers after a transient miss), and to tests/phpunit/styles/test_FrmStyle.php for 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. The header() calls themselves cannot be observed under PHPUnit CLI, so step 2 covers them.
  2. Open Formidable > Styles. In the network panel, the admin-ajax.php?action=frmpro_css&frm_scope_custom_css=1&ver=<frm_last_style_update> response has Cache-Control: private, max-age=86400 and no Pragma: no-cache. Reload the page and the request is served from the browser cache.
  3. Change a style color and save. On the page shown after the save the ver value 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.
  4. Delete the _transient_frmpro_css and _transient_timeout_frmpro_css options, then load the styler. The response keeps the no-cache headers.
  5. Request admin-ajax.php?action=frmpro_css without ver and confirm the no-cache headers are unchanged.

🤖 Generated with Claude Code

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>
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e690e5a and 06c65c8.

Files selected for processing: 4

  • classes/controllers/FrmStylesController.php
  • classes/models/FrmStyle.php
  • tests/phpunit/styles/test_FrmStyle.php
  • tests/phpunit/styles/test_FrmStylesController.php
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

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>
@Crabcyborg
Crabcyborg marked this pull request as ready for review November 30, 2026 21:57
@Crabcyborg

Copy link
Copy Markdown
Contributor Author

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

@franky-the-going-merry franky-the-going-merry Bot added the franky-working Franky is actively reviewing this label Dec 1, 2026

@franky-the-going-merry franky-the-going-merry Bot left a comment

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.

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 7 wp-commands notice).
  • --filter Style → 87 tests, 2 failures: test_front_head and the timestamp test above, 3 runs out of 3. On harness master the same filter has only test_front_head.
  • Mutations: removing the save_style() re-enqueue fails the new re-enqueue test; reverting get_next_css_version() to plain gmdate() 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() (the Cache-Control/Expires emission) 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).

Comment thread tests/phpunit/styles/test_FrmStyle.php Outdated
Comment on lines +349 to +350
// 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 );

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.

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:

Suggested change
// 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.

Comment thread classes/models/FrmStyle.php Outdated
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() );

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.

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.

@franky-the-going-merry franky-the-going-merry Bot removed franky-review franky-working Franky is actively reviewing this labels Dec 1, 2026
Crabcyborg and others added 4 commits December 1, 2026 09:14
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>
@deepsource-io

deepsource-io Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

DeepSource Code Review

We reviewed changes in e690e5a...06c65c8 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

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.

@Crabcyborg

Copy link
Copy Markdown
Contributor Author

Addressed the latest review in a557889 (merged with origin/master and the remote branch, head fb90f75):

  • Blocking 1 (padded timestamp test fails after a same-second save): fixed in a557889, the upper bound is replaced with assertGreaterThan( $previous ) as suggested. --filter Style now passes 3 runs out of 3 (91 tests after the merge).
  • Note 1 (save ordering in save_settings()): fixed in a557889, frm_last_style_update is now written after the CSS option and transient (and still written on the no-file early return). New test test_save_settings_updates_version_after_css fails with the old ordering.
  • Note 2 (no test for the cache headers or the option fallback rule): fixed in a557889, the header decision moved into get_saved_css_cache_headers(), which test_get_saved_css_cache_headers covers (Cache-Control value, Expires date, older version, option fallback sends none). It fails when the transient check is removed or the Cache-Control value changes. The header() calls themselves are not observable under PHPUnit CLI.
  • PR description updated for the new ordering and the reworded option fallback reason.

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.50000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.88%. Comparing base (5ceaf7d) to head (6899d45).
⚠️ Report is 21 commits behind head on master.

Files with missing lines Patch % Lines
classes/controllers/FrmStylesController.php 72.72% 9 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@franky-the-going-merry franky-the-going-merry Bot added the franky-working Franky is actively reviewing this label Dec 1, 2026

@franky-the-going-merry franky-the-going-merry Bot left a comment

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.

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_update is written after the CSS option and transient. Moving it back to the top fails test_save_settings_updates_version_after_css.
  • Cache headers: the decision is in get_saved_css_cache_headers(). Removing the transient check, or changing private to public, fails test_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).

@franky-the-going-merry franky-the-going-merry Bot removed franky-review franky-working Franky is actively reviewing this labels Dec 1, 2026
@Crabcyborg

Copy link
Copy Markdown
Contributor Author

Merged origin/master cleanly (3fca7c7).
Non-blocking (version update in the missing template early return untested): fixed in 6899d45. test_save_settings_updates_version_without_css_template moves css/custom_theme.css.php aside (restored in finally) since no filter or argument controls $filename, and fails without the update_option( 'frm_last_style_update', $version ) in the early return. --filter Style: 93 tests OK.

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>
@Crabcyborg Crabcyborg added this to the 7.2 milestone Dec 1, 2026
@Crabcyborg Crabcyborg changed the title Cache the styler CSS and stop rebuilding it when the transient expires Let the browser cache the styler CSS until a style is saved Dec 1, 2026
@franky-the-going-merry franky-the-going-merry Bot added the franky-working Franky is actively reviewing this label Dec 1, 2026

@franky-the-going-merry franky-the-going-merry Bot left a comment

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.

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, passes false !== $css to the header decision, and only then calls maybe_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 writes frm_last_style_update last (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 ...59 that 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_save and the missing-template test
  • should_cache_saved_css() always true → test_should_cache_saved_css and 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.

@franky-the-going-merry franky-the-going-merry Bot removed franky-review franky-working Franky is actively reviewing this labels Dec 1, 2026
@Crabcyborg
Crabcyborg merged commit 30f324f into master Dec 1, 2026
65 of 67 checks passed
@Crabcyborg
Crabcyborg deleted the fix/styler-css-caching branch December 1, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant