skip to content

In a Vite dev server, why can opening a lazy route log 'optimized dependencies changed. reloading' and reload the page, and what stops it?

level: middleimportance: should knowfreq 12%

answer

  1. what the start-up scan could see
  2. an import a plugin adds
  3. the optimizer runs again
  4. served optimized files changed
  5. optimizeDeps.include and optimizeDeps.entries

basics

~20 s

The route's module imports a dependency the start-up scan never recorded, so Vite re-runs the optimizer when the route is first requested; if that changes optimized files the page already loaded, it reloads. Listing the dependency in optimizeDeps.include prevents it.

solid answer

~50 s

At start-up Vite scans its entries (every `.html` file by default, or the globs in `optimizeDeps.entries`) for bare imports. With `optimizeDeps.holdUntilCrawlEnd` at its default `true`, it also holds the first results until the static imports reached on cold start have been crawled. An import the scan cannot see, such as one a plugin adds in its transform or one in a file the scanned entries never reach, turns up only when the browser requests the lazily loaded route and Vite transforms it, which is after the cold-start crawl has ended. Vite then re-runs the optimizer over old and new dependencies. If the optimized files already served keep their hashes, nothing reloads. If they changed, for example because shared chunks were split differently, Vite invalidates its module graph, logs `optimized dependencies changed. reloading` and sends a full reload. To stop it, add the package to `optimizeDeps.include` or widen `optimizeDeps.entries`.

code

typescript · 9 lines
typescript
// vite.config.ts
import { defineConfig } from 'vite'

export default defineConfig({
  optimizeDeps: {
    // bundled at start-up although the scan cannot see the import
    include: ['rich-notes-editor'],
  },
})

go deeper

for a junior

Recall that the reload comes from the dependency optimizer meeting a package it had not bundled yet, not from an edit to your own code.

for a middle

Explain the sequence: the start-up scan, the cold-start crawl held by holdUntilCrawlEnd, an import found only when the route is transformed, a re-run, and a reload only when served optimized files changed.

for a senior

Diagnose from the terminal: read the optimized-dependency lines and the include tip, check whether a plugin injects the import or the entries are too narrow, and fix it in config rather than by clearing caches.

for a principal

Decide how explicit a shared config should be: a curated include list removes cold-start reloads for every app but must track dependency changes, while noDiscovery makes every omission fail loudly.

## The symptom The library network's events calendar has a lazily loaded route with a rich-text editor. The first time a developer opens that route in a dev session, the terminal reports that a dependency was optimized, then prints `optimized dependencies changed. reloading`. The browser reloads the whole page and any half-filled form is lost. The second visit is fine. Your code did not fail a hot update: the reload comes from the **dependency optimizer** adjusting to a package it had not bundled yet. ## How Vite finds dependencies 1. **The scan at start-up.** With no valid cache, Vite crawls your source from its entries. By default those are all `.html` files in the project, skipping `node_modules`, `build.outDir`, `__tests__` and `coverage`. If you set `optimizeDeps.entries`, its globs replace that default. The scan follows the imports it can read in the source and records each bare import. 2. **The crawl on cold start.** As the browser requests the first modules, Vite transforms them and sees their imports. With `optimizeDeps.holdUntilCrawlEnd: true`, the default, Vite holds the first optimizer results until those static imports have been crawled. A dependency found inside that window is folded into the first run before the browser executes anything. 3. **Discovery afterwards.** A lazily loaded route is not requested until someone navigates to it, so its module is transformed after the cold-start crawl has finished. If the scan did not record the editor package, this is the first time Vite meets it. What the start-up scan misses: - an import **added by a plugin's transform**. The scan reads source as written, without your Vite plugins' transforms, so, as Vite's guide puts it, Vite "can only discover it after the file is requested by the browser and transformed"; - an import in a file the scan never reaches from its entries, for instance because `optimizeDeps.entries` lists too little. ## What the re-run decides When a new dependency appears after the first results were released, Vite waits briefly in case more arrive, then runs the optimizer again over the old and new dependencies together. The outcome depends on what the run produced. | Result of the re-run | What the browser experiences | |---|---| | Optimized files already served keep the same hashes | No reload; the new dependency is simply served | | Their content changed, e.g. shared chunks were split differently | Module graph invalidated, full page reload | | A dependency's CommonJS interop came out differently | Full page reload, plus a warning suggesting `optimizeDeps.needsInterop` | Vite reloads instead of swapping because the modules already running were transformed against the old optimized files. Their cached transforms point at stale locations, so Vite invalidates every one of them and sends a `full-reload` message to the page. ## How to stop it 1. **List the dependency.** `optimizeDeps.include: ['rich-notes-editor']` makes the optimizer bundle it at start-up with everything else. When this happens Vite prints a tip that names the packages to add "to avoid a full page reload during cold start". 2. **Fix the entries.** If the scan misses whole files, give `optimizeDeps.entries` globs, relative to the project root, that reach them. 3. **Go fully explicit only if you must.** `optimizeDeps.noDiscovery: true` turns automatic discovery off, so only `include` is optimized and every CommonJS-only dependency has to be listed. 4. **Tune `holdUntilCrawlEnd` last.** Leave it `true` while discovery is incomplete. The docs recommend `false` only once the scan plus `include` find every dependency, so the browser can process more requests in parallel. ## Reading the terminal The optimizer narrates what it does, and the lines tell you which case you are in: - **`dependency optimized: rich-notes-editor`** (or the plural form) names the packages a re-run added. - **`optimized dependencies changed. reloading`** means the re-run changed files the page had already loaded, so a full reload followed. - **`tip: consider adding … to optimizeDeps.include`** lists the packages found after the start-up scan, which are your candidates for `include`. - **`Mixed ESM and CJS detected in …`** suggests `optimizeDeps.needsInterop` for packages whose interop came out differently between runs. If you see the tip but no reload line, discovery was late but harmless this time. ## What it is not - **Not a cache problem.** Deleting `node_modules/.vite` makes the next start scan again, and the scan misses the same import for the same reason. - **Not the hot-update client's own reloads.** An `index.html` edit, or an update no module accepts, also reloads the page, but for reasons that have nothing to do with the optimizer. - **Not certain to happen.** A re-run that leaves the served files unchanged needs no reload, so one dependency can be added silently while another forces a reload.

  • Why doesn't deleting `node_modules/.vite` fix the reload for good?
    Deleting the cache only makes the next start scan again, and the scan misses the same import for the same reason. The fix has to change what the optimizer knows at start-up: list the package in `optimizeDeps.include`, or widen `optimizeDeps.entries` so the scan reaches the file.
  • When would you set `optimizeDeps.holdUntilCrawlEnd` to `false`?
    When the scan plus your `include` list already find every dependency. Holding the results until the cold-start crawl ends exists to avoid reloads for dependencies found during that crawl. If none are found there, releasing early lets the browser process more requests in parallel. With discovery still incomplete, turning it off swaps a faster first load for more reloads.
  • What does `optimizeDeps.noDiscovery: true` change?
    It turns automatic discovery off, so only the packages in `optimizeDeps.include` are optimized, and a CommonJS-only dependency must be listed there or it reaches the browser unconverted. With `include` left empty as well, it switches the optimizer off entirely, which is what the docs offer in place of the deprecated `optimizeDeps.disabled`.

  • Every dependency discovered after start-up forces a full page reload.
  • The reload means a component in the route failed its hot update.
  • Deleting node_modules/.vite stops the late-discovery reload for good.
  • optimizeDeps.exclude is how you make a dependency bundle at start-up.
  • Setting holdUntilCrawlEnd to false prevents reloads caused by late discovery.