Skip to content

apiFetch returns different types for preloaded, unparsed OPTIONS requests #37966

Closed

Description

@dlh01

Description

In #35527, the preloading middleware was updated to use preloaded OPTIONS requests only once, which matched a previous change for GET requests.

When the middleware returns preloaded data for a GET request with { parse: false }, it wraps the data in a Response object. But for OPTIONS requests, the middleware always returns a plain object, which means that preloaded OPTIONS requests return an object on the first call and a Response for all subsequent calls. As a developer, this inconsistency can affect me when:

  1. I attempt to preload an OPTIONS request, but some other code makes the same request first.
  2. Some other code preloads the OPTIONS request without my knowledge, but my code makes the request first.

At first glance, it seems to me that the expected behavior would be for the preloading middleware to return Response objects for unparsed OPTIONS requests like it does for GET requests.

Returning a Response would be a backwards compatibility break, but, on the other hand, #35527 itself is already a compatibility break (as was acknowledged at the time).

Step-by-step reproduction instructions

Run in the console:

wp.apiFetch.use(wp.apiFetch.createPreloadingMiddleware(
	{
		"OPTIONS": {
			"/someUrl": {
				'body': 'From middleware',
				'headers': {}
			}
		}
	}));

Then, run twice in the console:

wp.apiFetch( {
	path: "/someUrl",
	method: 'OPTIONS',
	parse: false,
} ).then( res  => {
	console.log( res );
} );

Observe that the first request resolves to { body: ..., headers: ... }, and the second request resolves to Response { type: ... }.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 5.9-RC2
  • Gutenberg plugin not installed
  • Chrome 96
  • macOS 10.14

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

Activity

  1. talldan commented on Jan 14, 2022

    @talldan
    Contributor
  2. added
    [Package] API fetch
    REST API Interaction
    on Jan 14, 2022
  3. anton-vlasenko commented on Jan 14, 2022

    @anton-vlasenko
    Contributor

    I will look into this. Thanks, @talldan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

REST API Interaction[Package] API fetch

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions