Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm trying to use SvelteKit with Cloudflare Pages, and as part of that I'd like to be able to develop locally using
wrangler pages dev
. I noticed that had an option to use--experimental-local
, which I added. When I ran the full commandwrangler pages dev --compatibility-date=2023-02-03 --experimental-local -- npm run dev
(and after upgrading Wrangler's Miniflare version to3.0.0-next.10
), I found that the Pages dev command crashed with the following error:I knew this was related to HMR, as it was the only websocket that my SvelteKit page opened. Curiously, the HMR websocket connection showed as having failed to open in my browser DevTools, with Firefox logging a "Failed to connect" error. However, a successful handshake was shown (the Firefox error came slightly later). After a bit of further digging, it appeared that both sides of the WebSocket pair were being closed with the code
1006
, a reserved code. Here start the digressions that were ultimately fruitless, included for completeness:svelte-hmr
(the HMR package that Svelte uses) could ever incorrectly send a1006
close code (they don't)sec-websocket-key
andsec-websocket-accept
headers (they matched)I then moved on to checking the connection with
curl
, and noticed something rather curious that I'd missed in the browser DevTools:The protocol
vite-hmr
was being sent, but the protocolvite-hmr, vite-hmr
was being received. This meant that the browser was rejecting the WebSocket connection, even though the handshake had "succeeded", and presumably closing the connection with1006
, in accordance with https://www.rfc-editor.org/rfc/rfc6455#section-11.3.4.After many (many) added
console.log
statements, I tracked down the offending chunk of code to the#webSocketExtraHeaders
property. When headers are requested from it (this.#webSocketServer.on("headers"
), a duplicateSec-WebSocket-Protocol
was being added (the only other extra headers present,Connection
andSec-WebSocket-Accept
, were already being filtered out). Thus ends my tale of woe—culminating in a one line change 😂.