Repository navigation
fix(mermaid): share pending render for concurrent re-renders - #1997
Merged
Merged
Conversation
A preview re-render while a diagram is still rendering (e.g. when MathJax fires md:mathjax-ready) started a second mermaid.render with the same element id. The two runs measured each other's temporary elements and the cached SVG lost its viewBox, class and max-width, so the diagram rendered blank. Share the in-flight render per cache key instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
🚀 Surge Preview has been successfully deployed! Preview URL: https://doocs-md-preview-pr-1997.surge.sh Built with commit ca359df |
yanglbme
approved these changes
Oct 21, 2026
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.
Summary
renderMermaidSvgonly consults the finished-SVG cache. If the preview re-renders while the first render of a diagram is still pending, a secondmermaid.render('mermaid-svg-<cacheKey>', code)starts with the same element id. This happens in practice when MathJax finishes loading and firesmd:mathjax-ready, which re-renders a document that contains a Mermaid block.The two concurrent runs measure each other's temporary elements, and the SVG that ends up in
svgCachehas noviewBox,classormax-width, so the diagram renders blank, and stays blank because the broken SVG is now cached.This PR keeps a
pendingRenders: Map<string, Promise<string>>keyed by the same cache key. A render that arrives while one is in flight awaits the existing promise instead of callingmermaid.renderagain; the entry is removed infinally, so a failed render can be retried on the next pass. Each caller still updates its own placeholder element throughrenderMermaid'shandleResult.Type of Change
Test Procedure
packages/core/src/extensions/mermaid.test.ts: mocksmermaidwith a gatedrender, renders the same Mermaid markdown twice while the first render is pending, then assertsmermaid.renderwas called once, the placeholder receives the SVG, and the next render inlines the cached SVG.main(rendercalled twice) and passes with this change.pnpm --filter @md/core testpasses (158 tests);tsc --noEmitinpackages/corepasses.Pre-flight Checklist
pnpm run lintpasses (ESLint clean on the changed files)🤖 Generated with Claude Code