Skip to content

Commit 2bf5ab9

Browse files
authored
copy join flash when falling back to redirect (#3764)
* copy join flash when falling back to redirect Fixes #3686. * add e2e test * actually put test liveview in separate live_session
1 parent f7882b0 commit 2bf5ab9

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

assets/js/phoenix_live_view/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ export default class View {
850850
return
851851
} else if(resp.reason === "unauthorized" || resp.reason === "stale"){
852852
this.log("error", () => ["unauthorized live_redirect. Falling back to page request", resp])
853-
this.onRedirect({to: this.root.href})
853+
this.onRedirect({to: this.root.href, flash: this.flash})
854854
return
855855
}
856856
if(resp.redirect || resp.live_redirect){
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
defmodule Phoenix.LiveViewTest.E2E.Issue3686.ALive do
2+
use Phoenix.LiveView
3+
4+
def render(assigns) do
5+
~H"""
6+
<h1>A</h1>
7+
<button phx-click="go">To B</button>
8+
9+
<div id="flash">
10+
{inspect(@flash)}
11+
</div>
12+
"""
13+
end
14+
15+
def handle_event("go", _unsigned_params, socket) do
16+
{:noreply, socket |> put_flash(:info, "Flash from A") |> push_navigate(to: "/issues/3686/b")}
17+
end
18+
end
19+
20+
defmodule Phoenix.LiveViewTest.E2E.Issue3686.BLive do
21+
use Phoenix.LiveView
22+
23+
def render(assigns) do
24+
~H"""
25+
<h1>B</h1>
26+
<button phx-click="go">To C</button>
27+
28+
<div id="flash">
29+
{inspect(@flash)}
30+
</div>
31+
"""
32+
end
33+
34+
def handle_event("go", _unsigned_params, socket) do
35+
{:noreply, socket |> put_flash(:info, "Flash from B") |> redirect(to: "/issues/3686/c")}
36+
end
37+
end
38+
39+
defmodule Phoenix.LiveViewTest.E2E.Issue3686.CLive do
40+
use Phoenix.LiveView
41+
42+
def render(assigns) do
43+
~H"""
44+
<h1>C</h1>
45+
<button phx-click="go">To A</button>
46+
47+
<div id="flash">
48+
{inspect(@flash)}
49+
</div>
50+
"""
51+
end
52+
53+
def handle_event("go", _unsigned_params, socket) do
54+
{:noreply, socket |> put_flash(:info, "Flash from C") |> push_navigate(to: "/issues/3686/a")}
55+
end
56+
end

test/e2e/test_helper.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ defmodule Phoenix.LiveViewTest.E2E.Router do
114114
pipeline :browser do
115115
plug :accepts, ["html"]
116116
plug :fetch_session
117+
plug :fetch_live_flash
117118
plug :protect_from_forgery
118119
plug :put_root_layout, html: {Phoenix.LiveViewTest.E2E.Layout, :root}
119120
end
@@ -163,12 +164,22 @@ defmodule Phoenix.LiveViewTest.E2E.Router do
163164
live "/3656", Issue3656Live
164165
live "/3658", Issue3658Live
165166
live "/3684", Issue3684Live
167+
live "/3686/a", Issue3686.ALive
168+
live "/3686/b", Issue3686.BLive
166169
live "/3709", Issue3709Live
167170
live "/3709/:id", Issue3709Live
168171
live "/3719", Issue3719Live
169172
end
170173
end
171174

175+
live_session :other, layout: {Phoenix.LiveViewTest.E2E.Layout, :live} do
176+
scope "/issues", Phoenix.LiveViewTest.E2E do
177+
pipe_through(:browser)
178+
179+
live "/3686/c", Issue3686.CLive
180+
end
181+
end
182+
172183
live_session :navigation, layout: {Phoenix.LiveViewTest.E2E.Navigation.Layout, :live} do
173184
scope "/navigation" do
174185
pipe_through(:browser)

test/e2e/tests/issues/3686.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const {test, expect} = require("../../test-fixtures")
2+
const {syncLV} = require("../../utils")
3+
4+
// https://github.com/phoenixframework/phoenix_live_view/issues/3686
5+
test("flash is copied across fallback redirect", async ({page}) => {
6+
await page.goto("/issues/3686/a")
7+
await syncLV(page)
8+
await expect(page.locator("#flash")).toHaveText("%{}")
9+
10+
await page.getByRole("button", {name: "To B"}).click()
11+
await syncLV(page)
12+
await expect(page.locator("#flash")).toContainText("Flash from A")
13+
14+
await page.getByRole("button", {name: "To C"}).click()
15+
await syncLV(page)
16+
await expect(page.locator("#flash")).toContainText("Flash from B")
17+
18+
await page.getByRole("button", {name: "To A"}).click()
19+
await syncLV(page)
20+
await expect(page.locator("#flash")).toContainText("Flash from C")
21+
})

0 commit comments

Comments
 (0)