Conflicting websockets between vite and @fastify/websocket
causing infinite redirects
#129
Open
2 tasks done
Prerequisites
Fastify version
4.24.3
Plugin version
5.0.6
Node.js version
20.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
22.04
Description
I have registered the plugin
@fastify/websocket
on my server and in development mode it is causing what is basically an infinite redirect when loading a page.This appears to be due to how the
@fastify/websocket
plugin is attempting to handle the upgradeGET 101
which is being initiated by thevite
HMR client on page loading.See this line in
@fastify/websocket
which is attempting to route any request upgrades on the server.https://github.com/fastify/fastify-websocket/blob/v8.3.1/index.js#L57
If I change
@fastify/vite
settings to not attach the HMR server to the current application server, it will correct this infinite redirect behavior.So it appears that the vite dev server has been configured to piggy back on the application server so that it can share https/http2 configurations.
568e178
That seems reasonable, but so does being able to easily use a plugin like
@fastify/websocket
and it seems reasonable for@fastify/websocket
to handle upgrade requests for all requests against the application server to which it is attached, so I'm not sure what the best fix might be.For my purpose it seems to work fine to add this to my projects
vite.config.js
false
is not technically a valid option forvite.server.hmr.server
but it will prevent the application server settings being merged in from@fastify/vite
where asserver: undefined
will not.With realtime applications being more and more common, having support for websockets would be very helpful so at the very least including a note on this behavior, I think, would be a good idea.
Happy to submit a PR once a direction is discussed.
Thanks!
Steps to Reproduce
@fastify/websocket
packagesnpm install @fastify/websocket
server.js
You can check out this codesandbox for an example:
Fastify Vite Kitchen Sink React Template
Expected Behavior
To be able to use blessed fastify plugins (like
@fastify/websocket
) on the application server without very special configuration settings.The text was updated successfully, but these errors were encountered: