Repository navigation
Render inactive nav sections once per build - #130
Merged
Merged
Conversation
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.
Contributor
Merging this PR will improve performance by 37.64%
Performance Changes
Tip Curious why performance improved? Comment Comparing |
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
mkdocstheme 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_fragmentcall block that themes wrap around a nav item:The first render of an inactive section is stored with placeholders for the URLs that went through the
urlfilter. 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 differentnavobject.The built-in
mkdocsandreadthedocsthemes use it. Output is byte for byte the same.Numbers (clean build, benchmark corpus)
mkdocstheme before → afterreadthedocstheme, 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
use_directory_urlssettings, explicit and automatic nav (with a repeated page, anchors, external and absolute links) with and without the cache and compares every HTML file.mkdocs,readthedocsand Material: identical output apart from the build date.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
docs/about/release-notes.mdupdated (if applicable)Generated by Claude Code