Skip to content

Closes #1315: Keep next-gen progress visible until done - #1348

Open
faisalahammad wants to merge 5 commits into
wp-media:developfrom
faisalahammad:fix/1315-nextgen-progress-loader
Open

faisalahammad wants to merge 5 commits into
wp-media:developfrom
faisalahammad:fix/1315-nextgen-progress-loader

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #1315

When next-gen generation runs on the settings page (after saving AVIF/WebP, or with the "Generate missing next-gen versions" button), the progress bar was hidden as soon as the remaining count reached 0. Users never saw n/n and got no message, so it looked like the process stopped. The first progress check also waited 15 seconds, so small runs finished before anything moved.

Now the bar shows n/n, stays full, the button is hidden and a "Next-gen generation is complete." message is shown. The first progress check is sent right away.

Type of change

  • New feature (non-breaking change which adds functionality).
  • Bug fix (non-breaking change which fixes an issue).
  • Enhancement (non-breaking change which improves an existing functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as before).
  • Sub-task of #(issue number)
  • Chore
  • Release

Detailed scenario

What was tested

Manual:

  • AVIF after saving settings: counter starts within a few seconds, ends at n/n with the done message.
  • WebP: same result.
  • "Generate missing next-gen versions" button: same result.
  • No credits / invalid key: error popup shows as before, no done message.
  • Reload during a run: bar comes back with the current count and finishes.
  • Reload after a run is done: no bar, no done message, the page shows the normal state.

Automated: npm run build (ESLint), npm run test:unit (Jest), composer test-unit, composer run-stan, PHPCS on the changed PHP file. All pass.

How to test

  1. Have some images optimized by Imagify, with backups, and without AVIF versions.
  2. Settings > Imagify, set "Next-Gen image format" to AVIF and save.
  3. Watch the "Generating missing next-gen" bar. It should start moving within a few seconds.
  4. Expected: at the end the bar stays at n/n (full), the button is hidden and the text says "Next-gen generation is complete."
  5. Reload the settings page right after the run ends. Expected: no progress bar, no done message, and the button is not stuck disabled.
  6. Reload during a run (before n/n). Expected: the bar comes back with the current count and finishes.
  7. Repeat with WebP and with the "Generate missing next-gen versions" button.

Affected Features & Quality Assurance Scope

Settings page next-gen generation progress (Imagifybeat polling in assets/js/options.js). No PHP logic change, only a new translatable string.

Technical description

Documentation

  • processQueueImagifybeat() paints the bar first, then calls processFinished( true ) when remaining === 0. Before, it finished before painting.
  • processFinished( completed ): when completed is true, the bar is kept, the button and the "missing" text are hidden, and a <p role="status"> is added with native DOM right after the single "missing" paragraph. Its text is set 100 ms after insertion so screen readers announce it.
  • init() and launchProcess() call imagify.beat.connectNow() so the first count comes right away.
  • init() resumes a run only when remaining > 0. The imagify_missing_next_gen_total transient stays up to an hour after a run, so a reload after a finished run must not show the run UI again.
  • Ticks are ignored when no run is shown, while the launch request is still waiting for its answer (launching flag), and after the launch answers until a new Imagifybeat request was sent (waitingFreshTick flag). Imagifybeat can send the next request from the previous request's .always() callback, before that previous tick fires, so the flag is cleared on the next task. This way an old count can not finish or reset the new run.
  • New string nextGenDoneText in inc/functions/i18n.php.

New dependencies

None.

Risks

Low, JS only. Known limits not changed by this PR: a media counts as done once any one of its sizes has a next-gen version, so n/n can show while the last full sizes are still converting. A media that keeps failing with a temporary error keeps the bar at x/n.

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

Unticked items justification

No JS test covers options.js today (it is a jQuery file bound to the settings page markup). The change was tested by hand on the settings page.

Additional Checks

  • In the case of complex code, I wrote comments to explain it.
  • When possible, I prepared ways to observe the implemented system (logs, data, etc.)
  • I added error handling logic when using functions that could throw errors (HTTP/API request, filesystem, etc.)

The "Generating missing next-gen" bar was hidden as soon as the
remaining count hit 0, so users never saw n/n and got no message.
The first count also took 15 seconds, so small runs ended unseen.

Now the bar shows n/n, stays full and a done message is shown.
The first progress request is sent right away, and ticks that come
before the run starts are ignored.

Fixes wp-media#1315
@codacy-production

codacy-production Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 12 complexity · 0 duplication

Metric Results
Complexity 12
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@remyperona

Copy link
Copy Markdown
Contributor

Note

Generated by the AI delivery pipeline (lead-reviewer · standalone).

Review: CHANGES REQUESTED (one MEDIUM convention finding; logic is sound)

Blockers:

  • [MEDIUM] assets/js/options.js:863 — new element built with jQuery ($( '<p role="status"></p>' ).text(...).insertAfter(...)). AGENTS.md: no jQuery in new or modified JS. Fix: document.createElement('p'), setAttribute('role','status'), textContent = ..., then this.$missingWebpMessage.get(0).after(el) (or .last()[0]). Safe today (.text() does not parse HTML), so this is convention only.

Inline findings (rendered, dry run):

  • assets/js/options.js:863 — [MEDIUM] see above. Also $missingWebpMessage is $('.generate-missing-webp p'), a collection: insertAfter on a multi-element collection clones the node after each match. Confirm the template has exactly one p, or target a single element.
  • assets/js/options.js:672 — [LOW] stale-tick race remains: a tick request sent before the click but answered after launching is cleared still carries the previous run's remaining === 0, and would now show "done" for a run that just started. Window is narrow (needs an in-flight tick across the launch round-trip). Cheap hardening: ignore completion unless total > 0 and this run's launch response total matches, or clear launching only after the next tick sent post-launch.
  • assets/js/options.js:858 — [LOW] completed path hides $button and leaves $progressWrap visible with no reset; the done message is not removed anywhere. Fine because a new run needs a reload (button hidden), but note on reload progress_next_gen.remaining === 0 with a still-cached imagify_missing_next_gen_total transient re-enters the "running" UI in init() and the first tick shows the done message again. Acceptable, but worth a conscious decision (the old code just hid the bar).
  • assets/js/options.js:864 — [LOW] message inserted dynamically with role="status": live regions injected together with their content are often not announced. Optional: render an empty role="status" container up front and fill it.

Verified OK:

  • Done text: set via .text() from wp_localize_script'd __() string; no innerHTML, no XSS. i18n key nextGenDoneText is in the right labels array, text domain imagify, matches the JS reference.
  • launching is reset on .done (first statement, before every early return) and .fail; no path leaves it true. processFinished sets working=false, so repeated ticks cannot double-append the message (new !working guard). Error paths (stopProcess) still go through processFinished() with no argument, so unchanged behaviour. connectNow exists in beat.js (exported at line 851).
  • options.min.js: rebuilt options.js with local uglifyjs; output is byte-identical to the committed min file apart from a trailing newline (same check on develop gives the same result), and the file contains launching, connectNow, nextGenDoneText. Min file is in sync.
  • Observable behaviour change: on completion the bar now stays at n/n with a message instead of disappearing. Intentional per PR title; no spec to compare.

Tests:

  • No test added. package.json does have test:unit (Jest, Tests/js: getProgress, bulkState, helpers), so AGENTS.md's "no JS runner" note is outdated. optionsBulk is an IIFE-scoped jQuery object and not importable, so testing the new tick/launching/completed logic would need extracting it into a pure helper next to getProgress/bulkState (feasible, follow-up) or a Playwright spec. Not blocking.

Nice-to-haves:

  • assets/js/options.js — extract the "should this tick be applied / is run complete" decision into a pure function with Jest coverage.
  • AGENTS.md — update the Traps entry about no JS unit runner.
  • Other new lines ($progressBar.css, $progressText.text) follow the file's existing jQuery-object pattern; only the element creation is cleanly replaceable natively.

- Create the next-gen done message with native DOM, after the single
  missing-message paragraph, instead of jQuery.
- Fill the role="status" live region after insertion so screen readers
  announce it.
- After the launch request answers, ignore ticks until a new Imagifybeat
  request was sent, so a tick from before the launch can not finish the
  new run.

Addresses PR feedback.

Refs wp-media#1348
@faisalahammad

Copy link
Copy Markdown
Contributor Author

@remyperona Thanks for the review. I pushed 1fda655 and merged the latest develop.

  • [MEDIUM] jQuery element (options.js:863): Fixed. The done message is now built with document.createElement( 'p' ), setAttribute( 'role', 'status' ) and textContent. It is inserted with this.$missingWebpMessage.get( 0 ).after( el ), so it goes in only once. The template has one p today, and the single target keeps it safe if that changes.
  • [LOW] Stale tick race (options.js:672): Fixed. After the launch answers, a new waitingFreshTick flag ignores ticks until a new Imagifybeat request was sent. Imagifybeat can send the next request from the .always() callback of the previous one, before that previous (stale) tick fires. So the flag is cleared on the next task (setTimeout( 0 )), and the stale tick is still ignored.
  • [LOW] Completed path and reload: Kept as is, on purpose. After a reload with a cached total and remaining === 0, the first tick shows the done message again. That is the correct state, and the button stays hidden, so no new run can start without a reload.
  • [LOW] Live region: Fixed in JS. The empty role="status" element goes in first, and its text is set 100 ms later, so screen readers announce it.
  • Tests: No new Jest test. optionsBulk is inside an IIFE and can not be imported. Moving the tick decision to a pure helper can be a follow-up if you want it.

Checks: ESLint clean, Jest 44/44 pass. The E2E and Code Coverage failures come from the missing IMAGIFY_TESTS_API_KEY secret on fork PRs, not from this change.

Could you take another look?

@remyperona remyperona 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.

Note

Generated by the AI delivery pipeline (lead-reviewer · standalone).

Review: CHANGES REQUESTED

One blocker: reloading the settings page after a completed run brings the "done" state back, with the button hidden, for up to an hour. Details are in the inline comment on options.js:582.

What holds up

  • launching, waitingFreshTick and the connectNow() calls stop an old count from finishing or resetting a new run. This relies on jQuery's callback order in beat.js (.always() before .done()) and a 0 ms timer, so it is fragile, but it is correct today.
  • The new DOM code uses native APIs and textContent, with no innerHTML. The jQuery calls added (.css, .hide, .slideDown) match the rest of this legacy file.
  • options.min.js was rebuilt from the new source.
  • The new string uses __() with the imagify text domain and is passed through wp_localize_script.

Tests

  • No automated test covers the launching / waitingFreshTick logic. The Jest suite in Tests/js only covers helpers and getProgress.
  • This JS admin UI is a frontend change, so it needs a Playwright spec, or at least a "reload after done" step in the manual test plan.

Comment thread assets/js/options.js Resolved
Comment thread assets/js/options.js Resolved
Comment thread inc/functions/i18n.php Resolved
- Resume the next-gen run in init() only when remaining > 0, so the
  cached total no longer replays the done state after a reload.
- Change the done message to "Next-gen generation is complete."
- Shorten the addQueueImagifybeat() comment to two lines.

Addresses PR feedback.

Refs wp-media#1348

@faisalahammad faisalahammad left a comment

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.

@remyperona Thanks for the review. I pushed a54f1f8 and merged the latest develop.

  • [MEDIUM] Reload after a done run (options.js:582): Fixed. init() resumes a run only when remaining > 0, so the cached total no longer brings back the done state or hides the button.
  • Nit, comment length: Fixed, now 2 lines.
  • Nit, done message: Changed to "Next-gen generation is complete."
  • Tests: I added "reload after done" and "reload during a run" steps to the How to test section. I did not add a Playwright spec in this PR. I can add one if you want it here.

Local checks: npm run build, npm run test:unit, composer test-unit, composer run-stan, PHPCS. All pass.

Can you take another look?

@faisalahammad

Copy link
Copy Markdown
Contributor Author

@remyperona Two CI checks fail on this PR, but not because of the code change.

All other checks pass: PHPUnit on all PHP and WP versions, Jest, PHPCS, PHPStan and Codacy.

Can you run CI with secrets for this branch? For example, push it to a branch in wp-media/imagify-plugin, or re-run it in a way that has access to the secrets. I can also make the key-dependent specs skip when no key is set, but I think that fits better in a separate PR.

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.

Settings — "Generating missing next-gen" loader is hidden without showing progress

2 participants