Given I have rails controller which accesses DB to retrieve data and render JSON response using serializer, what conditions must be met to make rails respond with 304?
If it has to compare previous response to currently retrieved, what kind of comparison is it?
The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource.
The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
In Rails, the head method is shorthand for "respond only with this status, headers, and an empty body." The head method takes a symbol corresponding to a status, in this case :no_content for "204."
The magic happens in Rack::ETag
and Rack::ConditionalGet
. (Mine are located at $GEM_HOME/rack-1.5.5/lib/rack/conditionalget.rb
and $GEM_HOME/rack-1.5.5/lib/rack/etag.rb
.)
These are middlewares.
Rack::ETag
makes a digest out of the body (the very same body which gets returned by the rack function @app.call
) and sets the response ETag
header to this value. For Rails 4.0, at least, it's an MD5 hex digest.
Rack::ConditionalGet
compares the request's If-None-Match
header against the response's ETag
header. If they match, then it returns a 304.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With