fresh icon indicating copy to clipboard operation
fresh copied to clipboard

fix: handle websocket connections using experimental deno:request event

Open CertainLach opened this issue 5 months ago • 6 comments

Fixes: #3350

Requires deno with this PR: https://github.com/denoland/deno/pull/30746

Context:

  • https://github.com/denoland/fresh/issues/3350#issuecomment-3294489536
  • https://github.com/denoland/fresh/issues/3350#issuecomment-3294522965

CertainLach avatar Sep 16 '25 02:09 CertainLach

@marvinhagemeister I'm really interested in this fix and would also like to help. You mentioned (https://github.com/denoland/fresh/issues/3554#issuecomment-3423562068) that this change will likely not be merged since

it deviates from the web platform

What would be your approach if intercepting the request is not the correct way.

DontMash avatar Oct 29 '25 12:10 DontMash

@DontMash Create a Fresh cli that calls vite as a middleware. That's the proper solution. This PR can't be merged because it depends on another PR being merged into Deno itself. The PR for Deno is very unlikely to be merged.

marvinhagemeister avatar Oct 29 '25 13:10 marvinhagemeister

Create a Fresh cli that calls vite as a middleware. What do you mean by cli and how to use vite as a middleware?

Should App provide a websocket command to define a websocket route and use vite as a middleware?

As far as I know, the vite middleware can only be accessed when creating a server or configuring a plugin. That is why I also assumed intercepting the request in the plugin is the best solution.

DontMash avatar Nov 01 '25 23:11 DontMash

Should App provide a websocket command to define a websocket route and use vite as a middleware?

You don't need to do that with requests being Deno requests, which is default in Fresh

The problem is that vite starts a node.js http server instead, and you can't properly convert node requests back to deno requests, because node.js handles websockets differently

It is possible however to create a vite dev middleware for fresh (I.e app.use(viteMiddleware(viteConfig))), that performs compilation, and responds with built client files from vite's output MemFS (or however it is called there).

CertainLach avatar Nov 01 '25 23:11 CertainLach

You don't need to do that with requests being Deno requests, which is default in Fresh

I know, but you still need to handle the upgrade yourself. The question was also closer related to meaning of cli in this context

The problem is that vite starts a node.js http server instead, and you can't properly convert node requests back to deno requests, because node.js handles websockets differently

I also tried to convert Nodejs websocket requests to Deno myself.

It is possible however to create a vite dev middleware for fresh (I.e app.use(viteMiddleware(viteConfig))), that performs compilation, and responds with built client files from vite's output MemFS (or however it is called there).

This would mean that just the dev_server would not be part of the plugin anymore, but rather be implemented as a middleware for fresh?

DontMash avatar Nov 02 '25 01:11 DontMash

This would mean that just the dev_server would not be part of the plugin anymorer, but rather be implemented as a middleware for fresh?

Exactly. It is not quite a middleware, because of how fresh handles build cache updates, but I already did that for webpack and the resulting setup works very well for me, it should work similarly for vite.

image

CertainLach avatar Nov 02 '25 01:11 CertainLach