Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rails re-compile the whole erb/haml view every time some hits it in production?

Say the view is in erb/haml, but is static (doesn't have any dynamic parts within it), does Rails recompile that view every time someone hits it, or does it cache the html after someone hits it once?

A follow up question: if I have a view with some dynamic parts, does Rails recompile only the dynamic part of the view or does it recompile the whole page?

I'm running Rails 4.

like image 708
Sbbs Avatar asked Apr 24 '14 15:04

Sbbs


2 Answers

Rails evaluates view files and partials on every request. That is why html fragment caching is so valuable.

See Caching with Rails in Rails Guides.

Typically you would use Rails' cache to cache a html fragment so it does not need to be re-rendered on each request. Here's a Haml example:

- cache "key-name-for-static-content" do
  .some-html
    some content

See DHH's How Key-Based Cache Expiration Works for key-based caching using models.

For advanced uses, I wrote cache_rocket to help cache static content around dynamic content in partials.

like image 100
tee Avatar answered Nov 06 '22 17:11

tee


Just a bit of terminology note: now compile is commonly referring to the act of turning the view template into a ruby function. Rails does this for all views:

http://guides.rubyonrails.org/action_view_overview.html#template-caching

Those functions will then output the output you want.

So I would say views are not "re-compiled" but "re-evaluated".

like image 44
lulalala Avatar answered Nov 06 '22 16:11

lulalala