Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping requests reach the user defined middlewares #13109

Closed
7 tasks done
gtm-nayan opened this issue May 6, 2023 · 1 comment · Fixed by #13117
Closed
7 tasks done

Ping requests reach the user defined middlewares #13109

gtm-nayan opened this issue May 6, 2023 · 1 comment · Fixed by #13117
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@gtm-nayan
Copy link
Contributor

Describe the bug

When the server shuts down, the client starts sending periodic ping requests to check if the server is back up and then reload. These requests reach the middlewares defined by the users but it should probably be handled by vite before it reaches any of these like the initial websocket upgrade request does. There is also no reliable way to identify these requests for the middlewares to ignore it themselves.

ref: sveltejs/kit#6910
ref: sveltejs/kit#7218

// A fetch on a websocket URL will return a successful promise with status 400,
// but will reject a networking error.
// When running on middleware mode, it returns status 426, and an cors error happens if mode is not no-cors
try {
await fetch(`${pingHostProtocol}://${hostAndPath}`, {
mode: 'no-cors',
})

Here it uses the no-cors mode so custom headers won't be sent along, can't use that to identify the ping request, but that can be worked around by abusing the Accept header, something like Accept: text/x-vite-ping and check that instead.

I could submit a PR, but can't tell which file would be responsible for intercepting it, is it ws.ts?

Reproduction

https://github.com/gtm-nayan/vite-ping-repro

Steps to reproduce

  1. Run pnpm install

  2. Run pnpm dev

  3. Open the page

  4. Turn off dev server

  5. Restart the dev server (ideally set a breakpoint on this line to avoid the clutter from unrelated requests)

  6. Observe that the custom middleware from vite.config.js will have first logged the request URL from the ping request

System Info

npmPackages:
    vite: ^4.3.5 => 4.3.5

Used Package Manager

pnpm

Logs

No response

Validations

@gtm-nayan gtm-nayan changed the title Ping requests reach the middleware Ping requests reach the user defined middlewares May 6, 2023
@bluwy bluwy added p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels May 8, 2023
@bluwy
Copy link
Member

bluwy commented May 8, 2023

We used to have a __vite_ping endpoint that the client will ping, but that was removed at #6819 and #8133, because it was changed to ping the HMR host instead of Vite's http server host. By default, both host are the same but some configuration can have custom setups.

I think it makes sense to handle this in the http server still, so adding a middleware here should be good enough. I'm also fine with the custom Accept header to recognize it.

@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants