Skip to content

Table of contents block – links don't work on initial page save #43595

Closed

Description

@Greatdane

Description

If you add the Table of Contents block to a new page, when you publish the page, the links don't work properly and instead open a new page with a Page not found error. The URL is retaining an auto-draft in it. If you edit and Update the page, the links will then work properly. Alternatively, if you publish the page first, then add the Table of Contents block, they will work correctly.

Step-by-step reproduction instructions

Create a new page
Add content, with headings, and the Table of Content block
Publish the page
View the page
Try the TOC links
Return to the editor and make a change
Update the published page
View the page
The TOC links now work

Screenshots, screen recording, code snippet

Screen.Capture.on.2022-08-20.at.10-40-21.mp4

Environment info

WordPress version 6.0.1
Gutenberg 13.9.0

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

Activity

  1. aatanasovdev commented on Aug 26, 2022

    @aatanasovdev
    Contributor

    I was able to reproduce this issue following the steps.

    The tricky part is that the public URL is generated after the publish action.

    When a page is added, the links of the headings are set correctly to the block's store without refreshing the edit page, but the page requires to be updated to use the public URL inside its content.

    As a solution, we can remove the page URL from the links and keep only the generated hashtags.

    For example, from https://example.com/sample-page/#heading-title to #heading-title.

    I'm unable to find information on whether there was a reason for using the full URLs. Such page links work with hashtags only, and this solution might be sufficient.

  2. added
    [Block] Table of contents (experimental)
    Needs Testing
    on Aug 28, 2022
  3. aaronrobertshaw commented on Aug 30, 2022

    @aaronrobertshaw
    Contributor

    Following the replication steps outlined in this issue, the ToC links worked for me on initial save. @Greatdane Could you confirm if this issue has been resolved or expand on the test steps?

    Screen.Recording.2022-08-30.at.2.47.08.pm.mp4
  4. aatanasovdev commented on Aug 30, 2022

    @aatanasovdev
    Contributor

    @aaronrobertshaw, which permalink structure did you use while recording the video?

    If the Plain permalink structure is used, the block works.

    However, it doesn't work for me when using other permalink structures such as Post Name. In that case, the links don't include the page's name, but auto-draft is added instead.

    Screenshot 2022-08-30 at 8 22 44

  5. aaronrobertshaw commented on Sep 5, 2022

    @aaronrobertshaw
    Contributor

    Hi @aatanasovdev, I tested with both Plain and Post Name permalink structures however I only posted a single video.

    Here's a video of the table of contents working for me on initial save with the Post Name permalink setting selected.

    Screen.Recording.2022-09-05.at.7.56.07.pm.mp4
  6. aatanasovdev commented on Sep 7, 2022

    @aatanasovdev
    Contributor

    Hi @aaronrobertshaw, it looks like you're adding the Table of Content block to a post, not a page.

    Publishing a post works slightly differently than a page.

    When we add a post, there is no auto-draft URL generation before publishing the page (I haven't checked why this happens, but it has to be consistent). In that case, the TOC block uses URLs like SITE_URL/?p=34. The links work because there is a redirect from SITE_URL/?p=34 to the pretty URL (WordPress handles this redirect).

    On the other side, when a page is published, the TOC block uses auto-draft inside its links.

    Here is a video that shows how the block behaves.

    Test-TOC-block.webm

    Probably, it will be better if the auto-draft URL generation issue is addressed. Initially, the links will use the plain format, which will be changed in the next content update. In that case, the links will work without pointing to a non-existing page.

    Alternatively, the links can be regenerated automatically behind the scenes when a page is published. However, it doesn't seem right.

  7. aaronrobertshaw commented on Sep 8, 2022

    @aaronrobertshaw
    Contributor

    it looks like you're adding the Table of Content block to a post, not a page.

    🤦 Sorry to waste your time. You are correct, I was on autopilot and misread the instructions.

    I can confirm I'm seeing this issue when I actually test it properly.

    Screen Shot 2022-09-08 at 10 26 08 am

  8. changed the title [-]Table of contents block – links don't work on initial save[/-] [+]Table of contents block – links don't work on initial page save[/+] on Sep 27, 2022
  9. added
    [Type] Bug
    and removed
    Needs Testing
    on Sep 27, 2022
  10. mahnunchik commented on Apr 18, 2023

    @mahnunchik

    I can confirm incorrect behaviour:

    • Incorrect URL in ToC block after initial save/publish action.
    • After second Update all links are correct.

    WordPress version 6.2.0
    Gutenberg plugin 15.1.1

  11. felipevelzani commented on Aug 18, 2023

    @felipevelzani
    Member

    I could reproduce this as well. Using Gutenberg 16.4.

    This is the code on the page in case it helps: https://d.pr/n/kp52JN

  12. annezazu commented on Aug 24, 2023

    @annezazu
    Contributor

    Noting that this was brought up in the twenty fifth call for testing for the FSE Outreach Program using 16.4:

    But it looks like the block does not work on previews only after the page has been published. As the preview does not have the correct structure. https://bluewinddesign.com/?page_id=385 (previewed page). Clicking one of the headings in the Table of Contents block the link anchor looks like this: https://bluewinddesign.com/resume/#head-3 . It went to a 404 page.

    I've added it to the stabilizing the TOC issue!

  13. Sukhendu2002 commented on Jun 10, 2025

    @Sukhendu2002
    Contributor

    I attempted to reproduce this issue but was not able to—it appears to be resolved.

    Tested with:

    • WordPress: 6.9-alpha-60275
    • Gutenberg: 21.0.0-rc.1
    Screen.Recording.2025-06-10.at.9.19.43.AM.mp4
  14. 5 remaining items

  15. scruffian commented on Sep 12, 2025

    @scruffian
    Contributor

    Sorry closed accidentally

  16. mikachan commented on Oct 6, 2025

    @mikachan
    Member

    I've attempted to fix this as part of #72110.

  17. moved this to 🔎 Needs Review in WordPress 6.9 Editor Taskson Oct 7, 2025
  18. moved this from 🔎 Needs Review to 🏗️ In Progress in WordPress 6.9 Editor Taskson Oct 8, 2025
  19. t-hamano commented on Oct 21, 2025

    @t-hamano
    Contributor

    Since the ToC block itself has been punted to WP 7.0, this issue will also be punted to 7.0.

  20. moved this from 🏗️ In Progress to 🦵 Punted to 7.0 in WordPress 6.9 Editor Taskson Oct 21, 2025
  21. getdave commented on Jul 16, 2026

    @getdave
    Contributor

    I'm currently working on dynamically rendering the block PHP side - #43595

    Once merged I will remove the editor code which serializes the headings to the ToC block markup on save.

    This should resolve the auto-save problem but it will need validating and test coverage.

  22. tyxla commented on Sep 11, 2026

    @tyxla
    Member

    @getdave is this addressed after #80318 and #80404? If yes, let's close it, and also consider closing #70386.

  23. Mamaduka commented on Sep 12, 2026

    @Mamaduka
    Member

    Yes, this was fixed when we switched the block to server-side rendering.

  24. moved this from 🦵 Punted to 7.0 to ✅ Done in WordPress 6.9 Editor Taskson Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Block] Table of contents (experimental)[Status] In Progress[Type] Bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions