How to get current url for og:url in Phoenix LV?

In Laravel I do this:

<meta property="og:url" content="<?php echo Request::url(); ?>" />

What should I do in Phoenix 1.6?

<meta property="og:url" content="??????????" />

to pass the correct url to the Open Graph url attribute?

1 Like


<meta property="og:url" content="<%= current_url(conn) %>" />

doesn’t work.

That’s to be expected. Phoenix.Controller is not imported in views by defaul. You’ll need to use Phoenix.Controller.current_url(conn)


Will this import it slow down my website?

What is the preferred way of doing this thing in Phoenix. I mean that FAcebook and Twitter grab the stuff correctly?

By the way I am still getting an error:

What am I doing wrong?

The file I am editing is root.html.heex

What you wrote is not valid heex syntax. It needs to be content={Phoenix.Controller.current_url(@conn)}.

1 Like

When I do that, this is what I see in the source code in browser:


instead of the url.


Because you didn’t use the syntax I showed, but you have additional "…" on the attribute.

1 Like

Oh, thanks. those “” were the culprit. Now it works.

A little caution here: root.html.heex is not updated across live_patch/2 or live_redirect/2 so If you want to change og:url you have to use a plain link. Only the page title is special cased to be patched in liveview:

Systems using meta tags usually don’t execute JS anyways. Even the google crawler afaik only does limited js interaction.

1 Like

What about <meta name="description"

I plan to use different meta tag description for each page.

How to make it reload/patch on each websocket page change?

@LostKobrakai is right. It does not matter if the og metas are wrong in the current browser session. The og meta is only used in a shared link, where the rendering is static anyway.

OK, so I won’t stress about it then.