maltoe

maltoe

LiveView + WebComponents re-render issue

Hello :wave:

We have a small table-based index page based on LiveView, which displays (among other data) some timestamps in “relative” format using <relative-time> from GitHub - github/relative-time-element: Web component extensions to the standard <time> element. · GitHub.

This works great on first render but as soon as the table content is updated via the websocket, the time elements are re-rendered with their “fallback” content (the timestamp).

relative_time_bug

The markup is generated using this function:

def relative_time(datetime)
  {:ok, humanized} = MyApp.Cldr.DateTime.to_string(datetime, format: :short)
  content_tag(:"relative-time", humanized, datetime: to_string(datetime))
end

Does anyone have an idea? I’m pretty sure the web components aren’t initialized properly (i.e. some JS doesn’t run), but I wonder why that is?

Best,
malte

Marked As Solved

maltoe

maltoe

phx-update: "ignore" does this trick, though I don’t understand why.

https://github.com/phoenixframework/phoenix_live_view/issues/285

Also Liked

kelvinst

kelvinst

Well, basically what you want to do is to create a phx-hook to re run the time-elements code when your nodes are updated by the websocket. Checkout the client hooks docs

kelvinst

kelvinst

Hmmmmm, I’m not really sure, but I suspect that placing phx-update="ignore" on whatever tag is just going to guarantee that tag or its contents is not going to be changed anymore, or at least that is what I would expect to happen. But as far as I understood, you marked the individual datetime elements with that, and not the table that contains them, and what’s changing when you click to reorder is that table, not the actual date time elements, they stay the same, they are just moved around.

maltoe

maltoe

Yes, the phx-update="ignore" attributes went on the <relative-time> elements.

Looking at the ws packages it seems that LV is sending the entire table with each re-ordering, so I suppose the “moving around” of elements you describe is magic that happens in morphdom. That is quite impressive.

Thanks for your help!

Last Post!

maltoe

maltoe

:thinking: hmm I noticed something: When I manually (in the inspector) add an attribute to the <relative-time> element and then re-order the table, the attribute sticks with the element in that row, which leads me to believe morphdom is actually smart enough to replace only the <relative-time>'s content instead of re-creating the DOM node. Which would also explain why the JS doesn’t convert the timestamp (it is triggered when the web component is “connected” relative-time-element/src/relative-time-element.ts at 40f3d1abb9a1271607182704fa949b5867f94496 · github/relative-time-element · GitHub, but this event doesn’t fire). However, I now wonder even more how phx-update=ignore fixes it in this case. The content of these elements is clearly updated (I see the content change), but they are now correctly shown in relative time.

just for completeness: Tried it with paginated result set again (so elements are destroyed and re-created) and it works. The ordering link is a live_patch, if that matters.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement