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.
111
111
112
-
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')).
That `:css` in the MIME type registry is one way to instruct Rails controller behavior with `respond_to`.
126
113
127
-
Support for CSS added a short time later on Feb 17, 2007 ([commit](https://github.com/rails/rails/commit/392c7f7314d196c54912a65981d79002d032f896 'Commit on Github').
114
+
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`:
That `:css` in the MIME type registry is one way to instruct Rails controller behavior with `respond_to`.
118
+
This controller action will respond differently for HTML and CSS requests. To demonstrate the difference, I've inserted two iframes for `ColorSchemesController#show` below:
134
119
135
-
Using `respond_to` in a controller allows you to define responses and logic based on the requested format. As a contrived example, imagine a `Product#index` action that returns 25 products for an HTML request, e.g. `/products`, and loads 250 products for an RSS request, e.g. `/products.rss`.
120
+
#### Iframe for HTML request
136
121
137
-
```ruby
138
-
class ProductsController < ApplicationController
139
-
def index
140
-
respond_to do |format|
141
-
format.html { @products = Product.limit(25) }
142
-
format.rss { @products = Product.limit(250) }
143
-
end
144
-
end
145
-
end
146
-
```
122
+
Below you‘ll see an `iframe` with `src` set to request the **Blue Chill** color scheme as `text/html`.
JavaScript not enabled? Go to the <%= link_to "color scheme demo", settings_color_scheme_path(custom_color_scheme_params) %>. Then come back when you’re done.
165
-
</noscript>
146
+
And we can use this endpoint in a stylesheet `<link>` tag just like any other static CSS URL:
A downside of moving our dynamic CSS to a separate controller action is that it requires an additional HTTP request. This might not be a big deal. But, for that same reason, we might also be able to take advantage of Rails conditional GET features to instruct HTTP proxies to cache the request if it hasn’t been modified:
Note the use of the `stale?` method here. This method will calculate a value for `Etag` or `Last-Modified` response headers and set the status to `304 Not Modified` if request headers match and the server doesn’t need to render anything.
168
157
169
-
Generating CSS dynamically from a controller offers several advantages:
158
+
> 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.
0 commit comments