Skip to content

Commit 0256515

Browse files
authored
Reload stale clients instead of rendering new markup with old assets (#288)
A LiveView client that reconnects across a deploy remounts against the new server code while still running the CSS and JS it loaded before the deploy, so new markup renders unstyled and new hooks are missing. The docker tags page showed this after #286: overlapping columns and bare copy-feedback icons. The asset tags already carry phx-track-static, but nothing acted on it. Enable cache_static_manifest in prod so assets are served under digested paths and static_changed?/1 has a manifest to compare against, and add a StaticReload on_mount hook to both live sessions that issues a full redirect to the current URL when the client's tracked assets are stale. Digested paths also fix asset caching itself: without the manifest, prod served /assets/app.css?vsn=d, which Plug.Static marks cacheable for a year even though its content changes every deploy. The favicon needs only_matching because ~p"/favicon.ico" now resolves to its digested filename, which the exact-match only: list would reject.
1 parent 2d0e8e8 commit 0256515

6 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎config/prod.exs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ config :bob,
44
tmp_dir: "/tmp",
55
persist_dir: "/persist"
66

7+
config :bob, BobWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
8+
79
config :logger, level: :info
810

911
config :sentry,

‎config/test.exs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ config :bob, Bob.Repo,
2929
config :bob, BobWeb.Endpoint,
3030
http: [ip: {127, 0, 0, 1}, port: 4002],
3131
secret_key_base: "tT9wQ2eR5yU8iO1pA4sD7fG0hJ3kL6zX9cV2bN5mQ8wE1rT4yU7iO0pA3sD6fG90",
32-
server: false
32+
server: false,
33+
cache_static_manifest_latest: %{
34+
"assets/app.css" => "assets/app-11111111111111111111111111111111.css",
35+
"assets/app.js" => "assets/app-22222222222222222222222222222222.js"
36+
}

‎lib/bob_web/endpoint.ex‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ defmodule BobWeb.Endpoint do
1616
plug(Bob.Plug.Forwarded)
1717
plug(Bob.Plug.Status)
1818

19+
# only_matching also serves the digested /favicon-<hash>.ico that
20+
# ~p"/favicon.ico" resolves to when the static manifest is loaded.
1921
plug(Plug.Static,
2022
at: "/",
2123
from: :bob,
2224
gzip: Mix.env() != :dev,
23-
only: BobWeb.static_paths()
25+
only: BobWeb.static_paths(),
26+
only_matching: ~w(favicon)
2427
)
2528

2629
if Mix.env() == :dev do

‎lib/bob_web/router.ex‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ defmodule BobWeb.Router do
4343
get("/oauth/callback", OAuthController, :callback)
4444
post("/oauth/logout", OAuthController, :logout)
4545

46-
live_session :default, on_mount: [{BobWeb.UserAuth, :mount_current_user}] do
46+
live_session :default,
47+
on_mount: [{BobWeb.UserAuth, :mount_current_user}, BobWeb.StaticReload] do
4748
live("/", JobsLive)
4849
live("/artifacts", ArtifactsLive)
4950
live("/docker", DockerTagsLive)
@@ -53,7 +54,8 @@ defmodule BobWeb.Router do
5354
scope "/", BobWeb do
5455
pipe_through([:browser, :require_authenticated_user])
5556

56-
live_session :authenticated, on_mount: [{BobWeb.UserAuth, :ensure_authenticated}] do
57+
live_session :authenticated,
58+
on_mount: [{BobWeb.UserAuth, :ensure_authenticated}, BobWeb.StaticReload] do
5759
live("/request", RequestLive)
5860
end
5961
end

‎lib/bob_web/static_reload.ex‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule BobWeb.StaticReload do
2+
import Phoenix.LiveView
3+
4+
# A client that reconnects across a deploy still runs the CSS and JS it
5+
# loaded before the deploy, so markup from the new server renders unstyled
6+
# and new hooks are missing. A full redirect makes the browser reload the
7+
# page and fetch the current assets.
8+
def on_mount(:default, _params, _session, socket) do
9+
if connected?(socket) and static_changed?(socket) do
10+
{:cont,
11+
attach_hook(socket, :static_reload, :handle_params, fn _params, uri, socket ->
12+
{:halt, redirect(socket, to: path_with_query(URI.parse(uri)))}
13+
end)}
14+
else
15+
{:cont, socket}
16+
end
17+
end
18+
19+
defp path_with_query(%URI{path: path, query: nil}), do: path
20+
defp path_with_query(%URI{path: path, query: query}), do: path <> "?" <> query
21+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule BobWeb.StaticReloadTest do
2+
use BobWeb.ConnCase
3+
4+
import Phoenix.LiveViewTest
5+
6+
# The digests here match cache_static_manifest_latest in config/test.exs.
7+
test "redirects for a full page load when the client's assets are stale", %{conn: conn} do
8+
conn =
9+
put_connect_params(conn, %{
10+
"_track_static" => [
11+
"http://localhost:4002/assets/app-00000000000000000000000000000000.css",
12+
"http://localhost:4002/assets/app-00000000000000000000000000000000.js"
13+
]
14+
})
15+
16+
assert {:error, {:redirect, %{to: "/docker?tag=1.18"}}} = live(conn, ~p"/docker?tag=1.18")
17+
end
18+
19+
test "mounts when the client's assets are current", %{conn: conn} do
20+
conn =
21+
put_connect_params(conn, %{
22+
"_track_static" => [
23+
"http://localhost:4002/assets/app-11111111111111111111111111111111.css",
24+
"http://localhost:4002/assets/app-22222222222222222222222222222222.js"
25+
]
26+
})
27+
28+
assert {:ok, _view, _html} = live(conn, ~p"/docker")
29+
end
30+
31+
test "mounts when the client does not track statics", %{conn: conn} do
32+
assert {:ok, _view, _html} = live(conn, ~p"/docker")
33+
end
34+
end

0 commit comments

Comments
 (0)