mpope

mpope

I am trying to cache static web pages in ETS, and using render_to_iodata does not seem to include the layout. I was wondering the proper way to do this. Here is how I am currently using it in my controller:

def post(conn, _param) do
  rendered_page = Phoenix.View.render_to_iodata(BlogWeb.LayoutView, "app.html", 
      %{  
        conn: conn,
        view_module: BlogWeb.PostView,
        view_template: "post.html"
      })  
  html(conn, rendered_page)
end

What happens is that this spins forever and nothing gets loaded, and nothing is really logged to the console.

My page.html.eex:

Hello, world!

And the layout/app.html.eex (shortened) is:

<h1>Welcome!</h1>
<%= render @view_module, @view_template, assigns %>

I have a barebones view for each. I am using the default layout.

Showing Posts 1 to 2

jola

jola

Since you’re not getting any errors it’s hard to say what’s wrong. I’ve never tried to do it that way, but I can suggest an alternative solution. You can cache rendered content by using Plug.Conn.register_before_send/2. Here’s some untested code

def post(conn, _param) do
  conn
  |> Plug.Conn.register_before_send(cache/1)
  |> render("post.html")
end

def cache(conn) do
  content = :erlang.iolist_to_binary(conn.resp_body)
  :ets.insert(:table, {you_pick_your_key, content})
  conn
end

The reason you want to coerce the iodata to binary is that based on experience the ETS memory usage will balloon if you don’t.

This solution also means you can create a Plug responsible for checking the cache and caching responses, and add it to a pipeline. I have an example of that here cache_example/lib/cache_example_web/plugs/item_cache2.ex at master · joladev/cache_example · GitHub

Note that the example is specific to my use case, you’d want to adjust it to yours :slight_smile:

mpope

mpope OP

This seems much more idomatic. Thank you very much!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

Potions
A new screencast on live LiveView updates with PubSub, the Oban Web dashboard, and smarter retries. Part 2 of 2. Screencast + full walkth...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews