Skip to content

Commit

Permalink
Document sticky liveview bug fix in v1.0.1 (#3579)
Browse files Browse the repository at this point in the history
* Document sticky liveview bug fix in v1.0.1

* Update integration tests

* don't send url on join for sticky live views

---------

Co-authored-by: Steffen Deusch <[email protected]>
  • Loading branch information
josevalim and SteffenDE authored Jan 9, 2025
1 parent 9ee4478 commit 27587a2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ generated user module:
### Bug fixes
* Raise when duplicate DOM IDs are found when rendering a LiveView during tests to avoid undefined behaviour
* Fix live session verification causing logged errors, push_patch failures, and failed mounts when a cold deploy occurs
* Fix a bug where the `live_session`'s `on_mount` hooks would be called for sticky live views on connected mount. Now a `sticky` live view is consistently marked as `:not_mounted_at_router`

## 1.0.0 (2024-12-03) 🚀

Expand Down
4 changes: 3 additions & 1 deletion assets/js/phoenix_live_view/live_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ export default class LiveSocket {
DOM.all(document, `${PHX_VIEW_SELECTOR}:not([${PHX_PARENT_ID}])`, rootEl => {
if(!this.getRootById(rootEl.id)){
let view = this.newRootView(rootEl)
view.setHref(this.getHref())
// stickies cannot be mounted at the router and therefore should not
// get a href set on them
if(!DOM.isPhxSticky(rootEl)){ view.setHref(this.getHref()) }
view.join()
if(rootEl.hasAttribute(PHX_MAIN)){ this.main = view }
}
Expand Down
14 changes: 4 additions & 10 deletions lib/phoenix_live_view/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1558,21 +1558,15 @@ defmodule Phoenix.LiveView.Channel do

if Session.main?(session) do
# Ensure the session's LV module and live session name still match on connect.
# If the route has changed the LV module or has moved live sessions, the client
# will fallback to full page redirect to the current URL.
# If the route has changed the LV module or has moved live sessions (typically
# during a deployment), the client will fallback to full page redirect to the
# current URL.
case session_route(session, endpoint, url) do
%Route{view: ^view, live_session: %{name: ^session_name}} = route ->
{:ok, session, route, url}

# if we have a sticky LV, it will be considered a main with no live session
%Route{} when is_nil(session_name) ->
{:ok, session, nil, url}

# if we have a session, then it no longer matches and is unauthorized
%Route{} ->
{:error, :unauthorized}

nil ->
_ ->
{:error, :unauthorized}
end
else
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix_live_view/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Phoenix.LiveView.Session do
live_session_vsn: nil,
assign_new: []

def main?(%Session{} = session), do: !is_nil(session.router) and !session.parent_pid
def main?(%Session{} = session), do: session.router != nil and session.parent_pid == nil

def authorize_root_redirect(%Session{} = session, %Route{} = route) do
%Session{live_session_name: session_name, live_session_vsn: session_vsn} = session
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/support/issues/issue_3047.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end
defmodule Phoenix.LiveViewTest.E2E.Issue3047.Sticky do
use Phoenix.LiveView

def mount(_params, _session, socket) do
def mount(:not_mounted_at_router, _session, socket) do
items =
Enum.map(1..10, fn x ->
%{id: x, name: "item-#{x}"}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/support/issues/issue_3496.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ end
defmodule Phoenix.LiveViewTest.E2E.Issue3496.StickyLive do
use Phoenix.LiveView

def mount(_params, _session, socket) do
def mount(:not_mounted_at_router, _session, socket) do
{:ok, socket, layout: false}
end

Expand Down

0 comments on commit 27587a2

Please sign in to comment.