Skip to content

Conversation

@grzegorz-cp
Copy link
Contributor

@grzegorz-cp grzegorz-cp commented Dec 23, 2025

Fixes MYJP-268

Proposed changes:

  • The maybe_show_red_bubble() function runs on every WordPress admin page load and triggers external API calls to WordPress.com (that might timeout and delay page rendering) when the cache expires. The solution defers these API calls to an asynchronous REST request that runs after the page has loaded. My Jetpack page maintains existing logic to preserve React and window object implementation.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

  • Checkout locally
  • Go to My Jetpack
  • Clear the red bubble cache
    jetpack docker wp option delete _transient_my-jetpack-red-bubble-transient
    jetpack docker wp option delete _transient_timeout_my-jetpack-red-bubble-transient
  • Refresh the page and verify that a call to red-bubble-notifications endpoint was made, but HTML doesn't have async-notification-bubble.js script appended.
  • Go to any WordPress admin page (e.g., Posts)
    • Page loads immediately (no blocking)
    • You should NOT see a POST request to red-bubble-notifications (cache should be used)
    • If alerts exist, the Jetpack menu badge appears/updates after the request completes
  • Clear the red bubble cache
    jetpack docker wp option delete _transient_my-jetpack-red-bubble-transient
    jetpack docker wp option delete _transient_timeout_my-jetpack-red-bubble-transient
  • Refresh the page
    • Page loads immediately (no blocking)
    • You should see a POST request to red-bubble-notifications (cache should be used)
    • HTML has async-notification-bubble.js script appended
    • If alerts exist, the Jetpack menu badge appears/updates after the request completes

@github-actions github-actions bot added [Package] My Jetpack [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Status] In Progress labels Dec 23, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 23, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: February 3, 2026
    • Code freeze: February 3, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Backup plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Boost plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Search plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Social plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Starter Plugin plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Videopress plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Dec 23, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 23, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the SOCIAL-268-bubble-async branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack SOCIAL-268-bubble-async

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@grzegorz-cp grzegorz-cp self-assigned this Dec 23, 2025
@jp-launch-control
Copy link

jp-launch-control bot commented Dec 23, 2025

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/my-jetpack/src/class-initializer.php 54/312 (17.31%) -1.00% 17 💔
projects/packages/my-jetpack/src/class-red-bubble-notifications.php 21/192 (10.94%) -0.12% 2 ❤️‍🩹

1 file is newly checked for coverage.

File Coverage
projects/packages/my-jetpack/_inc/utils/async-notification-bubble.ts 0/18 (0.00%) 💔

Full summary · PHP report · JS report

Coverage check overridden by I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. .

@manzoorwanijk
Copy link
Member

manzoorwanijk commented Dec 23, 2025

There seems to be some mistake about the linked issue. SOCIAL-268 is a different issue. Is it MYJP-268 instead?

@grzegorz-cp grzegorz-cp changed the title SOCIAL-268 Update My Jetpack red bubble notifications MYJP-268 Update My Jetpack red bubble notifications Jan 4, 2026
@grzegorz-cp
Copy link
Contributor Author

There seems to be some mistake about the linked issue. SOCIAL-268 is a different issue. Is it MYJP-268 instead?

Yes, it's related to MYJP-268. Thank you noticing this.

@grzegorz-cp grzegorz-cp added I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 8, 2026
@grzegorz-cp grzegorz-cp requested a review from a team January 8, 2026 08:18
@grzegorz-cp grzegorz-cp marked this pull request as ready for review January 8, 2026 08:18
Copilot AI review requested due to automatic review settings January 8, 2026 08:18
} )
.catch( ( error: Error ) => {
// eslint-disable-next-line no-console
console.error( '[My Jetpack] Failed to fetch notification alerts:', error );
Copy link
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 sure if this is bringing any value.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the performance of My Jetpack's red bubble notifications by deferring expensive external API calls to an asynchronous REST request that runs after page load. The change prevents blocking page rendering when the notification cache expires, while preserving the existing synchronous behavior on the My Jetpack page itself.

  • Introduces async loading of red bubble notifications via JavaScript when cache is unavailable
  • Adds a new get_cached_alerts() method to check for cached data without triggering API calls
  • Updates logic to use cached data only on non-My Jetpack admin pages to avoid blocking

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
projects/packages/my-jetpack/webpack.config.js Adds new webpack entry point for async notification bubble script
projects/packages/my-jetpack/src/class-red-bubble-notifications.php Adds get_cached_alerts() method to retrieve cached notifications without expensive computation
projects/packages/my-jetpack/src/class-initializer.php Updates maybe_show_red_bubble() to use cached data on non-My Jetpack pages and enqueue async script when cache is empty; adds enqueue_red_bubble_script() method
projects/packages/my-jetpack/_inc/utils/async-notification-bubble.ts New TypeScript utility that fetches alerts via REST API and updates the menu bubble DOM asynchronously
projects/plugins/*/changelog/SOCIAL-268-bubble-async Changelog entries documenting the performance improvement (9 files total)
projects/packages/my-jetpack/changelog/SOCIAL-268-bubble-async Package-level changelog entry

Comment on lines +1 to +46
/**
* My Jetpack Notification Bubble async loader.
* Fetches fresh alert data via REST API without blocking page load.
*/
import apiFetch from '@wordpress/api-fetch';

// Minimal type for counting non-silent alerts.
type Alert = {
is_silent?: boolean;
};

type AlertsResponse = Record< string, Alert >;

apiFetch< AlertsResponse >( {
path: 'my-jetpack/v1/red-bubble-notifications',
method: 'POST',
} )
.then( alerts => {
const count = Object.values( alerts ).filter( a => ! a.is_silent ).length;
const menuItem = document.querySelector( '#toplevel_page_jetpack .wp-menu-name' );

if ( ! menuItem ) {
return;
}

const bubble = menuItem.querySelector( '.awaiting-mod' );

if ( count > 0 ) {
if ( bubble ) {
bubble.className = 'awaiting-mod';
bubble.textContent = String( count );
} else {
const span = document.createElement( 'span' );
span.className = 'awaiting-mod';
span.textContent = String( count );
menuItem.appendChild( document.createTextNode( ' ' ) );
menuItem.appendChild( span );
}
} else if ( bubble ) {
bubble.remove();
}
} )
.catch( ( error: Error ) => {
// eslint-disable-next-line no-console
console.error( '[My Jetpack] Failed to fetch notification alerts:', error );
} );
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

The new async-notification-bubble.ts utility lacks test coverage. Consider adding unit tests to verify the DOM manipulation logic (bubble creation, update, and removal) and the error handling for API failures. This is particularly important since this code runs on every admin page when the cache is empty.

Copilot uses AI. Check for mistakes.
Comment on lines +371 to +380
/**
* Get cached red bubble alerts without triggering expensive computation.
* Returns the cached transient value or an empty array if not cached.
*
* @return array Cached alerts or empty array.
*/
public static function get_cached_alerts() {
$stored_alerts = get_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY );
return $stored_alerts !== false ? $stored_alerts : array();
}
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

The new get_cached_alerts() method lacks test coverage. Consider adding unit tests to verify it correctly returns cached values when available and an empty array when the cache doesn't exist.

Copilot uses AI. Check for mistakes.
Comment on lines +373 to +379
* Returns the cached transient value or an empty array if not cached.
*
* @return array Cached alerts or empty array.
*/
public static function get_cached_alerts() {
$stored_alerts = get_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY );
return $stored_alerts !== false ? $stored_alerts : array();
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

The get_cached_alerts() method returns an empty array when there's no cache, making it impossible to distinguish between "cache doesn't exist" and "cache exists but contains no alerts". This causes an unnecessary async script load when the cache legitimately contains an empty array (no alerts to show). The check if ( empty( $cached_alerts ) ) will trigger an async fetch in both cases. Consider modifying get_cached_alerts() to return false when the cache doesn't exist, preserving the ability to distinguish these two states.

Suggested change
* Returns the cached transient value or an empty array if not cached.
*
* @return array Cached alerts or empty array.
*/
public static function get_cached_alerts() {
$stored_alerts = get_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY );
return $stored_alerts !== false ? $stored_alerts : array();
* Returns the cached transient value or false if not cached.
*
* @return array|false Cached alerts array or false if no cache exists.
*/
public static function get_cached_alerts() {
return get_transient( self::MY_JETPACK_RED_BUBBLE_TRANSIENT_KEY );

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings January 8, 2026 08:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

manzoorwanijk
manzoorwanijk previously approved these changes Jan 8, 2026
Copy link
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

It works as expected. I have some minor suggestions for code quality improvements.

}

$rbn = new Red_Bubble_Notifications();
// Check if we're on the My Jetpack page
Copy link
Member

Choose a reason for hiding this comment

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

Let us avoid adding PHPCS errors. This file already has too many of them.

Suggested change
// Check if we're on the My Jetpack page
// Check if we're on the My Jetpack page.


// filters for the items in this file
add_filter( 'my_jetpack_red_bubble_notification_slugs', array( $rbn, 'add_red_bubble_alerts' ) );
// Filter out silent alerts
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Filter out silent alerts
// Filter out silent alerts.

*/
public static function enqueue_red_bubble_script() {
Assets::register_script(
'my_jetpack_notification_bubble',
Copy link
Member

Choose a reason for hiding this comment

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

Handles are usually with a hyphen instead of underscore.

Suggested change
'my_jetpack_notification_bubble',
'my-jetpack-notification-bubble',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't seen a string with mixed hyphens and underscores in the codebase, but there are places where everything has underscores, even if it contains "my_jetpack", so I think it's better to keep the proposed value.

Co-authored-by: Manzoor Wani <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

method: 'POST',
} )
.then( alerts => {
const count = Object.values( alerts ).filter( a => ! a.is_silent ).length;
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

If the API returns null, undefined, or a non-object value, calling Object.values() will throw a runtime error. While apiFetch with proper typing should prevent this, consider adding a defensive check to ensure alerts is a valid object before processing it.

Suggested change
const count = Object.values( alerts ).filter( a => ! a.is_silent ).length;
const alertValues = alerts && typeof alerts === 'object' ? Object.values( alerts ) : [];
const count = alertValues.filter( a => ! a.is_silent ).length;

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. [Package] My Jetpack [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Status] In Progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants