Repository navigation
Description
Note
As the maintainer of Material for MkDocs, I'd like to open a discussion on how we can collaborate to enhance MkDocs. This initiative is inspired by Tom Christie's recent reflections on the future development of MkDocs. I believe that through collective efforts, we can identify and implement improvements that will benefit our users significantly.
The mkdocs serve command provides a powerful write-build-check-repeat loop that is integral for documentation projects, setting MkDocs apart from many static site generators that lack live preview functionality. This feature greatly enhances the efficiency and accuracy of developing and refining documentation, allowing for immediate feedback and iterative improvements.
Startup time of mkdocs serve
Unfortunately, there are significant issues with the mkdocs serve command, particularly when working with large documentation projects that consist of thousands of pages. Currently, mkdocs serve requires a full build of the documentation before it becomes interactive. This process can take an extensive amount of time, ranging from 30 to 40 minutes for large projects. This delay significantly impedes the ability to use mkdocs serve effectively for previewing changes.
The need for a preview is crucial, especially given that Material for MkDocs integrates with the Python Markdown Extensions, a powerful set of Markdown extensions, especially for technical writing, adding features like content tabs via Tabbed and enhanced indent detection through SuperFences. Unfortunately, editor support for these syntaxes is limited, if not non-existent. This lack of support means that authors must rely on mkdocs serve to preview changes. Given the current build times on large projects, authors face considerable difficulty in efficiently making and reviewing changes, essentially working 'blind' without this functionality. Performance is in fact one of the most major critiques on MkDocs.
Problems with the --dirtyreload flag
The --dirtyreload flag in MkDocs offers a partial solution to speed up the re-build process during a documentation project's development by not rebuilding the entire site with each change. However, this flag only affects subsequent builds and does not improve the initial build time. Moreover, it introduces issues such as incorrect navigation and incomplete metadata, which can disrupt the functionality of plugins, like the blog plugin that struggles to correctly update archive and category indexes under --dirtyreload. Consequently, plugins must be designed to specifically work around these limitations, complicating their development and integration.
Conclusion
To significantly enhance the editing experience with MkDocs and reduce the environmental impact by saving thousands of build minutes daily, we need to focus on two critical improvements:
-
Reducing the initial preview load time: The time it takes from starting the live server to when the preview is first available needs to be substantially decreased. This change would make MkDocs more usable, especially for large projects.
-
Speeding up live preview updates: After making edits, the time to see these changes in the preview should be minimized. This improvement will support a more efficient and iterative documentation process.
Potential strategies to achieve these improvements include implementing more sophisticated caching mechanisms and exploring the possibility of parallelizing the build process. These changes would address both the initial and subsequent build times, making mkdocs serve a more robust tool for documentation development.
Activity
pawamoy commentedon May 3, 2024
Are there public examples of large repositories that take up to 30 minutes to build? I tried locally with 10K dummy files and ran out of memory before the site was built 😅 With 1K files, the template rendering seems to be the most costly.
squidfunk commentedon May 3, 2024
Users have mentioned this in multiple occasions, but I'm having a hard time finding it due to GitHub's rather mediocre issue search. Here's what I could gather from a quick search:
The fact is that 30min is a worst case scenario. Even a repeated build that takes 1 minute is too slow to be useful, and
--dirtyreloadisn't a workable solution due to the problems stated, especially for plugin authors. It also doesn't solely depend on the number of pages, but on the plugins used. Thus, discussing how plugins and the core could better work together to employ caching and reduce build time is a discussion we should start.Running out of memory is another problem that should be fixed, as already discussed in #2669
squidfunk commentedon May 3, 2024
This is a project with 3,400 files and a very limited set of plugins, i.e.,
search,minifyandsocial:https://github.com/openfabr/terraform-provider-cdk-docs
IMHO, not many plugins, and the social plugin which I wrote employs caching, which means repeated builds are much cheaper due to leveraging cached images. I've built the project on my machine, an M2 MacBook Pro:
First build
Repeated build (social plugin cached)
It's infeasible to make edits on this project without
--dirtyreload, which as mentioned is incorrect, plus the author has to wait for more than 8 minutes until the live reload server becomes available. Add a few more plugins and a few hundred more pages and you're up to 20 minutes.kamilkrzyskow commentedon May 4, 2024
I tested the repository mentioned above on my Ryzen 3600 Windows 10 PC, mkdocs==1.6.0, mkdocs-material==9.5.20
First build:
Repeated build:
I do not dare to run it again 😅
I used my performance_debug hook.
Debug YAML result: performance_debug_first.yml.txt
More info about the categories can be found in the gist Python file, but most should be self explanatory, but the amount of files could have generated quite a bit of noise 🤔
Currently the
mkdocs servewill invoke the same asmkdocs build, so the benchmark results apply there too.The main issue is with the minify plugin, a much cheaper (performance-wise) minification, of sorts, could be achieved using Jinja2 Environment settings, which I mentioned here, and another approach would be proper enforcement of whitespace management inside the template files, via the
%-tags. Perhaps some sort of another minify plugin needs to be released which uses some C/Rust libraries to handle the minification process 🤔I'm surprised those markdown values are so low, as last time I checked with GMC (~190 files) the same classes had ~6 seconds each. Perhaps the complexity of the Markdown or the amount of Code Blocks has a bigger impact than I thought. But still 3k files vs 200 files and only a x2 time increase seems odd hmm
Template rendering took ~270 seconds:
this time gets repeated each re-serve without
--dirtyreloadCaching for later builds with
mkdocs servewon't help much, as it immediately turns off the prospective user.Also rendering the whole docs in the background with concurrency seems also like a waste of resources when I only want to check one web page only.
So I would like to see some sort of on-demand loading,
servewould only process index.html and later only load pages when navigating to them. This of course breaks the laston_post_buildevent, as plugins expect all files to be present in thesitedirectory, so invoking it after only a few pages were built could lead to issues. Other events are more agile IMOI guess this would requires a fork in
mkdocs serveandmkdocs buildevent loops? Rather risky, but would allow for more control maybe? Just a first top of the head idea ✌️pawamoy commentedon May 4, 2024
Like this one https://github.com/monosans/mkdocs-minify-html-plugin? Could you build once with it and see if you just spared 950 seconds or so 😛? It only minifies HTML files though apparently (but still CSS and JS within them).
pawamoy commentedon May 4, 2024
Also, solid work @kamilkrzyskow 👍 Thanks for making and sharing all this!
squidfunk commentedon May 5, 2024
Ah, nice, I didn't know about the minify-html plugin! I'll check it out and probably switch to it. Offloading pure string processing to Rust makes a lot of sense.
waylan commentedon May 6, 2024
The issue is that the site navigation requires the entire pages collection to be available for the one page to be rendered. This is where caching and/or concurrency would likely be helpful. For that matter, the pages don't need to all be fully rendered, but they all do need to be read and processed to a certain extent to determine the page title, etc for the nav.
And then there are those scenarios where a page's content consists of the pages collection (either be means of a plugin or as a static template). In that case, to render that page (even if the nav is excluded), the entire pages collection is needed.
Ultimately, it has been the above two issues which have thus far prevented a better solution from being developed. Work out a way to address those and then we may have a workable solution.
pawamoy commentedon May 8, 2024
Quick thought: what if plugins informed MkDocs whether each one of their hooks could be executed concurrently, or only sequentially? I'm imagining some utilities to build a "pipeline" of things to run depending on whether they support concurrency or not.
Quick flowchart which doesn't make sense but illustrate the idea:
flowchart TD p1f["plugin1.on_files"] p2f["plugin2.on_files"] p3f["plugin3.on_files"] p1n["plugin1.on_nav"] p2n["plugin2.on_nav"] p3n["plugin3.on_nav"] p1pm["plugin1.on_page_markdown"] p2pm["plugin2.on_page_markdown"] p3pm["plugin3.on_page_markdown"] start --> p1f & p2f p1f & p2f --> p3f p3f --> p1n & p2n & p3n p1n & p2n & p3n --> p1pm p1pm --> p2pm & p3pmon_filesrun concurrently, then plugin 3on_filessequentially.on_navrun concurrently.on_page_markdownruns sequentially, then plugin 2 and 3on_page_markdownrun concurrently.EDIT: hmm I suppose there's another possible layer of concurrency on files/pages themselves. The transformation pipeline would likely be quite complex. I'm sick and have fever today so please be indulgent 😂
humitos commentedon May 9, 2024
This is exactly what Sphinx does. Each extension defines if it's safe for parallel reading and/or parallel writing. See https://www.sphinx-doc.org/en/master/extdev/index.html#extension-metadata
I haven't checked how it works internally, but it's probably something to explore a little more and see if there are some ideas that can be reused.
dr-br commentedon May 10, 2024
I would like to be able to use parallel build. It has been stated in #1900 that the benefit is not so high. However, I have lots of jupyter-notebooks to convert (the execute step consumes most of the time). I ended up executing all notebooks concurrently in advance.
lovelydinosaur commentedon May 17, 2024
Okay, so I've been working on this and I've got enough to demo now...
https://github.com/mkdocs/sketch/tree/main
That's a work-in-progress of "how could mkdocs look" that properly deals with this issue.
Specifically, the
mkdocs servecommand doesn't require a site build at all*I needed to do a bit of poking to make this work with the terraform example above (since it doesn't include a
navconfig), tho once I'd done there serve startup time was under a second.There's other aspects that I'm looking to address as part of that work, just getting things into shape so that I've got a coherent body of work to start sharing here.
* search indexes aren't in there just yet. yes they would require a full-site build, but we can use HTML
rel=preloadlinks to prompt them in the background, and likely also have per-page caching.pawamoy commentedon May 18, 2024
Nice work @tomchristie!
In the case of mkdocstrings and its cross-references ability,
rel=preloadwouldn't be enough. To statically resolve a cross-reference, we must wait for all pages to have been built. The only way to make cross-references work when serving pages on the fly (without building everything) would be to inject some Javascript magic 🤔 Like, the plugin would store query-able state in the server, that the client could continuously request, until all needed pages were loaded withrel=preloadand the unresolved references on the current page can be resolved 🤔 And since we don't know which pages are needed to resolve a reference, all pages would have to be pre-loaded anyway 🤔 (or, if not all, maybe most pages, with a priority order or something).squidfunk commentedon May 18, 2024
This looks really promising! Really excited how this will work with more complex setups. I guess there're still things to be worked out (haven't checked the implementation), but it's a great start! 👏
DonatStankiewicz commentedon Dec 20, 2024
Recently I was struggling with the problem of the
mkdocs servecommand taking a very long time to execute and I managed to find some info, so I decided to share it here. It would be ideal if someone managed to fix the problem, but at least I will share my workaround solution.In my repository I have a separate links.md file, where all external links are stored and in the other files we only use labels. Recently we did a clean up of this file and in the process all empty lines were removed. The resulting file contained about 500 lines and in each line was one link. Since then the documentation was building a very long time. Interestingly, the process did not spend a lot of time in any specific place, but in general building every page significantly lengthened.
I noticed that adding empty lines in the links.md file solves this problem. I suspect that the
mkdocsormarkdownpackage is written in such a way that it does not tolerate many links one after another and thus it takes much longer to search for these links. So now I know that links must be separated by an empty line, otherwise themkdocs servecommand takes longer to execute. After adding empty lines between each line containing a link, the commandmkdocs serveexecution takes about 10 times less time. I do not remember the documentation being built as quickly as it is now. 😄pawamoy commentedon Dec 20, 2024
@DonatStankiewicz thanks for sharing. What you describe is surprising, but I believe you 😄 Just to be clear: you're using
pymdownx.snippetsto auto-append references to each Markdown document, right?DonatStankiewicz commentedon Jan 3, 2025
@pawamoy yes, exactly 😄
Here is a fragment of mkdocs.yml:

And here is what the links.md file looks like:

pawamoy commentedon Jan 3, 2025
@DonatStankiewicz thanks. Could you create a new discussion (https://github.com/mkdocs/mkdocs/discussions) with a reproducible example that demonstrates the performance issue?
traquito commentedon Jan 20, 2025
Hi, I think the output of this works is fantastic, but yes there is a genuine slowness on each rebuild that really impacts my ability to get work done effectively.
My use case is that I have a medium-low sized site, and I'll be editing a few pages at a time, tops. As in, most of the site is not going to change. I click save to a file, and the entire site is recreated. This now takes 15 seconds and is getting longer every time it seems.
What would speed up my use case would be, perhaps, an additional and more specialized mode of 'serve' which:
Other features to think about:
To be clear with the thought, most of the time, I do not care about the "full site" implications of a change to a single page, I just want to see the markup I just wrote rendered properly. If I wanted to see the impact on site search or some other site-wide consideration, I could just refresh and pay the cost. But 99% of the time I don't want that, and pay the full cost anyway.
I also have interspersed within the documentation folders some code folders (javascript, etc) which each time I touch those the site reloads. I don't know if there's a better way to do this. I typically run two webservers to give me static access to the mkdocs serve for the documentation. It's a shame my code edits trigger site-wide reload.
I don't know how the underlying generation works, but the computer I work on is 12 core and 100GB RAM with an SSD drive hosting the files. I'd be happy to have the option to cache the entire site in RAM (or have it mirrored into a RAM-backed filesystem which is updated on each file change to be rebuilt?). Happy for maximum parallelization across cores. Time cost really is the worst cost for me.
Thanks for all the work.
Doug
pawamoy commentedon Jan 20, 2025
Just in case: have you tried
mkdocs serve --dirty? It's not perfect, but could help. Otherwise your suggestions are all good 🙂traquito commentedon Jan 20, 2025
Wow I wish I had spent the 15 seconds learning about this flag before. Thank you so much!
VigneshVSV commentedon Jun 9, 2025
It would nice to speed up the mkdocs building time, while not introducing bugs.
I recently created a skaffold compatible mkdocs deployment to develop docs directly with kubernetes cluster.
skaffold offers filesync where I can simply drop a new file into a pod without having to rebuild the docker container.
Only issue next is to fix the slower build time of mkdocs. I have had limited luck with dirty reload.
The project is here if anyone is interested to see: https://github.com/hololinked-dev/docs-v2
Add scaling and serve-loop benchmarks to the CodSpeed suite
Add scaling and serve-loop benchmarks to the CodSpeed suite (#89)