You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Rails `mime_types.rb`, we can see the registered MIME types with each of their recognized extensions.
123
+
In Rails `mime_types.rb`, we can see the registered MIME types with each of their recognized extensions. Here‘s the original `mime_types.rb`, added to Rails on Dec 2, 2006 ([source](https://github.com/rails/rails/blob/5410f2cb74737bd6d96c226230c2b9c2bfe1d80b/actionpack/lib/action_controller/mime_types.rb 'Source code on Github')).
Support for CSS was added a short time later on Feb 17, 2007 ([commit](https://github.com/rails/rails/commit/392c7f7314d196c54912a65981d79002d032f896 'Commit on Github')).
138
+
139
+
```diff:{"show_header": false}
140
+
+ Mime::Type.register "text/css", :css
141
+
```
124
142
125
-
That `:css` in the MIME type registry is one way to instruct Rails controller behavior with `respond_to`.
143
+
We can take advantage of these registered MIME types to instruct Rails controller behavior with `respond_to`.
126
144
127
145
Using `respond_to` in a controller allows you to define responses and logic based on the requested format. Here‘s how I take advantage of this behavior in `ColorSchemesController#show`:
This controller action will respond differently for HTML and CSS requests. To demonstrate the difference, I've inserted two iframes for `ColorSchemesController#show` below:
149
+
This controller action will respond differently for HTML and CSS requests. Note the `format.html` and `format.css` blocks describe alternate results. To demonstrate the difference, I've inserted two iframes for `ColorSchemesController#show` below:
132
150
133
151
#### Iframe for HTML request
134
152
@@ -162,7 +180,7 @@ We can use this endpoint in a stylesheet `<link>` tag just like any other static
A downside of moving our dynamic CSS to a separate controller action is that it requires an additional HTTP request. This could be an issue for a highly interactive user experience. But on the other hand, using a separate request allows you to take advantage of Rails [conditional GET features](https://guides.rubyonrails.org/v3.2/caching_with_rails.html#conditional-get-support)
183
+
One downside of moving our dynamic CSS to a separate controller action is that it requires an additional HTTP request. This could be an issue for a highly interactive user experience. But on the other hand, using a separate request allows you to take advantage of Rails [conditional GET features](https://guides.rubyonrails.org/v3.2/caching_with_rails.html#conditional-get-support)
166
184
167
185
> Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.
168
186
>
@@ -176,6 +194,15 @@ This method will calculate a value for `Etag` or `Last-Modified` response header
176
194
177
195
In short, dynamic CSS combined with a conditional GET allows you to leverage put Ruby logic behind your stylesheet link tags in a performant manner.
178
196
197
+
## Which approach is right for you?
198
+
199
+
There‘s no one right answer here but consider the tradeoffs. Both work well with Hotwire; just like any other element, our style tags and stylesheet link tags can be nested inside a Turbo Frame or can be manipulated with a Turbo Stream if we need some interactivity. With a `<style>` tag, no extra requests to your Rails endpoints are needed to render the page. Separating dynamic css into a controller action can help address bandwidth as response body length as your dynamic styles grow more numerous.
200
+
201
+
My thought process is usually:
202
+
203
+
- start with dynamic css straight into HTML within a `<style>`
204
+
- move to a controller action when more flexibility is needed or when HTTP caching features are desired
205
+
179
206
## Recap
180
207
181
208
Rails provides us with all sorts of [sharp knives](https://rubyonrails.org/doctrine#provide-sharp-knives) especially it comes to dynamic rendering in various formats, like CSS.
0 commit comments