Skip to content

Guidelines: Read block types from REST instead of @wordpress/block-library - #79680

Closed
gziolo wants to merge 1 commit into
trunkfrom
try/guidelines-block-types-from-rest
Closed

gziolo wants to merge 1 commit into
trunkfrom
try/guidelines-block-types-from-rest

Conversation

@gziolo

@gziolo gziolo commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

What?

Part of #77230. Follow up to #79674.

This is a proof of concept, opened as a draft to illustrate an idea and gather feedback.

It changes the Guidelines screen so it reads the list of block types from the REST API (/wp/v2/block-types) through @wordpress/core-data, instead of loading @wordpress/block-library and calling registerCoreBlocks() only to enumerate blocks.

Why?

The Guidelines screen has a Blocks section where you set guidelines per block. To build that list today, the page loads the whole block library (every Core block's editor code) just to read each block's name, title, icon, and whether it can carry content. That is a large bundle to load for what is, in effect, a list of names.

The REST API already exposes the registered block types, so the page can read the list as data. This also lines up with the long term goal of supporting more than Core blocks, since the endpoint returns every server-registered block.

How?

  • Add a blockType core-data root entity for /wp/v2/block-types. The page already registers a custom entity for guideline scopes, so this follows the same pattern.
  • Rewrite useContentBlocks() to read that entity with useEntityRecords and keep only content blocks. A content block has an attribute with the content role, or supports contentRole. This mirrors the isContentBlock private API from @wordpress/blocks, but reads the REST shape (the server normalizes the legacy __experimentalRole to role).
  • Delete bootstrap-block-registry.ts and stop calling registerCoreBlocks().
  • Stop enqueuing the wp-block-library script on the page.
  • Drop @wordpress/block-library, @wordpress/blocks, and the lock-unlock helper from the route.

Trade-offs (pros and cons)

Pros

  • Removes @wordpress/block-library (the largest editor bundle) plus @wordpress/blocks and the lock-unlock helper from the page. It no longer parses and runs every Core block just to list names.
  • Removes the wp-block-library script enqueue from the PHP.
  • Simpler bootstrap. Deletes bootstrap-block-registry.ts and the reapplyBlockTypeFilters() / registerCoreBlocks() step.
  • One data model. Block types now load through useEntityRecords, the same way the page already loads scopes and rows.
  • Server-registered third-party blocks show up automatically. The old path only saw Core blocks.
  • No private API. The content-role check is a small local function instead of unlock( blocksPrivateApis ).isContentBlock.

Cons

  • Block icons regress. The endpoint returns icon: null for Core blocks, because their SVG icons live in the JavaScript bundle. So the Blocks list and picker show a generic block icon. This is the main user-visible cost. (A third-party block that sets a dashicon in block.json would still show that dashicon.)
  • Async plus one request. The list was synchronous from the in-memory registry. It now loads over REST with a loading state. This PR does not add server preloading yet, so there is one extra request on page load.
  • Payload vs bundle. We trade JavaScript for JSON. The response must include attributes (that is where the content role lives), so even with _fields it carries every block's attribute schema. It is still smaller than the block-library bundle, and it is data rather than code, but it is a per-load transfer.
  • Logic duplication. isContentBlockType re-implements the private isContentBlock. They could drift. A clean follow up would be to add an object-taking isContentBlockType( blockType ) export to @wordpress/blocks and reuse it.
  • Server-only view. REST reflects server registration. Blocks registered only on the client, or a content role added by a client blocks.registerBlockType filter, would not be seen. On a standalone settings page with no editor this is effectively a non-issue, but it is a semantic difference.
  • Capability. /wp/v2/block-types needs edit_posts. The page is manage_options today, so administrators are fine. It would matter only if the page capability were ever lowered.

Testing Instructions

  1. Enable the Guidelines experiment on the Gutenberg → Experiments screen.
  2. Run npm install && npm run build.
  3. Open Settings → Guidelines and scroll to the Blocks section.
  4. Confirm the list shows content blocks (for example Paragraph and Heading) and that you can add a guideline for a block.
  5. Expected trade-off: Core blocks show a generic icon rather than their own icon.
  6. In the browser Network tab, confirm the wp-block-library script is no longer loaded, and that a request to /wp/v2/block-types is made.

Testing Instructions for Keyboard

  1. Open Settings → Guidelines.
  2. Tab to the Blocks section and the Add block guidelines control.
  3. Confirm you can open the block picker, choose a block, type a guideline, and save, using the keyboard only.

Screenshots or screencast

The visible change is the block icon in the Blocks list: the real block icon before, a generic block icon after.

Screenshot 2026-06-30 at 12 21 16

Use of AI Tools

This pull request was prepared with the help of Claude Code (Anthropic). The AI assisted with the data layer change, the REST content-role check, removing the block-library dependency, and this description. All changes are reviewed by the author. This is a draft proof of concept and has not yet been built or verified in a running site.

@gziolo gziolo self-assigned this Jun 30, 2026
@gziolo
gziolo requested a review from aagam-shah June 30, 2026 10:16
@gziolo gziolo added [Type] Enhancement A suggestion for improvement. [Feature] Guidelines An experimental feature for adding site-wide editorial rules. [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible and removed [Type] Enhancement A suggestion for improvement. labels Jun 30, 2026
@gziolo gziolo mentioned this pull request Jun 30, 2026
1 of 9 tasks
@github-actions

Copy link
Copy Markdown

Size Change: 0 B

Total Size: 7.61 MB

compressed-size-action

…brary

Proof of concept. Read the list of block types from the core
/wp/v2/block-types endpoint through @wordpress/core-data, instead of loading
@wordpress/block-library and calling registerCoreBlocks() just to enumerate
the blocks that can carry guidelines.

- Add a `blockType` core-data root entity for /wp/v2/block-types (entity.ts).
- Rewrite useContentBlocks() to read that entity and filter content blocks
  from the REST shape (data.ts).
- Delete bootstrap-block-registry.ts and drop the registerCoreBlocks() call.
- Stop enqueuing the wp-block-library script on the page (load.php).
- Drop @wordpress/block-library, @wordpress/blocks, and the lock-unlock helper.

Trade-off: Core block icons are not in the REST response, so the Blocks list
falls back to a generic icon. See the pull request description for the full
pros and cons.

Part of #77230

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gziolo
gziolo force-pushed the try/guidelines-block-types-from-rest branch from 9fa9b23 to 445ddec Compare June 30, 2026 10:24
@github-actions

Copy link
Copy Markdown

Flaky tests detected in 445ddec.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/28437540244
📝 Reported issues:

@gziolo

gziolo commented Jul 1, 2026 •

Copy link
Copy Markdown
Member Author

@aagam-shah and @aaronjorbin, I have three observations based on this prototype:

  • The list of blocks gets filtered to include only those that contain at least one attribute annotated as role content.
  • While Block Library will show only core blocks, when using the REST API, it won't show custom blocks if they are registered only on the client.
  • When using the REST API, we won't be able to show block icons until we figure out how to convert names defined on the server as strings into icons defined in @wordpress/icons. Well, it still won't work with custom icons defined inline during block registration.

@gziolo

gziolo commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

When using the REST API, we won't be able to show block icons until we figure out how to convert names defined on the server as strings into icons defined in @wordpress/icons. Well, it still won't work with custom icons defined inline during block registration.

@t-hamano, I'm looking at #75715. So that could potentially be a solution to register SVG icons on the server and set them as block icons consumed on the client. Is there any effort to offer a string to SVG conversion similar to Dashicons? To give an example, for the Paragraph block we do the following:

import { paragraph as icon } from '@wordpress/icons';

Could we instead set paragraph as an icon in block.json and have it automatically resolved on the client to paragraph from @wordpress/icons?

For custom icons, you would use register_icon on the server to define my-plugin/my-block, and then you would also set it as the block's icon, which would get resolved on the client magically using the new SVG icon API.

@t-hamano

t-hamano commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Could we instead set "paragraph" as an icon in "block.json" and have it automatically resolved on the client to "paragraph" from "@wordpress/icons"?

I believe it is technically feasible. We could read the icon field from block.json on the server-side, call wp_get_icon( 'core/paragraph' ) based on its value, and include the resulting SVG string in the REST response.

However, we are intentionally excluding them to prevent icons unrelated to post content from appearing in the Icon block picker. We are considering an option to register icons in the icon registry without making them insertable or selectable. See #77260 (comment)

P.S. There is also a PR proposing to support file paths in the icon field of block.json. #79658

@gziolo gziolo added the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Jul 13, 2026
@gziolo

gziolo commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

This does not seem actionable at the moment.

@gziolo gziolo closed this Oct 8, 2026
@gziolo
gziolo deleted the try/guidelines-block-types-from-rest branch October 8, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Guidelines An experimental feature for adding site-wide editorial rules. [Status] Blocked Used to indicate that a current effort isn't able to move forward [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants