- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 7.4k
 
Description
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.
- start the app using 
npm run dev - visit, http://localhost:3000
 - open the browser console
 - 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
- build the app using 
npm run build - start the app using 
npm start - visit, http://localhost:3001 (make sure you change https to http if your browser changes it automatically)
 - open the browser console
 - 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.10Used Package Manager
npm
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.