Skip to content

Shiki warning: StorySourceCode creates a new highlighter on every mount #849

Description

@earthabledev

Describe the bug

Opening / switching stories with the Source panel active repeatedly creates new Shiki highlighter instances. After enough navigations the browser console warns:

[Shiki] 10 instances have been created. Shiki is supposed to be used as a singleton, consider refactoring your code to cache your highlighter instance; Or call `highlighter.dispose()` to release unused instances.

This is more noticeable in projects with many stories (e.g. browsing a large story tree), but it is not project-specific.

Root cause: @histoire/app StorySourceCode.vue calls createHighlighter() inside onMounted and neither reuses a shared instance nor calls dispose() on unmount:

https://github.com/histoire-dev/histoire/blob/main/packages/histoire-app/src/app/components/panel/StorySourceCode.vue#L89-L100

Shiki documents that highlighters should be singletons (warns every 10 instances). Related Shiki discussion: shikijs/shiki#756

Suggested fix: cache a module-level Promise<Highlighter> (same langs/themes) and reuse it across remounts. Optionally also share/dispose the Node-side highlighters created in packages/histoire/src/node/markdown.ts (createMarkdownRenderer is invoked from both the Vite markdown plugin and the markdown file watcher without reuse).

I do not plan to open a PR unless maintainers want one.

Reproduction

  1. Open the Vue 3 Histoire starter: https://stackblitz.com/edit/histoire-vue3-starter
  2. Run Histoire (pnpm run story:dev / the StackBlitz Histoire script).
  3. Open any story so the Source panel is visible.
  4. Switch between stories (or variants that remount the source panel) 10+ times.
  5. Observe the Shiki singleton warning in the browser console.

No custom app code is required; the default starter is enough. The same path still exists on main as linked above.

System Info

  System:
    OS: macOS 26.5.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 4.86 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.14.0 - …/bin/node
    npm: 11.9.0 - …/bin/npm
    pnpm: 10.30.3 - …/bin/pnpm
    bun: 1.3.11 - …/bin/bun
  Browsers:
    Chrome: 151.0.7922.71
    Edge: 150.0.4078.105
    Firefox: 152.0.6
    Safari: 26.5.2
  npmPackages:
    @histoire/plugin-vue: 1.0.0-beta.1 => 1.0.0-beta.1
    @vitejs/plugin-vue: ^6.0.8 => 6.0.8
    histoire: 1.0.0-beta.1 => 1.0.0-beta.1
    vite: ^8.1.5 => 8.1.5

Used Package Manager

pnpm

Validations

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions