Skip to content

Commit 7aaaf5c

Browse files
committed
Add CustomSocket
Extra closeCodes handling for Firefox see phoenixframework/phoenix#5735 (comment)
1 parent 0ae7344 commit 7aaaf5c

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

assets/js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
1919
import "phoenix_html"
2020
// Establish Phoenix Socket and LiveView configuration.
21-
import { Socket } from "phoenix"
21+
import CustomSocket from "./custom_socket"
2222
import { LiveSocket } from "phoenix_live_view"
2323
import topbar from "../vendor/topbar"
2424

2525
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
26-
let liveSocket = new LiveSocket("/live", Socket, {
26+
let liveSocket = new LiveSocket("/live", CustomSocket, {
2727
longPollFallbackMs: 2500,
2828
params: { _csrf_token: csrfToken },
2929
})

assets/js/custom_socket.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Socket } from "phoenix"
2+
3+
export default class CustomSocket extends Socket {
4+
// Adapted from https://github.com/phoenixframework/phoenix/tree/v1.7.14/assets/js/phoenix
5+
onConnClose = function (event) {
6+
let closeCode = event && event.code
7+
if (this.hasLogger()) this.log("transport", "close", event)
8+
9+
// Extra closeCodes handling for Firefox
10+
// see https://github.com/phoenixframework/phoenix/pull/5735#issuecomment-2167196419
11+
if (closeCode == 1000 || closeCode == 1001) this.channels.forEach((c) => c.leave())
12+
13+
this.triggerChanError()
14+
this.clearHeartbeats()
15+
if (!this.closeWasClean && closeCode !== 1000) {
16+
this.reconnectTimer.scheduleTimeout()
17+
}
18+
this.stateChangeCallbacks.close.forEach(([, callback]) => callback(event))
19+
}
20+
}

lib/playground_web/components/core_components.ex

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ defmodule PlaygroundWeb.CoreComponents do
155155
id="client-error"
156156
kind={:error}
157157
title={gettext("We can't find the internet")}
158-
phx-disconnected={delayed_show(".phx-client-error #client-error")}
158+
phx-disconnected={show(".phx-client-error #client-error")}
159159
phx-connected={hide("#client-error")}
160160
hidden
161161
>
@@ -612,19 +612,6 @@ defmodule PlaygroundWeb.CoreComponents do
612612
)
613613
end
614614

615-
# Workaround for "We can't find the internet" flashes on Firefox
616-
# https://github.com/phoenixframework/phoenix/issues/5102#issuecomment-1970738490
617-
def delayed_show(js \\ %JS{}, selector) do
618-
JS.show(js,
619-
to: selector,
620-
time: 1300,
621-
transition:
622-
{"delay-1000 transition-all transform ease-out duration-300",
623-
"opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95",
624-
"opacity-100 translate-y-0 sm:scale-100"}
625-
)
626-
end
627-
628615
def hide(js \\ %JS{}, selector) do
629616
JS.hide(js,
630617
to: selector,

0 commit comments

Comments
 (0)