Make WordPress Core

Opened 2 days ago

Last modified 11 hours ago

#66740 new defect (bug)

JavaScript error in user-profile.js when leaving a user screen before it has finished loading

Reported by: rahultank Owned by:
Priority: normal Milestone: Awaiting Review
Component: Users Version:
Severity: normal Keywords: has-patch
Cc: Focuses: javascript, administration

Description

wp-admin/js/user-profile.js binds its beforeunload handler as soon as the script runs, but $form is only set in the DOM ready callback. If you leave profile.php, user-edit.php or user-new.php before DOM ready, the handler throws:

Uncaught TypeError: Cannot read properties of undefined (reading 'serialize')
    at wp-admin/js/user-profile.js:604

The gap is easy to hit. On these screens about 45 scripts load in the footer after user-profile.js (React, wp-components, wp-block-editor, wp-core-data, the command palette, and others). On a slow connection, the page is already usable while they are still loading.

Steps to reproduce

  1. In Chrome DevTools, open the Console, turn on "Preserve log", and set Network throttling to "Slow 4G".
  2. Go to Users > Profile.
  3. Before the page finishes loading, click "Dashboard" in the admin menu.
  4. The console shows the error above.

Patch

The attached patch adds a check for $form in the handler. Before DOM ready, the form cannot have changed, so returning nothing is the correct result. Another option is to bind the handler inside the ready callback; I kept the smaller change.

Testing

Tested on WordPress 7.1.5 with PHP 8.3, in Playground, with Chrome 158. The patched file was served in place of user-profile.js. The changed lines are the same in trunk, and the patch applies cleanly to trunk.

  • Leaving profile.php, user-edit.php and user-new.php before DOM ready: the error appears without the patch, and does not appear with the patch.
  • After the page has loaded, the "changes you made will be lost" prompt still shows when a field has been changed, and still does not show when nothing has changed. The result is the same with and without the patch.

AI assistance: Yes. An AI assistant helped automate the browser tests and draft this ticket. I reviewed the code change and the results.

Attachments (1)

user-profile-beforeunload.diff​ (456 bytes ) - added by rahultank 2 days ago.
Check that $form is set before using it in the beforeunload handler.

Download all attachments as: .zip

Change History (2)

@rahultank
2 days ago

Check that $form is set before using it in the beforeunload handler.

This ticket was mentioned in ​PR #16140 on ​WordPress/wordpress-develop by ​@therssoftware.


11 hours ago
#1

Description

src/js/_enqueues/admin/user-profile.js binds its beforeunload handler immediately when the script executes, while $form is only initialized inside the DOM-ready callback. Navigating away from profile.php, user-edit.php, or user-new.php before DOM ready throws:

Uncaught TypeError: Cannot read properties of undefined (reading 'serialize')

Changes

  • Added a $form truthiness check in the beforeunload handler (if ( $form && originalFormContent !== $form.serialize() && ! isSubmitting )) in src/js/_enqueues/admin/user-profile.js.

Testing

  • npx eslint src/js/_enqueues/admin/user-profile.js — passed (0 errors, 0 warnings).
  • Verified beforeunload returns undefined without throwing before DOM ready, returns undefined after DOM ready when the form is unchanged, and returns the unsaved-changes prompt when the form is modified.
Note: See TracTickets for help on using tickets.