Skip to content

Render inactive nav sections once per build - #130

Merged
shenxianpeng merged 1 commit into
mainfrom
claude/project-thread-8gce4u
Nov 1, 2026
Merged

shenxianpeng merged 1 commit into
mainfrom
claude/project-thread-8gce4u

Conversation

@shenxianpeng

Copy link
Copy Markdown
Member

Every page renders the whole navigation again, so build time grows with the square of the number of pages (upstream mkdocs/mkdocs#3695). Profiling a 1,600-page site showed the nav was most of the build time with both the mkdocs theme and Material.

A nav section that doesn't contain the current page renders the same on every page, except for its relative URLs. This adds a nav_fragment call block that themes wrap around a nav item:

{% call nav_fragment(nav_item) %} ... {% endcall %}

The first render of an inactive section is stored with placeholders for the URLs that went through the url filter. Later pages reuse it and fill in their own relative URLs. When all URLs of a section share a directory and the page is outside it, filling in is one string replace instead of one URL computation per link. Sections containing the current page, and sections containing a page listed twice (PageAlias), always render as before. The cache is per Jinja environment, so per build, and starts over if a plugin passes a different nav object.

The built-in mkdocs and readthedocs themes use it. Output is byte for byte the same.

Numbers (clean build, benchmark corpus)

Pages mkdocs theme before → after Material before → after ¹
100 1.25s → 1.03s 1.89s → 1.50s
400 6.30s → 4.14s 12.33s → 6.96s
1,600 52.8s → 24.0s 118.9s → 35.9s

readthedocs theme, 400 pages: 7.9s → 4.2s.

¹ Material needs the same block in its nav-item.html; that is a follow-up PR in mkdocs-ng/mkdocs-material, guarded by {% if nav_fragment is defined %} so it still works with older MkDocs NG.

Checks

  • New unit tests for the cache, plus a test that builds a site with both built-in themes, both use_directory_urls settings, explicit and automatic nav (with a repeated page, anchors, external and absolute links) with and without the cache and compares every HTML file.
  • Built the benchmark corpus with mkdocs, readthedocs and Material: identical output apart from the build date.
  • Plugin compatibility matrix: 32/32 pass.

Targets 1.11.0 so that 1.10.0 final stays as tested in rc1; the release notes have a new 1.11.0 section.

Checklist

  • New tests added for new behavior (if applicable)
  • Documentation updated (if applicable)
  • Release notes docs/about/release-notes.md updated (if applicable)

Generated by Claude Code

Every page rendered the whole navigation again, so build time grew with
the square of the number of pages. A nav section that doesn't contain the
current page renders the same on every page except for its relative URLs.

Add a nav_fragment call block that records such a section once, with
placeholders for the URLs that went through the url filter, and fills in
each page's relative URLs afterwards. Sections from one directory take a
fast path: one string replace per page instead of one URL computation per
link. Output is byte for byte the same. The mkdocs and readthedocs themes
use it.

1,600 pages: mkdocs theme 52.8s -> 24.0s; Material with the same block in
its nav template 118.9s -> 35.9s.
@codspeed

codspeed Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 37.64%

⚡ 3 improved benchmarks
✅ 25 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_template_render_large_nav 39 ms 21 ms +85.51%
⚡ test_full_build_scaling[200] 19.4 s 15.8 s +22.21%
⚡ test_build_theme_pages 742.4 ms 645.4 ms +15.02%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/project-thread-8gce4u (c07f641) with main (19c83b9)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit b71fd6d into main Nov 1, 2026
24 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-8gce4u branch November 1, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant