Skip to content

Dev server doesn't receive SSE close event #12157

@abcd-ca

Description

@abcd-ca

Describe the bug

I am using server-sent events (SSE) and encountered an issue when running Vite dev server.

When a React client is connected to an express.js HTTP SSE endpoint via proxy config, the client should issue a close event that the server receives via res.on('close'...) but the event never fires. When I use vite to create a production build, the server event does get fired.

I have tried doing a low level implementation SSE implementation and also used a higher level library, better-sse. It was reproducible in both cases.

Reproduction

https://github.com/abcd-ca/vite-sse-issue-example

Steps to reproduce

Steps to Reproduce

Dev mode

Demonstrates SSE not closing connections when the browser window is closed or refreshed. You will see in the Chrome DevTools console that client connections are accumulating when you refresh the page which shouldn't happen.

  1. start the app using npm run dev
  2. visit, http://localhost:3000
  3. open the browser console
  4. refresh the page a few times and see the number of connections grow ("There are 1 person(s) here right now!" then "There are 2 person(s) here right now!"...)

Prod mode

Demonstrates SSE disconnect working as expected

  1. build the app using npm run build
  2. start the app using npm start
  3. visit, http://localhost:3001 (make sure you change https to http if your browser changes it automatically)
  4. open the browser console
  5. refresh the page a few times and see the number of connections remain at 1 ("There are 1 person(s) here right now!")

System Info

System:
    OS: macOS 12.6.1
    CPU: (8) arm64 Apple M1
    Memory: 97.45 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.19.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 110.0.5481.100
    Firefox: 100.0.1
    Safari: 16.1
  npmPackages:
    @vitejs/plugin-react: ^1.0.2 => 1.0.2
    vite: ^2.5.10 => 2.5.10

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vitep2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions