Repository navigation
Reuse the HTML of unchanged pages in mkdocs serve - #154
Merged
Merged
Conversation
After an edit, every rebuild rendered the theme template of every page, which took most of the rebuild time on large sites, since each page holds the whole navigation. Keep each page's HTML, keyed on a digest of its template context: every attribute of the page, the navigation with the titles, URLs and metadata of its pages, the list of pages, the config, the other context variables, the template files that were loaded and the Jinja globals. A page whose context holds anything without a stable description is rendered as before. The page_context and post_page events still run for every page; when there are no post_page events and the file already holds the HTML, the page is skipped right away. The HTML is kept compressed, about 16 KB per page with Material. With 1,000 pages, an edited page shows up after 0.9 s instead of 2.3 s with the mkdocs theme, and after about 1.4 s instead of 4.1 s with Material.
Contributor
Merging this PR will degrade performance by 21.26%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
This was referenced Nov 2, 2026
Merged
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.
After an edit,
mkdocs serverendered the theme template of every page again. Since every page holds the whole navigation, that was most of the rebuild time on large sites (1.3 of 1.7 s with themkdocstheme and 3.2 of 5.4 s with Material, on 1,000 pages), even though the Markdown of unchanged pages was already cached (#131, #152).This is not "rebuild the edited page first": every rebuild is still a full build, and the output is the same as
mkdocs buildgives. It only reuses more.How it works
RenderCachenow also keeps each page's HTML, keyed on a digest of everything the template can see:vars(page): HTML, title, meta, TOC, previous/next, URLs, and any attribute a plugin set)meta.icon/statusin the nav)pageslist, the other context variables (base_url,extra_css,i18n, …) andconfig.extra(whichalternatechanges per page)custom_dir), all cached pages are dropped. If a plugin replaces the loader, the page cache is off for that build.Values are described with
_stable(), which knows scalars, containers, pages, nav items, files, TOCs, dates, enums, configs, locales and functions. Anything else, such as an object of a plugin's own class, makes the page render as before. So the default is correct output, not speed.page_contextandpost_pagestill run for every page. When no plugin has apost_pageevent and the file already holds the same HTML, the page is skipped right away (newSiteOutput.keep()), so unchanged files aren't encoded and hashed again. The HTML is kept zlib-compressed (about 16 KB per page with Material instead of 320 KB), so memory stays reasonable on big sites.Along the way: the first config that
mkdocs serveloads can have a relativeconfig_file_pathand later ones an absolute one, which made the first rebuild miss. The path is normalized in the key.Numbers
1,000-page benchmark site, 4 cores,
benchmarks/corpus:mkdocstheme, rebuildmkdocstheme, edit → servedThe rest of Sumi's time is in its own plugin (
on_page_markdownandon_post_build, about 0.9 s), which is a separate fix in mkdocs-sumi.Checked
post_pageruns on reused pages; attributes that plugins set on a page are part of the key.Related Issue
Item 3 of the serve performance plan; follow-up to #131, #145 and #152.
Checklist
dev-guide/themes.md"Rendering pages duringmkdocs serve", note indev-guide/plugins.md)docs/about/release-notes.mdupdated (1.11.0 unreleased)