Skip to content

Preload <link> load/error listeners are never removed, retaining the preload scope #23920

Closed

Description

@bpuns

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

  1. npm install
  2. npm run build
  3. serve dist over HTTP (e.g. npx serve dist) and open it
  4. click mount — appends a <div> and triggers import('./panel.js'), which
    imports a CSS file, so __vitePreload inserts a <link rel="stylesheet">
  5. click unmount — the <div> is removed from the document
  6. DevTools → Memory → click Collect garbage twice
  7. 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

Activity

  1. added theissue type on Oct 28, 2026
  2. added a commit that references this issue on Oct 30, 2026
    8109efd
  3. locked and limited conversation to collaborators on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions