devnacho

devnacho

Hi there! :grinning:

For SEO purposes, I’m trying to improve loading performance by not having render blocking stylesheets and loading critical styles inside <style> tags directly in the HTML.

Does anybody have any idea or recommendation on how to put my webpack-processed stylesheet inside <style> tags in the HTML layout.

Any help would be really appreciated!

Showing Posts 1 to 6

hubertlepicki

hubertlepicki

You can read and output compiled app.css file (or other target if you have one). This should work in development and in prod, also with Erlang releases. In your layout or template do:

  <style type="text/css">
    <%= {:safe, File.read!(Path.join(:code.priv_dir(:ui), "static/css/app.css"))} %>
  </style>

And it will output the compiled CSS in place, provided your compiled file ends up in priv/css/app.css and that your application name is :ui. Replace these with your own if needed :slight_smile:

idi527

idi527

Is it read once during compilation or every time the template is invoked / rendered?

devnacho

devnacho OP

I’m exploring loading it as Module attributes so it isn’t read every time the template is invoked.

idi527

idi527

If you load the contents into a module attribute, it’s read from the filesystem only once during the compilation.

Oh, I misunderstood you. Ignore me.


I’d put it in a function instead of a module attribute though. This way you would be able to invoke it from other modules as well, not just within the module where you defined the module attribute.

defmodule Web.CSSView do
  app_css = File.read!(Path.join(:code.priv_dir(:your_app_name), "static/css/app.css"))
  def inline_css(:app) do
    {:safe, unquote(app_css)}
  end
end

and then in any template

<style type="text/css"><%= Web.CSSView.inline_css(:app) %></style>
hubertlepicki

hubertlepicki

Every time it is rendered. OS should keep it in cache if that file is being read over and over again, I would probably not bother too much with making it module attribute.

devnacho

devnacho OP

Thanks for the replies.

I ended up getting the file from priv/static and I used GitHub - melpon/memoize: A method caching macro for elixir using CAS on ETS. · GitHub to memoize the contents of the file and avoid opening the file with each rendering of the template.

— 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
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
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
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews