Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support diagram preloading #4275

Open
luin opened this issue Apr 4, 2023 · 3 comments
Open

Support diagram preloading #4275

luin opened this issue Apr 4, 2023 · 3 comments
Labels
Status: Triage Needs to be verified, categorized, etc Type: Enhancement New feature or request

Comments

@luin
Copy link
Contributor

luin commented Apr 4, 2023

Proposal

We are currently using v9.1 and lazy loading Mermaid using await import('mermaid'). After upgrading to v10, we noticed that an additional network round trip is required to render the diagrams. This is because after downloading the mermaid core file, mermaid v10 will lazy load the code for diagrams that are used on the page.

I'm wondering if there is a plan to provide preloading support for diagrams. A simple approach would be making the diagram files importable:

await Promise.all([
  import('mermaid'), // may be just import('mermaid-core')?
  import('mermaid/flowchart'),
  import('mermaid/mindmap'),
  // ...
])

Currently, it doesn't work because there are content hashes in the diagram files' file names.

Another approach would be providing a mermaid-bootstrap, the only purpose of it is to load diagrams so it's very tiny and can be bundled into the main chunk.

Example

No response

Screenshots

No response

@luin luin added Status: Triage Needs to be verified, categorized, etc Type: Enhancement New feature or request labels Apr 4, 2023
@sidharthv96
Copy link
Member

You can use the registerExternalDiagrams to pre-load all diagrams.

await mermaid.registerExternalDiagrams([], {
        lazyLoad: false,
      });

If this is helpful, we can expose a method on mermaid or add a config to preload diagrams.

Also, curious to know about your use case.

@luin
Copy link
Contributor Author

luin commented Apr 23, 2023

Thank you for the update! However, this means we must import Mermaid (which has a relatively large bundle size as it's not just a bootstrapper) into our main bundle to prevent network waterfalls.

In our use case, we are using v9 and we only lazy-load Mermaid when users visit a page containing Mermaid code. This helps us reduce our main asset bundle size. This approach only requires one round-trip to download all necessary assets. However, after version 10, there are two round-trips:

  1. We need to download the Mermaid first.
  2. Mermaid retrieves the diagram types from the code we provide and then downloads the corresponding diagrams.

My initial attempt to avoid the two round trips is to bundle Mermaid with our main bundle and let Mermaid handle lazy-loading for diagrams. Unfortunately, this approach was not effective because the Mermaid bundle is somewhat large. Therefore, I believe an alternative direction would be to bundle all diagrams and the Mermaid core into a single bundle, which can be lazy-loaded as a whole. Does this make sense?

@fritx
Copy link

fritx commented May 15, 2024

How do I specify which of the diagrams to enable? Is the feature supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage Needs to be verified, categorized, etc Type: Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants