Skip to content

Table of Contents: Reuse server rendering for the editor preview - #80796

Draft
getdave wants to merge 1 commit into
trunkfrom
toc-server-side-render
Draft

getdave wants to merge 1 commit into
trunkfrom
toc-server-side-render

Conversation

@getdave

@getdave getdave commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

What

Related to #80794.

Convert the Table of Contents editor preview to reuse the existing server-side block renderer. When post context is available, the editor renders the heading list produced by the same rendering path that readers receive on the front of the site.

Why

The editor previously built a client-side approximation of the heading list, while the front end resolves headings from rendered post content. This evaluates whether server-side rendering can avoid a dedicated REST endpoint while giving authors a preview based on the reader-facing rendering path.

Key reviewer question: persisted preview versus live editing

This approach does not produce a live Table of Contents preview from unsaved editor state. It shows the most recently persisted heading list.

  • New, unsaved post: No post ID exists, so the server cannot load post content. No Table of Contents preview is shown until the first save.
  • Saved draft: After an author changes headings, the preview initially remains stale. A WordPress autosave may persist a draft as a regular save, in which case the preview refreshes after that autosave. Saving the draft refreshes it reliably.
  • Published post: Autosaves are normally revisions, while this preview reads the canonical post. The preview can therefore remain stale until the author explicitly clicks Update.

Are we comfortable with that author experience? If not, SSR alone is insufficient and we should consider a client-side fallback, a hybrid, or the dedicated endpoint proposed in #80786.

How

  • Request the preview through useServerSideRender, passing the edited post ID when available.
  • Allow the existing dynamic renderer to resolve headings for REST previews as well as front-end content rendering.
  • Render the returned HTML safely in the editor while preserving editor controls.
  • Refresh after persisted post changes and cover the saved and no-post-ID states with user-focused e2e tests.

Testing Instructions

These scenarios help assess whether a persisted preview is acceptable while an author edits.

First save of a new post

  1. Create a new post.
  2. Insert a Table of Contents block and a Heading block.
  3. Before saving, observe that no Table of Contents preview is shown.
  4. Save a draft and confirm that the preview appears with the heading.

Saved draft: changes before and after autosave

  1. Add a second Heading block to the saved draft without manually saving.
  2. Confirm that the preview still shows the last saved heading list.
  3. Wait for WordPress to autosave, then observe whether the preview refreshes.
  4. Save the draft manually and confirm that the preview reflects the new heading.

Published post: changes before and after autosave

  1. Publish the post and confirm its preview contains the saved headings.
  2. Add or rename a heading without clicking Update.
  3. Wait for WordPress to autosave and confirm that the preview can still show the previous heading list.
  4. Click Update and confirm that the preview reflects the changed headings.

Automated coverage

npm run test:e2e -- test/e2e/specs/editor/blocks/table-of-contents.spec.js --project=chromium

@getdave getdave self-assigned this Jul 28, 2026
@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Jul 28, 2026
@getdave

This comment was marked as outdated.

@github-actions

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Size Change: +12 B (0%)

Total Size: 7.76 MB

📦 View Changed
Filename Size Change
build/scripts/block-library/index.min.js 356 kB +12 B (0%)

compressed-size-action

@getdave

getdave commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@t-hamano @Mamaduka I tested this approach and the behaviour which you can see below

Screen.Recording.on.2026-07-29.at.10-36-09.mp4

The problems I see are

  • the headings don't update in response to changes to headings within the Editor
  • there is an annoying debounced refresh on the block which can interrupt and disrupt typing

The key problem though is clearly the former. I don't see it as being acceptable UX for a block like this.

I'm not aware of any way around this using the current SSR approach but as it sounds like you've had more experience with this you might be aware of alternatives. If not then I don't see this approach as viable.

Looking forward to your feedback.

@Mamaduka

Copy link
Copy Markdown
Member

the headings don't update in response to changes to headings within the Editor

Right. The server only has data from persisted post content, so it can only update after the post is saved.

