Skip to content
  • Aaron Lipman's avatar
    4671fe20
    Ensure cache fragment digests include all templates · 4671fe20
    Aaron Lipman authored
    A Rails view may rely on several templates (e.g. layouts and partials)
    in addition to the template for the action being rendered (e.g.
    "show.html.erb"). To track which view file is currently being rendered
    for the purpose of generating template tree digests used in cache
    fragment keys, Action View uses a stack, the top item of which is
    accessed via the @current_template variable (introduced in 1581cab9).
    
    Consider the following template:
    
        <!-- home.html.erb -->
        <%= render layout: "wrapper" do %>
          <%= cache "foo" %>
            HOME
          <%= end %>
        <%= end %>
    
    Inside the block passed to the render helper, @current_template
    corresponds to the wrapper.html.erb template instead of home.html.erb.
    As wrapper.html.erb is then used as the root node for generating the
    template tree digest used in the cache fragment key, the cache fragment
    fails to expire upon changes to home.html.erb. Additionally, should a
    second template use the wrapper.html.erb layout and contain a cache
    fragment with the same key, the cache fragment keys for both templates
    will be identical - causing cached content to "leak" from one view to
    another (as described in #38984).
    
    This commit skips adding templates to the stack when rendered as a
    layout with a block via the render helper, ensuring correct and unique
    cache fragment digests. Additionally, the virtual_path keyword arguments
    found in CacheHelper and all references to the are removed as they no
    longer possess any function. (Following the introduction of
    @current_template, virtual_path is accessed via
    @current_template.virtual_path rather than as a standalone variable.)
    4671fe20
    Ensure cache fragment digests include all templates
    Aaron Lipman authored
    A Rails view may rely on several templates (e.g. layouts and partials)
    in addition to the template for the action being rendered (e.g.
    "show.html.erb"). To track which view file is currently being rendered
    for the purpose of generating template tree digests used in cache
    fragment keys, Action View uses a stack, the top item of which is
    accessed via the @current_template variable (introduced in 1581cab9).
    
    Consider the following template:
    
        <!-- home.html.erb -->
        <%= render layout: "wrapper" do %>
          <%= cache "foo" %>
            HOME
          <%= end %>
        <%= end %>
    
    Inside the block passed to the render helper, @current_template
    corresponds to the wrapper.html.erb template instead of home.html.erb.
    As wrapper.html.erb is then used as the root node for generating the
    template tree digest used in the cache fragment key, the cache fragment
    fails to expire upon changes to home.html.erb. Additionally, should a
    second template use the wrapper.html.erb layout and contain a cache
    fragment with the same key, the cache fragment keys for both templates
    will be identical - causing cached content to "leak" from one view to
    another (as described in #38984).
    
    This commit skips adding templates to the stack when rendered as a
    layout with a block via the render helper, ensuring correct and unique
    cache fragment digests. Additionally, the virtual_path keyword arguments
    found in CacheHelper and all references to the are removed as they no
    longer possess any function. (Following the introduction of
    @current_template, virtual_path is accessed via
    @current_template.virtual_path rather than as a standalone variable.)
Loading