WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#37487 closed defect (bug) (duplicate)

Use Beacon API since Synchronous XMLHttpRequests on the main thread are deprecated

Reported by: iseulde Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: javascript Cc:

Description

jQuery ajax calls with async: false trigger the following warning:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

To see this warning you must preserve the log.

The waring refers to the following section:

Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user's experience. (This is a long process that takes many years.) Developers must not pass false for the async argument when entry settings object's global object is a Window object. User agents are strongly encouraged to warn about such usage in developer tools and may experiment with throwing an InvalidAccessError exception when it occurs.

At the moment the only use in core is for removing the post lock when the user navigates away from a post page. As it is mentioned in an article on the MDN, the Beacon API should be used for requests like this.

There are some cases in which the synchronous usage of XMLHttpRequest was not replaceable, like during the window.onunload and window.onbeforeunload events. The navigator.sendBeacon API can support these usecases typically while delivering a good UX.

[...]

Using the sendBeacon() method, the data will be transmitted asynchronously to the web server when the User Agent has had an opportunity to do so, without delaying the unload or affecting the performance of the next navigation.

Specification: https://www.w3.org/TR/beacon/.
More information: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon.

According to the MDN and caniuse.com, Chrome, Firefox, Opera and Android support it, Safari and Internet Explorer do not, but support in Edge is on its way.

So with fairly good support, I think we should use this API and fall back to a synchronous XMLHttpRequest. In another ticket we could also take a look to use this for autosaving posts.

Attachments (1)

37487.patch (953 bytes) - added by iseulde 4 years ago.

Download all attachments as: .zip

Change History (5)

@iseulde
4 years ago

#1 @iseulde
4 years ago

  • Keywords has-patch added

#2 @ocean90
4 years ago

Duplicate of #33349?

#3 @iseulde
4 years ago

@ocean90 Yep, did a search for "beacon", but not "Synchronous XMLHttpRequest"...

#4 @iseulde
4 years ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #33349.

Note: See TracTickets for help on using tickets.