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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews