liveview-guidelines
Installation
SKILL.md
Phoenix LiveView Guidelines
General
- Never use the deprecated
live_redirectandlive_patchfunctions, instead always use<.link navigate={href}>and<.link patch={href}>in templates, andpush_navigateandpush_patchin LiveViews - Avoid LiveComponents unless you have a strong, specific need for them
- LiveViews should be named like
AppWeb.WeatherLive, with aLivesuffix. When adding routes, the default:browserscope is already aliased with theAppWebmodule, so you can just dolive "/weather", WeatherLive
LiveView Streams
-
Always use LiveView streams for collections instead of assigning regular lists to avoid memory ballooning and runtime termination:
- Basic append:
stream(socket, :messages, [new_msg]) - Reset stream:
stream(socket, :messages, [new_msg], reset: true)(e.g. for filtering) - Prepend:
stream(socket, :messages, [new_msg], at: -1) - Delete:
stream_delete(socket, :messages, msg)
- Basic append:
-
When using streams, the template must:
- Set
phx-update="stream"on the parent element with a DOM id - Consume
@streams.stream_nameand use the id as the DOM id for each child:
- Set