Johnson Yeap Johnson Yeap - 4 months ago 13
Ruby Question

How to print X-Runtime on Ruby on Rails's view?

I wish to print the response time that is set by Rack::Runtime on my view.
I know we can get all of the HTTP headers by " curl -I address ".

enter image description here

However, the response object in Rails controller only contains several headers by default:

config.action_dispatch.default_headers = {
'X-Frame-Options' => 'SAMEORIGIN',
'X-XSS-Protection' => '1; mode=block',
'X-Content-Type-Options' => 'nosniff'
}

Answer

I'm afraid there's no such way. The reason is

  1. When Rack adds the X-Runtime header to the response, the view has already been rendered. This is obvious because if not, how could Rack tell how long the view rendering takes? So you can't insert the value into your view at server side.
  2. At the time Javascript starts to run on a browser, the response that contains the HTML has already been consumed, and the headers of the response has been thrown. So you can't insert the value into your view at browser side either.