Not sure if a custom endpoint will help here; cons:

  • It would require us to send the entire content to every (probably debounced) keystore.
  • Refreshing ToC content won't be immediate and slow, depending on the connection.
  • Might suffer similar layout shifts and interrupt typing.

Maybe we should keep Editor preview handling on the client-side; it would require some refactoring, but it would be responsive and probably make it easier to keep the "Convert to static list" option. cons:

  • Need to maintain a separate rendering handling. Not a big problem, IMO.
  • Extensibility can get tricky. Need to account for both the server and client sides.

@getdave
getdave force-pushed the toc-server-side-render branch from a18ad78 to ad2c4f2 Compare September 11, 2026 12:20
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

📦 Bundle size

Size Change: -131 B (0%)

Total Size: 8.08 MB

📦 View Changed
Filename Size Change
build/scripts/block-library/index.min.js 364 kB -131 B (-0.04%)

ad2c4f2 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric b63f27a trunk % Change
timeToFirstByte 49 ms +9.29% -2.96% 49.65 ms +4.03% -1.91% -1.31%
largestContentfulPaint 86 ms +4.65% -6.98% 84 ms +9.52% -4.76% 2.38%
lcpMinusTtfb 33.25 ms +14.14% -2.86% 31.3 ms +27.8% -3.04% 6.23%
wpBeforeTemplate 23.08 ms +8.88% -2.12% 23.44 ms +6.06% -1.71% -1.54%
wpTemplate 22.05 ms +4.04% -5.85% 22.48 ms +4.67% -2.49% -1.91%
wpTotal 45.3 ms +8.32% -2.83% 45.94 ms +4.01% -1.7% -1.39%
wpMemoryUsage 7.52 MB +0% -0% 7.49 MB +0% -0% 0.46%
wpDbQueries 17 +0% -0% 17 +0% -0% 0%

front-end-classic-theme

Metric b63f27a trunk % Change
timeToFirstByte 36 ms +2.64% -3.06% 40.75 ms +2.82% -3.19% -11.66%
largestContentfulPaint 80 ms +5% -0% 92 ms +0% -4.35% -13.04%
lcpMinusTtfb 45.15 ms +1% -1.88% 49.9 ms +3.41% -1.3% -9.52%
wpBeforeTemplate 20.45 ms +3.86% -3.03% 20.25 ms +5.28% -0.54% 0.99%
wpTemplate 12.31 ms +1.95% -1.62% 17.06 ms +3.4% -6.57% -27.84%
wpTotal 33.08 ms +3.69% -3.2% 37.71 ms +3.13% -3.13% -12.28%
wpMemoryUsage 6.00 MB +0% -0% 6.10 MB +0% -0% -1.67%
wpDbQueries 10 +0% -0% 14 +0% -0% -28.57%

media-processing

Metric b63f27a trunk % Change
mediaProcessingJpeg 379.31 ms +0.38% -0.79% 380.38 ms +0.8% -0.73% -0.28%
mediaProcessingAvif 5598.6 ms +0.15% -0.26% 5611.79 ms +0.28% -0.35% -0.24%
mediaProcessingJpegToAvif 3831.04 ms +0.23% -0.1% 3826.12 ms +0.08% -0.1% 0.13%

media-upload

Metric b63f27a trunk % Change
jpegUploadProcessing 1445.71 ms +34.49% -1.65% 1409.7 ms +0.67% -0.1% 2.55%
pngUploadProcessing 200.08 ms +6.35% -2.95% 202.84 ms +3.36% -6.05% -1.36%
largeJpegUploadProcessing 1408.4 ms +0.36% -0.23% 1415.51 ms +1.15% -1.23% -0.5%
multipleImageUploadProcessing 1517.95 ms +1.86% -1.1% 1539.69 ms +0.38% -2.93% -1.41%

post-editor

