Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP 6.7RC-1: wp.mediaUtils.uploadMedia no longer returns Promise #66551

Closed
3 of 6 tasks
elzadj opened this issue Oct 29, 2024 · 2 comments
Closed
3 of 6 tasks

WP 6.7RC-1: wp.mediaUtils.uploadMedia no longer returns Promise #66551

elzadj opened this issue Oct 29, 2024 · 2 comments
Labels
[Feature] Media Anything that impacts the experience of managing media Needs Technical Feedback Needs testing from a developer perspective. [Type] Bug An existing feature does not function as intended

Comments

@elzadj
Copy link

elzadj commented Oct 29, 2024

Description

Hi all!

We were testing our plugin on WordPress 6.7RC-1, and noticed that our media uploader no longer works. Managed to track it to the differences in wp.mediaUtils.uploadMedia between 6.6.2 and 6.7RC-1.

We use this simple code for tests (based on the readme ):

wp.mediaUtils.uploadMedia( {
	filesList: [ new File( [ 'foo' ], 'foo.txt', { type: 'text/plain' } ) ],
	onFileChange: ( [ fileObj ] ) => console.log( fileObj ),
	onError: console.error,
} );

Compare the results between versions:

6.7RC-1:
Image

6.6.2:
Image

We can adjust code in our plugin to make it work on both versions, but would be nice to know if this change was intentional.
Thanks.

Step-by-step reproduction instructions

  1. Update to 6.7RC-1
  2. Open Gutenberg Editor.
  3. Run
wp.mediaUtils.uploadMedia( {
	filesList: [ new File( [ 'foo' ], 'foo.txt', { type: 'text/plain' } ) ],
	onFileChange: ( [ fileObj ] ) => console.log( fileObj ),
	onError: console.error,
} ); 

in your console.
4. Check the result.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.7RC-1
  • Chrome Version 130.0.6723.58 (Official Build) (64-bit)
  • Ubuntu 24.04.02

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@elzadj elzadj added the [Type] Bug An existing feature does not function as intended label Oct 29, 2024
@Mamaduka Mamaduka added [Feature] Media Anything that impacts the experience of managing media Needs Technical Feedback Needs testing from a developer perspective. labels Nov 5, 2024
@Mamaduka
Copy link
Member

Mamaduka commented Nov 5, 2024

@swissspidy, could this be related to the recent changes made to this utility method?

@swissspidy
Copy link
Member

Yes. This was introduced in #64784.

Previously, it was an async function (thus returning a Promise with a void return value) because it uploaded files in a for loop with an await in it. That means all uploads were done in sequence, which is inefficient.

As part of the conversion, this was changed to use Array.prototype.map so the uploads are queued in parallel. That also meant there was no longer a need for the function to be async.

Note: awaiting uploadMedia() was never useful anyway, because it never returned anything. It solely relies on the onFileChange and onError callbacks. The readme also doesn't suggest using await.

I'm curious to learn why your code doesn't work anymore, since there is no return value. It sounds like you might have just relied on the coincidence that onFileChange or onError is called on the next tick in the event loop as soon as the Promise settled, instead of relying on the callback functions.

A short term fix would be to make uploadMedia async again, but that won't help you in the future, where files will first be processed in the browser in a queue-like system before sending them to the server (see #61447). So I'd advise against that.

@elzadj elzadj closed this as completed Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Media Anything that impacts the experience of managing media Needs Technical Feedback Needs testing from a developer perspective. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants