We have 3 possible options:
- user not signed in
- user signed in
- user signed in as admin
Since our user area is significantly heavier (in terms of css), I would like to spare people that are not signed in to load the code.
Is it possible to change my css.scss to css.scss.erb & then add some helpers? For example:
Application.css.scss.erb
# include css files for non-logged in users
<% if signed_in? %>
# include css files for logged in users
<% if current_user.try(:staff_role?) %>
# include css files for admin users
<% end %>
<% end %>
This way, I prevent adding a second or a third http request for the user. Effective?
How would this affect rendering times? Would rails cache 3 different files?
I don’t think this will work very well. All your assets will get pre-compiled in production so your ERB won’t do what you’re expecting it to do. Any ERB in your assets needs to be “environment level” only and by that I mean things like paths that don’t depend on the current request’s state at all. You can use multiple style sheets though:
Then you include the appropriate style sheet in your views.
If for some reason you need per-user CSS then you’ll have to use
<style>elements in your views and inline it or serve the CSS through a controller so that you can reliably customize it on a per-request basis.