MatijaL
LiveView and meta tags on first request?
Hi,
In a LiveView app, I want to setup different meta tags for some pages. For example, on a user profile page, I want to add user specific meta title and description.
In mount function, I set :profile_meta to true
def mount(_params, %{"user_token" => user_token} = _session, socket) do
current_user = Accounts.get_user_by_session_token(user_token)
{:ok,
socket
|> assign(:current_user, current_user)
|> assign(:profile_meta, true)}
end
In the head section I have the following:
<%= if @profile_meta do %>
<meta property="og:title" content={"#{@profile.name}"} />
<meta property="og:description" content={"#{@profile.about} "} />
<% else %>
...
When the profile page is opened in the browser, meta tags are loaded properly in the head tag as they should be.
The problem is that when sharing those page on social media, the data is not loaded because those websites don’t use javascript when scraping meta tags so mount function doesn’t happen and profile_meta: true is never assigned.
What would be the best way to handle this?
Most Liked
sodapopcan
There are two stages to LiveView mounting, dead render for standard http and live render for websocket connection. mount is called each time and there’s no JS at play on the dead render, so you should be good.
thomas.fortes
I would also recommend the use of some site like https://www.opengraph.xyz/ to test if it isn’t a cache issue on the side of the social network.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








