Skip to content

Reuse the HTML of unchanged pages in mkdocs serve - #154

Merged
shenxianpeng merged 2 commits into
mainfrom
claude/project-thread-epdfwi
Nov 2, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
claude/project-thread-epdfwi

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

After an edit, mkdocs serve rendered 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 the mkdocs theme 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 build gives. It only reuses more.

How it works

RenderCache now also keeps each page's HTML, keyed on a digest of everything the template can see:

  • every attribute of the page (vars(page): HTML, title, meta, TOC, previous/next, URLs, and any attribute a plugin set)
  • the navigation, with the titles, URLs and metadata of its pages (themes show meta.icon/status in the nav)
  • the pages list, the other context variables (base_url, extra_css, i18n, …) and config.extra (which alternate changes per page)
  • the rest of the config, without plugins and hooks
  • the names of the Jinja globals, filters and tests
  • the template files: a recording loader notes which file each template came from, with its mtime and size. At the start of a rebuild, if any of them changed, or another file now takes its place (a new override in 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_context and post_page still run for every page. When no plugin has a post_page event and the file already holds the same HTML, the page is skipped right away (new SiteOutput.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 serve loads can have a relative config_file_path and 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:

serve, edit one page before after
mkdocs theme, rebuild 1.7 s 0.6 s
Material (with ng-material#20), rebuild 3.8 s 1.2–1.3 s
Sumi, rebuild 2.5 s 1.9 s
mkdocs theme, edit → served 2.3 s 0.9 s
Material, edit → served 4.1 s ~1.4 s
Sumi, edit → served 3.3 s ~2.2 s

The rest of Sumi's time is in its own plugin (on_page_markdown and on_post_build, about 0.9 s), which is a separate fix in mkdocs-sumi.

Checked

  • For all three themes, after a body edit, a title change (the nav changes on every page), a front-matter title change, a new page and a deleted page, the serve output directory is byte for byte the same as a clean build's, except for the build date line.
  • New tests: unchanged pages are not rendered again; a title change renders every page; a changed template or a new override renders every page; an unknown object in the context turns the reuse off; post_page runs on reused pages; attributes that plugins set on a page are part of the key.
  • Full test suite, ruff and mypy pass.

Related Issue

Item 3 of the serve performance plan; follow-up to #131, #145 and #152.

Checklist

  • New tests added for new behavior
  • Documentation updated (dev-guide/themes.md "Rendering pages during mkdocs serve", note in dev-guide/plugins.md)
  • Release notes docs/about/release-notes.md updated (1.11.0 unreleased)

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.
@shenxianpeng shenxianpeng self-assigned this Nov 2, 2026
@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 21.26%

❌ 1 regressed benchmark
✅ 28 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_serve_rebuild 988.3 ms 1,255.1 ms -21.26%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/project-thread-epdfwi (6c83f78) with main (80488fb)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 503edbe into main Nov 2, 2026
23 of 24 checks passed
@shenxianpeng
shenxianpeng deleted the claude/project-thread-epdfwi branch November 2, 2026 21:18
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