Skip to content

Dependencies optimization in ssr Environments during dev request handling can cause inconsistent states #19323

Description

@dario-piotrowicz

Describe the bug

The problem consists in the fact that during local development dependency optimization (optimizeDeps) can happen, in an ssr environment, while a request is being processed.

This can be problematic as this allows modules to be reloaded while code is still being evaluated/run, meaning that the two can get out of sync.

Consider this very simplified example of such a scenario:

  • the vite dev server receives a request and it starts processing the request in an ssr environment
  • a dependency A is encountered and optimized
  • some global state gets saved based on the dependency initialization
  • a new dependency B is encountered (lazily)
  • so it gets optimized and a fullReload() occurs (which invalidates the environmen's whole module graph)
  • some code uses dependency A + the environment state with the assumption that the two match

The last point is where the issue occurs, the environment's state was modified, then the dependency A was reloaded and now the two might not match anymore, and this can be problematic for application.

The minimal reproduction linked in this issue should explain things further, it also adds some details and context as to why this is something that can become a common issue for many consumers/authors of ssr environments.

Reproduction

https://github.com/flarelabs-net/vite-dev-ssr-prebundling-mid-flight-issue-repro

Steps to reproduce

No response

System Info

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 8.51 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v20.11.0/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
    pnpm: 9.12.2 - ~/Library/pnpm/pnpm
    bun: 1.0.6 - ~/.bun/bin/bun
  Browsers:
    Chrome: 132.0.6834.111
    Safari: 18.2

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions