Skip to content

Commit 016d395

Browse files
lardcanoeSteffenDE
andauthored
Update comments to explain how to handle asset version change warnings (#4098)
* Update comments to explain how to handle asset version change warnings * Switch to spaces * Apply suggestions from code review --------- Co-authored-by: Steffen Deusch <[email protected]>
1 parent 936787f commit 016d395

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/phoenix_live_view.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,8 @@ defmodule Phoenix.LiveView do
12711271
version of the marked static files. It works by comparing the static paths
12721272
sent by the client with the one on the server.
12731273
1274+
Browser console logs will have messages starting with "LiveView asset version mismatch."
1275+
12741276
**Note:** this functionality requires Phoenix v1.5.2 or later.
12751277
12761278
To use this functionality, the first step is to annotate which static files
@@ -1303,8 +1305,17 @@ defmodule Phoenix.LiveView do
13031305
</div>
13041306
```
13051307
1308+
For larger projects, you can extract this into [a hook](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#on_mount/1):
1309+
1310+
# MyAppWeb.CheckStaticChanged
1311+
def on_mount(:default, _params, _session, socket) do
1312+
{:cont, assign(socket, static_changed?: static_changed?(socket))}
1313+
end
1314+
1315+
And then add it to the existing `live_view` macro in your `my_app_web.ex` file or add it as part
1316+
of your `live_session` hooks.
13061317
If you prefer, you can also send a JavaScript script that immediately
1307-
reloads the page.
1318+
reloads the page, but this will cause the client-side to lose all work in progress.
13081319
13091320
**Note:** only set `phx-track-static` on your own assets. For example, do
13101321
not set it in external JavaScript files:

priv/static/phoenix_live_view.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,6 +4329,11 @@ removing illegal node: "${(childNode.outerHTML || childNode.nodeValue).trim()}"
43294329
});
43304330
}
43314331
if (liveview_version !== this.liveSocket.version()) {
4332+
// Either the user has a mismatched version - for example using LiveView from npm instead of
4333+
// the bundled version from the hex package - or, more likely, the assets are stale because of a
4334+
// deployment. In that case, see
4335+
// https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#static_changed?/1
4336+
// for tips to address this.
43324337
console.warn(
43334338
`LiveView asset version mismatch. JavaScript version ${this.liveSocket.version()} vs. server ${liveview_version}. To avoid issues, please ensure that your assets use the same version as the server.`
43344339
);

0 commit comments

Comments
 (0)