Skip to content

Core data: postType/attachment and root/media entities duplicate one another #70815

Closed

Description

@talldan

Description

Discovered after #70405

Core data has two separate entities that represent the same document type / REST endpoint:

  • The postType attachment entity is dynamically created via loadPostTypeEntities
  • The root media entity is declared in the list of entities

Both use the wp/v2/media REST endpoint.

The problem with having two entity types is that they maintain separate caches for the same data. We might invalidate the cache for one entity via a call like saveEntityRecord, and the other entity then contains stale data that hasn't been invalidated. There's no control over which one plugins and code in the wider WordPress ecosystem uses, so we can't easily consider that best practice is to use one of the entities.

The best solution might be to deprecate one of the entities and implement some kind of redirect to the other entity to maintain back compat.

Step-by-step reproduction instructions

  • With the block editor open, using the browser console, run wp.data.select( 'core' ).getEntityRecords( 'postType', 'attachment' );
  • In the network tab, observe a request to wp/v2/media is triggered
  • Now run wp.data.select( 'core' ).getEntityRecords( 'root', 'media' );
  • Observe an identical request to wp/v2/media was made

Screenshots, screen recording, code snippet

No response

Environment info

No response

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

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. self-assigned this
    on Jul 21, 2025
  2. added
    [Type] Bug
    [Package] Core data
    on Jul 21, 2025
  3. talldan commented on Jul 21, 2025

    @talldan
    ContributorAuthor

    The best solution might be to deprecate one of the entities and implement some kind of redirect to the other entity to maintain back compat.

    To add, these entities aren't widely used. There's a small amount of usage of the media entity internally, none of attachment that I can see. The media entity has a small benefit that it treats caption and description as a raw field (meaning it's converted from an object to a string internally). Adding that handling to attachment is possible, but I think it would be a back compat break.

    I've done a brief exploration of how we might deprecate and there aren't many good options. My first thought was to wrap the selectors, actions, and resolvers to handle the deprecation. Something like that might still be the best option. It's a lot of code.

    I also considered using a proxy within the reducer to redirect any access to the entities, but I quickly learned that JS proxies have some pretty significant performance issues.

  4. Mamaduka commented on Jul 21, 2025

    @Mamaduka
    Member

    The postType attachment entity is dynamically created via loadPostTypeEntities

    I think this was an oversight on our side. The root|media should be the only core entity for the attachment post types.

  5. DarkMatter-999 commented on Jul 21, 2025

    @DarkMatter-999
    Contributor

    In that case, we can simply prevent the attachment Post type from registering in loadPostTypeEntities() as root/media takes care of that.

  6. added
    [Status] In Progress
    on Jul 21, 2025
  7. removed
    [Status] In Progress
    on Jul 21, 2025
  8. talldan commented on Jul 22, 2025

    @talldan
    ContributorAuthor

    In that case, we can simply prevent the attachment Post type from registering in loadPostTypeEntities() as root/media takes care of that.

    Plugins might already be using the attachment entity, so it's not as simple as removing it, that would break those plugins.

  9. talldan commented on Jul 22, 2025

    @talldan
    ContributorAuthor

    Found another option while exploring, which is to utilize the __unstableNormalizeArgs feature added by #52120. It works for selectors possibly also for resolvers (though I'd need to check it works when they're called via resolveSelect). I don't think it works for actions, so that would be needed.

    I think it's similar to wrapping the functions in that it adds quite a lot of extra code, but using an existing API feels like a better solution.

  10. added
    [Status] In Progress
    on Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Package] Core data[Status] In Progress[Type] Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions