Skip to content

Localize schema for wp api client#2057

Merged
aduth merged 2 commits intomasterfrom
add/api-localize-schema
Aug 1, 2017
Merged

Localize schema for wp api client#2057
aduth merged 2 commits intomasterfrom
add/api-localize-schema

Conversation

@aduth
Copy link
Copy Markdown
Member

@aduth aduth commented Jul 27, 2017

Supersedes #1498
Fixes #1298

This pull request seeks to bootstrap the REST API schema to prevent an unusable editor screen from being shown while the schema is fetched from the REST API.

Implementation notes:

Ideally one could hook into and append data to the default set (schema, cacheSchema), but to my knowledge the script loader provides no such opportunities.

https://github.com/WordPress/WordPress/blob/89fa297/wp-includes/script-loader.php#L510-L514
https://github.com/WordPress/WordPress/blob/89fa297/wp-includes/class.wp-scripts.php#L403-L440

It may be the case that we want to revise the above core logic to be filterable, or inject the schema by default.

Testing instructions:

Verify in your browser's Developer Tools Network tab that no request is issued for the /wp/v2 root API route when navigating to the Gutenberg editor.

@aduth aduth requested a review from adamsilverstein July 27, 2017 18:59
@westonruter
Copy link
Copy Markdown
Member

@aduth you can amend to the existing wpApiSettings object by using wp_add_inline_script() like so:

wp_add_inline_script( 'wp-api', sprintf( 
    'wpApiSettings.cacheSchema = true; wpApiSettings.schema = %s;',
    wp_json_encode( $schema_response->get_data() ) 
) );

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Jul 28, 2017

@westonruter Thanks, that works, updated in aa093eb. Not so nice to inject hand-written property setting like that, but feels a bit less error-prone overall.

@westonruter
Copy link
Copy Markdown
Member

@aduth as part of this, should this PR also include the revert of 7780764?

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Jul 31, 2017

@aduth as part of this, should this PR also include the revert of 7780764?

No, I don't think so. Remarked also in original comment, but it can serve as a fallback behavior if the server-side bootstrapping fails. Since the callback will shortcut itself if a schema is already present.

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Aug 1, 2017

Since the callback will shortcut itself if a schema is already present.

Seems the lack of network request could be attributed to session storage of the schema, not that it was picking up the schema from the API settings 😞 Certainly seems like an improvement which could be made to the client.

@aduth aduth force-pushed the add/api-localize-schema branch from aa093eb to 9cd6d77 Compare August 1, 2017 19:39
@aduth
Copy link
Copy Markdown
Member Author

aduth commented Aug 1, 2017

Actually, it does detect the schema. The issue was ordering: The default of wp_add_inline_script is to include the snippet after the script. At that point the initialization has already run, and since we've not set the global at that point, assumes the schema is not preloaded. Simply adding the 'before' parameter is enough to ensure that the settings property is assigned during initialization.

One worry is whether window.wpApiSettings could be undefined at the time we start setting properties on it. We might consider at least adding some checks here, but given that the wp-api handle is localized immediately upon being registered, I don't think we need to worry about this ordering.

@codecov codecov bot deleted a comment from codecov-io Aug 1, 2017
@aduth aduth merged commit 85ba855 into master Aug 1, 2017
@aduth aduth deleted the add/api-localize-schema branch August 1, 2017 20:16
@nylen
Copy link
Copy Markdown
Member

nylen commented Aug 1, 2017

Seems like a reasonable solution for now, but this call is much slower than expected and we should seek to get rid of it as soon as possible. See https://core.trac.wordpress.org/ticket/40988 for reference.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants