[Feature] How about server side rendered single file components?

Sometimes I wonder why we can’t have a standard for something like server side rendered single file components/pages in rails. Here are some frontend inspirations:

Often I think why we always need to split html, css, js, i18n into that many files, and then having to reference each other. Rails already removes a lot of this reference pain, but not all of it.

I could imagine something like this, but of course a lot more refined:

# app/views/animals.sfc.erb
 <%= @animals.each do |animal| %>
   <% tag.h1 animal, class: "background-#{animal.id}", onclick: happy %>
   <%= animal.name %> is a <%= t :beautiful %> animal
 <% end %>
# Why do we need to place ALL code in app/javascript ? Let's place some local code here too
const happy = () => { alert("the animal is happy, go to <%= animals_path %> to find out more") }
# of course, we need to handle all the edge cases of loading this correctly. And yes, I want to inject all the rails magic directly into my javascript responses.

# inherit from standard asset pipeline and allow local-only overrides
<%= @animals.each do |animal| %>
.background-<%= animal.id%> {
  background-color: <%= animal.color %>
  font-size: <%= animal.size%>px;
<% end %>
# inherit from standard i18n and allow local overrides
  beautiful: "wunderschön"

What your opinion on single file components? I think it would increase the chance of code duplication while introducing an easy way to handle edge cases easier. An easy version could be implemented using content_for() but I think that would be too limited.

Generally I would like to achieve more simplicity and not complexity, in this case by grouping all required code as much as possible.

1 Like

+1 I would love to see something like this and I even tried to propose something similar in the past.

1 Like

I’m thinking about this idea for some time now. I have small concern that Rails might be a limitation here, or bring more complexity to make it work. How about implementing it for plain Ruby?

If you’d like to brainstorm this, let me know. I would like to try at least.