workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: `wrangler dev` discards additional headers from WebSocket upgrade response

Open mrbbot opened this issue 3 years ago • 0 comments

What version of Wrangler are you using?

2.0.29

What operating system are you using?

Mac

Describe the Bug

If a worker responds with a WebSocket upgrade response containing additional headers, those headers will not be returned to the client.

Example

export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);
    if (url.pathname === "/") {
      return new Response(
        `<!DOCTYPE html>
        <html>
        <body>
        <script>
        const url = new URL("/ws", window.origin);
        url.protocol = url.protocol.replace("http", "ws");
        const webSocket = new WebSocket(url);
        webSocket.addEventListener("open", () => {
          console.log("open");
        });
        </script>
        </body>
        </html>
        `,
        { headers: { "Content-Type": "text/html; charset=utf-8" } }
      );
    } else if (url.pathname === "/ws") {
      const [client, worker] = Object.values(new WebSocketPair());
      worker.accept();
      return new Response(null, {
        status: 101,
        webSocket: client,
        headers: { "X-Key": "value" },
      });
    } else {
      return new Response(null, { status: 404 });
    }
  },
};

Running wrangler dev, visting http://localhost:8787 in a browser shows the following in the Firefox devtools network tab:

image

Publishing the worker and visiting the deployed URL shows the following instead:

image

mrbbot avatar Sep 11 '22 13:09 mrbbot