Metric b63f27a trunk % Change
serverResponse 332.05 ms +2.19% -8.36% 324.62 ms +2.6% -8.45% 2.29%
firstPaint 231.95 ms +9.92% -24.27% 191.9 ms +26.07% -13.03% 20.87%
domContentLoaded 814.46 ms +1.44% -5.06% 799.13 ms +0.87% -3.69% 1.92%
loaded 815.43 ms +1.43% -5.06% 800.19 ms +0.87% -3.7% 1.9%
firstContentfulPaint 347.99 ms +3.99% -9.09% 334.63 ms +2.91% -4.69% 3.99%
firstBlock 2394.17 ms +1.42% -0.79% 2396.88 ms +0.49% -0.98% -0.11%
type 11.3 ms +5.58% -6.02% 11.51 ms +6.26% -3.48% -1.82%
typeWithoutInspector 11.91 ms +1.93% -9.91% 11.89 ms +2.69% -5.97% 0.17%
typeWithTopToolbar 13.62 ms +11.6% -6.75% 13.37 ms +9.27% -2.17% 1.87%
typeContainer 6.81 ms +8.52% -5.73% 7.12 ms +8.99% -4.35% -4.35%
focus 58.34 ms +7.58% -6.21% 57.11 ms +1.58% -3.82% 2.15%
firstFocus 130.31 ms +0% -0% 139.4 ms +0% -0% -6.52%
selectAll 439.84 ms +1.56% -1.73% 418.66 ms +3.96% -1.82% 5.06%
listViewOpen 49.64 ms +4.83% -9.91% 47.67 ms +2.27% -5.83% 4.13%
inserterOpen 18.45 ms +17.18% -10.73% 19.35 ms +8.63% -5.43% -4.65%
inserterHover 2.88 ms +10.76% -7.64% 3.15 ms +4.76% -8.57% -8.57%
inserterSearch 6.62 ms +3.78% -10.27% 7.31 ms +3.56% -6.29% -9.44%
loadPatterns 502.73 ms +1.15% -4.42% 527.76 ms +3.58% -6.73% -4.74%
wpTotal 322.72 ms +2.05% -8.37% 315.86 ms +2.65% -8.7% 2.17%
wpMemoryUsage 13.05 MB +0% -0% 13.02 MB +0% -0% 0.25%
wpDbQueries 54 +0% -0% 54 +0% -0% 0%

site-editor

Metric b63f27a trunk % Change
serverResponse 309.47 ms +3.59% -3.54% 316.84 ms +2.44% -9.03% -2.33%
firstPaint 190.42 ms +13.74% -11.12% 181.21 ms +10.32% -17.07% 5.08%
domContentLoaded 865.02 ms +0.9% -2.18% 865.98 ms +1.41% -0.5% -0.11%
loaded 865.98 ms +0.9% -2.18% 867.08 ms +1.41% -0.5% -0.13%
firstContentfulPaint 343.19 ms +3.93% -1.94% 349.77 ms +0.96% -1.22% -1.88%
firstBlock 3095.85 ms +1.08% -0.32% 3131.63 ms +0.59% -0.53% -1.14%
type 12.57 ms +3.34% -4.06% 13.03 ms +5.37% -4.14% -3.53%
navigate 94.1 ms +17.33% -7.28% 109.82 ms +9.17% -20.1% -14.31%
loadPatterns 978.91 ms +6.4% -9.53% 1039.98 ms +3.62% -16.61% -5.87%
loadPages 1038.91 ms +1.35% -1.52% 1069.37 ms +0.94% -1.28% -2.85%
wpTotal 301.27 ms +3.59% -3.67% 308.45 ms +2.39% -9.4% -2.33%
wpMemoryUsage 12.01 MB +0% -0% 11.97 MB +0% -0% 0.3%
wpDbQueries 43.5 +1.15% -1.15% 44 +0% -2.27% -1.14%

ad2c4f2 Run

🏁 Flaky tests

Show the failures

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.

Navigates the items list via UP/DOWN arrow keys in /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByLabel('Page Two')
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByLabel('Page Two') with timeout 5000ms
  - waiting for getByLabel('Page Two')
    14 × locator resolved to <button type="button" tabindex="-1" aria-pressed="false" id="view-list-0-317-item-wrapper" class="dataviews-view-list__item" aria-labelledby="view-list-0-317-label" aria-describedby="view-list-0-317-description"></button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:146:49

ad2c4f2 Run

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 library /packages/block-library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants