404 in gutenberg autosave of a Custom Post Type with custom rest_namespace

I'm having some troubles with CPTs defined with custom rest_namespace at register_post_type. I keep getting an 404 error at the console. The error shows that the namespace is not being changed in autosave route.

WP default namespace: wp/v2

Custom namespace defined: ek/v1

I dug into the core classes to find how Wordpress handle custom namespace.

The WP_REST_Autosaves_Controler define the namespace using get_post_type_object at the __construct method:

...
$post_type_object = get_post_type_object( $parent_post_type );
....
$this-namespace = ! empty( $post_type_object-rest_namespace ) ? $post_type_object-rest_namespace : 'wp/v2';
...

If I do a discovery request at the ek/v1 I can see the endpoints registered correctly:

/ek/v1/releases/(?Pparent[\\d]+)/autosaves/(?Pid[\\d]+)

But in the console this error pops up:

GET http://localhost:8888/wp-json/wp/v2/releases/70/autosaves?context=edit_locale=user 404 (Not Found)

So, why the default namespace persist if I did all right? Would it be some javascript function of wordpress that would be doing the request on the incorrect route? What function? How to fix it?

Topic block-editor autosave rest-api Wordpress

Category Web


So, why the default namespace persist if I did all right? Would it be some javascript function of wordpress that would be doing the request on the incorrect route? What function? How to fix it?

If the endpoint works then you changed it correctly, but if you've flushed rewrite rules/permalinks, and it still isn't working, then you need to raise a bug with the Gutenberg github, or the official WP Trac.

Note that this is assuming you're only trying to change the REST API endpoint of a single custom post type, and doing it via register_post_type and the rest_namespace parameter with WP 5.9/6 or higher.

Otherwise, it looks based on this code that you have found a WordPress/Gutenberg bug with retrieving autosaves:

https://github.com/WordPress/gutenberg/blob/c0df23894233e851f3fe734358831ad560a18448/packages/core-data/src/resolvers.js#L332

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.