Gutenberg: API Fetch fetch-all middleware can not be prefetched.

Created on 3 Jul 2020  路  22Comments  路  Source: WordPress/gutenberg

The fetch-all middleware, loads all pages of an API request, if -1 is passed. To do this, it uses, parse: false to get headers and total page headers. However requested that are parse: false are not prefetched. See this line. It would be amazing if this change, so the something like users, could be prefeteched.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

[Package] API fetch [Status] In Progress [Type] Enhancement

All 22 comments

Previously #22510. cc @epiqueras.

Didn't that PR fix it?

What happens if we just change parse to true there?

What happens if we just change parse to true there?

The reason parse: false, is sent here, as to get the next page, you need to get the headers for the request that contain the next page link ( using a function called getNextPageUrl ). See this example.
Screenshot 2020-07-07 at 17 29 17
See link field above ^

Using enveloping would simple fix this.
Adding ?_envelope=1 to a request, means that headers, status and body are returned in the body of the request. Take this example.

Screenshot 2020-07-07 at 17 33 11

Meaning parse: true could be set and still get the next page url from the body of the request, instead of the header.

Got it, that makes sense.

@epiqueras Are you interesting working on a PR for this?

I can see other ways to make this work without making an envelope option. What are the third party use cases for envelope?

The main use case, AFAIK, for envelope is for scenarios where the user cannot access the headers of a response. If we can make preloading work with parse: false I think that would be ideal.

When would they need the headers to be preloaded like that?

For stock core, the pagination headers themselves. So you can preload how many items are in a list. In more custom use cases, the defacto way to add information to a collection response in the REST API is via headers.

Got it. So making preloading work with parse: false would remove the need for an envelope parameter.

Yep!

@TimothyBJacobs @spacedmonkey

I'm trying to implement this, but rest_preload_api_request is not returning headers as expected:

Screen Shot 2020-07-08 at 12 17 27 PM

That looks right to me. Not all of the responses have additional headers, the types and taxonomies endpoints don't support pagination so they don't add any.

Were there other headers you were expecting?

Seem right to be also. Not all requests have headers. Like type, but I can see headers in wp/v2/themes for example.

The Fetch All Middleware needs the Link header.

The Fetch All Middleware needs the Link header.

Not all the requests are using Fetch All Middleware anyway. Types don't need those headers, are they are not paginated. Also, these requests would not be return headers currently.

Hm, I'm seeing the Link header in my tests.

rest_preload_api_request([], '/wp/v2/posts');
=> phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/Shell/REPL.php:52:
array(1) {
  '/wp/v2/posts' =>
  array(2) {
    'body' =>
    array(10) {
      [0] =>
      array(24) {
        ...
      }
      [1] =>
      array(24) {
        ...
      }
      [2] =>
      array(24) {
        ...
      }
      [3] =>
      array(24) {
        ...
      }
      [4] =>
      array(24) {
        ...
      }
      [5] =>
      array(24) {
        ...
      }
      [6] =>
      array(24) {
        ...
      }
      [7] =>
      array(24) {
        ...
      }
      [8] =>
      array(24) {
        ...
      }
      [9] =>
      array(24) {
        ...
      }
    }
    'headers' =>
    array(3) {
      'X-WP-Total' =>
      int(269)
      'X-WP-TotalPages' =>
      int(27)
      'Link' =>
      string(58) "<http://trunk.test/wp-json/wp/v2/posts?page=2>; rel="next""
    }
  }
}

Not all the requests are using Fetch All Middleware anyway. Types don't need those headers, are they are not paginated. Also, these requests would not be return headers currently.

This issue is about supporting preloading for requests made by the Fetch All Middleware. To do this, we need the Link headers.

Hm, I'm seeing the Link header in my tests.

Try logging the preloaded data the editor is bootstrapped with.

There isn't a collection route in edit-form-blocks.php so I added one. It looks right to me:

image

Do you have less than one page of pages?

Do you have less than one page of pages?

That was it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franz-josef-kaiser picture franz-josef-kaiser  路  3Comments

jasmussen picture jasmussen  路  3Comments

youknowriad picture youknowriad  路  3Comments

jasmussen picture jasmussen  路  3Comments

BE-Webdesign picture BE-Webdesign  路  3Comments