sybrew / the-seo-framework

The SEO Framework WordPress plugin.
https://theseoframework.com/
GNU General Public License v3.0
426 stars 47 forks source link

Clicking save in Gutenberg editor causes multiple admin-ajax.php calls #688

Open Ciantic opened 2 months ago

Ciantic commented 2 months ago

Describe the bug When clicking save in Gutenberg it triggers multiple POST requests

How to reproduce the bug Steps to reproduce the behavior:

  1. Go to WordPress page editor
  2. Open developer tools
  3. Click on save button
  4. See network tab and admin-ajax.php calls

On my machine it triggers multiple POST requests to the admin-ajax.php with this kind of payload:

On my test (4-10 extra POST requests)

{
    "success": true,
    "data": {
        "data": {
            "seobar": "
....<\/div>", "metadescription": "", "ogdescription": "", "twdescription": "", "imageurl": "" }, "processed": [ "seobar", "metadescription", "ogdescription", "twdescription", "imageurl" ] } }

Expected behavior Only one request to save the details

Additional context Google Chrome and latest WordPress.

I've yet to test with other sites.

sybrew commented 2 months ago

Thank you for the detailed bug report!

The admin-ajax.php calls are for a feature where TSF fetches updated metadata, a new SEO Bar, etc., after saving.

Gutenberg doesn't handle its saving subscription well. Hence, there are multiple calls for it in rapid succession.

We have already debounced most of the duplicate calls. This debouncing filters out duplicated calls by delaying our callbacks for 0.5 seconds. If 0.5 seconds haven't passed, and another save action occurs, we wait for another 0.5 seconds. Once this 0.5 is passed, TSF performs its on-save actions, such as making the admin-ajax.php calls.

If the browser or server responds too slowly and the saving calls are more than 0.5 seconds apart, TSF will indeed repeat its saving sequence.

I won't increase the debouncing delay of 0.5 seconds because the lag with the user interface would become too significant. We also cannot accurately determine whether a user saved twice in succession or that Gutenberg is still unworkable.

This must be fixed upstream. Gutenberg's team already tried to address this a couple of times in the past, but I'm sure there's more work to be done:

  1. https://github.com/WordPress/gutenberg/issues/14759
  2. https://github.com/WordPress/gutenberg/pull/17522

Still, please let me know if you find that the admin-ajax.php calls repeat in under 0.5 seconds (500 milliseconds) because that would be a bug in The SEO Framework.

With that, I recently reworked TSF's debouncers to be native JS, which will ship with the next update. The change also affects this part of the saving sequence, although I do not think you'll find an improvement in this situation. But it did get me up to speed with this issue.