devnacho

devnacho

Adding processed styles from stylesheet to HTML template

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!

Most Liked

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?

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>

Last Post!

devnacho

devnacho

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.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement