Skip to content

Block List: Render the ancestors of the selected block synchronously - #87910

Draft
draganescu wants to merge 5 commits into
trunkfrom
fix/undo-selection-ancestor-async-render
Draft

draganescu wants to merge 5 commits into
trunkfrom
fix/undo-selection-ancestor-async-render

Conversation

@draganescu

Copy link
Copy Markdown
Contributor

What?

Renders the ancestors of the selected block synchronously, like the selected block itself. This fixes an undo that a Template Part (or any other inner block controller) misses when it holds the caret.

Found while working on #87861, and independent of it: it reproduces with trunk's code.

Why?

With two Template Parts on different entities, type in the first, click into the second and type there, then undo right away. The first undo does nothing on the canvas, the second reverts the first part's typing, and the second part's typing is never reverted.

The undo history is not at fault: core-data records one level per part, and the first undo does clear the second part's edits. The canvas just never shows it:

  • A block that is neither visible nor selected is rendered in async mode, so it re-renders on store changes only when the browser is idle. A Template Part whose paragraph holds the caret is not itself selected. Until block visibility is first reported (a debounced IntersectionObserver, about 400 ms after the blocks mount) or while the part is out of view, it is async.
  • useBlockSync resets a controller's inner blocks only when the entity's blocks change between two of its renders. When the Template Part has not yet rendered the edits it sent out, its last render still holds the blocks parsed from the saved content. The undo brings the entity back to exactly that array, so the effect sees no change, and the typed text stays on the canvas.

How?

BlockListItems already keeps the selected block out of async mode. It now does the same for the selected block's ancestors, which are the blocks that hold the selection. That is a handful of blocks at most, so the extra synchronous rendering is small.

Testing Instructions

The new e2e test, undo across containers of different entities, in test/e2e/specs/site-editor/undo.spec.js, types in two Template Parts on different entities and undoes them in order. It pins the race: it marks the second part as off the canvas viewport and holds the browser's idle callbacks while typing and undoing. It fails on trunk every time (6/6 runs) and passes with the fix (12/12).

npm run test:e2e -- test/e2e/specs/site-editor/undo.spec.js

To try it by hand: in the site editor, add two Template Parts on different slugs to a template, type in the first, click into the second and type, then press Ctrl+Z (Cmd+Z) right away, before the parts have been on screen for half a second. Each undo should revert the typing in the opposite order.

There is no core-data or undo-manager unit test, since neither changes.

site-editor/template-part.spec.js › "doesn't throw a block error when clicking on a link" also fails locally on trunk: the test types the URL before the link input has focus. That is unrelated to this change.

🤖 Generated with Claude Code

draganescu and others added 2 commits December 3, 2026 16:54
A block that is neither visible nor selected renders in async mode, and
catches up with store changes only when the browser is idle. Until block
visibility is first reported, or when a container is out of view, that
includes a container holding the caret, such as a Template Part.

Such a container syncs its controlled inner blocks with an entity, and
`useBlockSync` resets the inner blocks only when the entity's blocks
change between two of its renders. When the container has not rendered
the edits it sent out, an undo that brings the entity back to what it
last rendered goes unnoticed: the typing stays on the canvas, and the
next undo reverts the previous level instead.

Treat the ancestors of the selected block like the selected block, so
they render each change synchronously.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

📦 Bundle size

Size Change: +36 B (0%)

Total Size: 8.86 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 563 kB +36 B (+0.01%)

6d9e202 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 1c8bbaa trunk % Change
timeToFirstByte 57.9 ms +7.94% -1.99% 58.45 ms +2.05% -3.68% -0.94%
largestContentfulPaint 88 ms +9.09% -2.27% 88 ms +2.27% -4.55% 0%
lcpMinusTtfb 30.1 ms +13.79% -1.99% 29.1 ms +9.11% -8.08% 3.44%
wpBeforeTemplate 30.39 ms +8.03% -1.35% 30.25 ms +9.02% -2.51% 0.46%
wpTemplate 23.36 ms +1.2% -4.62% 23.55 ms +1.61% -7.18% -0.81%
wpTotal 53.94 ms +8.03% -1.61% 54.46 ms +2.46% -3.73% -0.95%
wpMemoryUsage 7.76 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 1c8bbaa trunk % Change
timeToFirstByte 48.6 ms +4.63% -2.98% 47.95 ms +4.9% -0.94% 1.36%
largestContentfulPaint 86 ms +2.33% -2.33% 84 ms +0% -4.76% 2.38%
lcpMinusTtfb 36.45 ms +3.43% -3.02% 34.2 ms +6.29% -5.41% 6.58%
wpBeforeTemplate 27.72 ms +6.53% -1.73% 27.4 ms +7.3% -0.95% 1.17%
wpTemplate 17.09 ms +2.28% -3.34% 16.91 ms +2.54% -4.97% 1.06%
wpTotal 45.22 ms +4% -2.79% 44.69 ms +4.92% -1.3% 1.19%
wpMemoryUsage 6.47 MB +0% -0% 6.43 MB +0% -0% 0.55%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 1c8bbaa trunk % Change
mediaProcessingJpeg 398.71 ms +1.36% -0.03% 403.11 ms +0.71% -1.54% -1.09%
mediaProcessingAvif 6161.7 ms +0.16% -0.12% 6129.28 ms +0.25% -0.43% 0.53%
mediaProcessingJpegToAvif 4261.17 ms +0.32% -0.18% 4221.04 ms +0.26% -0.4% 0.95%

media-upload

Metric 1c8bbaa trunk % Change
jpegUploadProcessing 1514.74 ms +33.74% -0.81% 1492.06 ms +3.34% -0.73% 1.52%
pngUploadProcessing 277.18 ms +3.3% -4.22% 274.87 ms +4.29% -1.94% 0.84%
largeJpegUploadProcessing 1511.59 ms +28.22% -1.49% 1502.52 ms +29.12% -1.71% 0.6%
multipleImageUploadProcessing 2086.77 ms +0.34% -7.5% 2094.97 ms +0.96% -2.65% -0.39%

post-editor

Metric 1c8bbaa trunk % Change
serverResponse 481.29 ms +1.98% -9.59% 458.5 ms +5.33% -4.85% 4.97%
firstPaint 388.68 ms +12.3% -1.52% 391.55 ms +10.7% -4.84% -0.73%
domContentLoaded 826.36 ms +0.46% -1.85% 825.16 ms +2.02% -0.69% 0.15%
loaded 827.64 ms +0.47% -1.84% 826.72 ms +1.99% -0.71% 0.11%
firstContentfulPaint 460.95 ms +1.81% -0.71% 457.15 ms +2% -3.38% 0.83%
firstBlock 2227.64 ms +0.93% -1.21% 2270.38 ms +1.39% -0.99% -1.88%
type 17.28 ms +5.32% -8.04% 17.11 ms +13.56% -8.77% 0.99%
typeWithoutInspector 17.56 ms +8.31% -6.61% 19.12 ms +4.08% -10.83% -8.16%
typeWithTopToolbar 25.19 ms +8.06% -5.24% 24.83 ms +6.56% -7.53% 1.45%
typeContainer 9.41 ms +1.28% -3.93% 9.8 ms +1.12% -20.82% -3.98%
focus 103.16 ms +8.39% -4.62% 99.8 ms +5.98% -6.45% 3.37%
firstFocus 271.96 ms +0% -0% 274.12 ms +0% -0% -0.79%
selectAll 414.35 ms +0.61% -0.48% 394.84 ms +2.3% -1.74% 4.94%
undo 30.31 ms +8.55% -3.37% 28.88 ms +2.56% -2.67% 4.95%
listViewOpen 40.17 ms +11.08% -6.15% 40.1 ms +7.51% -4.94% 0.17%
inserterOpen 44.36 ms +9.72% -8.12% 41.14 ms +12.13% -3.69% 7.83%
inserterHover 11.49 ms +7.83% -10.01% 11.91 ms +4.95% -8.06% -3.53%
inserterSearch 9.71 ms +3.4% -10.09% 9.03 ms +9.86% -4.76% 7.53%
loadPatterns 670.58 ms +2.35% -4.94% 673.79 ms +4.37% -2.65% -0.48%
wpTotal 471.6 ms +1.73% -9.78% 448.57 ms +5.51% -4.92% 5.13%
wpMemoryUsage 14.86 MB +0% -0% 14.82 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 1c8bbaa trunk % Change
serverResponse 467.07 ms +2.15% -7.44% 466.41 ms +0.61% -3.67% 0.14%
firstPaint 440.83 ms +4.14% -14.38% 408.47 ms +11.65% -6.59% 7.92%
domContentLoaded 835.54 ms +3.33% -1.44% 822.04 ms +0.22% -1.32% 1.64%
loaded 836.77 ms +3.32% -1.42% 823.34 ms +0.22% -1.31% 1.63%
firstContentfulPaint 466.29 ms +7.64% -2.13% 455.42 ms +2.07% -1.85% 2.39%
firstBlock 2597.16 ms +1.95% -1.83% 2548.15 ms +1.07% -0.36% 1.92%
type 16.72 ms +10.53% -4.01% 18.24 ms +4.71% -4.11% -8.33%
navigate 106.93 ms +2.74% -1.12% 103.31 ms +6.09% -3.06% 3.5%
loadPatterns 1605.9 ms +7.65% -2.18% 1466.45 ms +4.1% -11.98% 9.51%
loadPages 941.43 ms +4.47% -2.41% 944.24 ms +3.49% -1.31% -0.3%
wpTotal 457.73 ms +2.01% -7.59% 456.91 ms +0.65% -3.7% 0.18%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

6d9e202 Run

🏁 Flaky tests

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.

allows widgets to be moved between widget areas using the dropdown in the block toolbar in /test/e2e/specs/widgets/editing-widgets.spec.js, passed after 1 failed attempt.

should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":24,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8891/wp-content/uploads/2026/12/433c3c7d-8459-4888-9439-c4eb438e526b.png" alt="" class="wp-image-24"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49

6d9e202 Run

…ncestor-async-render

# Conflicts:
#	packages/block-editor/CHANGELOG.md
#	test/e2e/specs/site-editor/undo.spec.js
@draganescu draganescu added the [Type] Bug An existing feature does not function as intended label Dec 3, 2026

This branch has not been deployed

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

Labels

[Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant