Repository navigation
Description
Describe the bug
In a production build, __vitePreload() creates a <link rel="stylesheet"> for an
async chunk's CSS and attaches load / error listeners to it. Both listeners are
never removed after the promise settles.
The <link> itself stays in document.head for the lifetime of the page, so the
listener closures keep the whole __vitePreload() scope reachable — including
baseModule, the dynamic import() factory passed in by user code.
If that factory is defined inside a component or function scope (for example an
inline () => import('./Panel.vue') in <script setup>), the enclosing scope is
retained as well. In our app this kept an entire detached DOM subtree (~900 nodes)
alive after closing a tab. It reproduces on every page load, permanently, and only
in production builds.
vite dev is unaffected: it uses native import() plus injected <style> tags,
so __vitePreload is never emitted.
Expected: after the link settles, the listeners should be removed so the preload
scope can be garbage collected.
Reproduction
https://github.com/bpuns/vite-preload-listener-memory-leak.git
Steps to reproduce
npm installnpm run build- serve
distover HTTP (e.g.npx serve dist) and open it - click mount — appends a
<div>and triggersimport('./panel.js'), which
imports a CSS file, so__vitePreloadinserts a<link rel="stylesheet"> - click unmount — the
<div>is removed from the document - DevTools → Memory → click Collect garbage twice
- take a heap snapshot and filter for
Detached
Actual: the <div> is still listed as a detached node, together with its 100
<span> children.
Control: move const load = () => import('./panel.js') to module top level,
rebuild, repeat steps 4–7 → the node is collected.
System Info
System:
OS: Windows 11 10.0.26300
CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
Memory: 22.45 GB / 39.81 GB
Binaries:
Node: 22.18.0 - C:\Users\bpuns\AppData\Local\fnm_multishells\9016_1793150241825\node.EXE
npm: 10.9.3 - C:\Users\bpuns\AppData\Local\fnm_multishells\9016_1793150241825\npm.CMD
pnpm: 12.15.0 - C:\Users\bpuns\AppData\Local\fnm_multishells\9016_1793150241825\pnpm.CMD
Browsers:
Chrome: 156.0.8078.48
Edge: Chromium (140.0.3485.66)Used Package Manager
pnpm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Activity
perf(build): remove preload link listeners on settle (fix #23920) (#2…
Update dependency vite to v8.3.6
Update dependency vite to v8.3.6
fix(deps): update dependency vite to v8.3.6 (#1180)
fix(deps): update dependency vite to v8.3.6 (#412)
chore(deps): update dependency vite to v8 (#4)