Skip to content

Commit

Permalink
Merge pull request #2001 from aviav/scrolled-rails-cache
Browse files Browse the repository at this point in the history
Use Rails fragment cache for scrolled entries
  • Loading branch information
aviav authored Sep 20, 2023
2 parents f3d0329 + a4f80f7 commit 7c84e0e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 42 deletions.
11 changes: 11 additions & 0 deletions entry_types/scrolled/app/helpers/pageflow_scrolled/cache_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PageflowScrolled
# @api private
module CacheHelper
def cache_scrolled_entry(entry:, widget_scope:, &block)
condition =
widget_scope == :published &&
entry.feature_state('scrolled_entry_fragment_caching')
cache_if(condition, [entry, :head_and_body, widget_scope], &block)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module SeedHtmlHelper
include Pageflow::WidgetsHelper
include Pageflow::StructuredDataHelper
include Pageflow::TextDirectionHelper
include PageflowScrolled::CacheHelper
include FaviconHelper
include PacksHelper
include WebpackPublicPathHelper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
<!DOCTYPE html>
<%= content_tag(:html, lang: @entry.locale, dir: text_direction(@entry.locale)) do %>
<head>
<title><%= pretty_entry_title(@entry) %></title>
<%= cache_scrolled_entry(entry: @entry, widget_scope: @widget_scope) do %>
<!DOCTYPE html>
<%= content_tag(:html, lang: @entry.locale, dir: text_direction(@entry.locale)) do %>
<head>
<title><%= pretty_entry_title(@entry) %></title>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">


<%= social_share_meta_tags_for(@entry) %>
<%= meta_tags_for_entry(@entry) %>
<%= feed_link_tags_for_entry(@entry) unless @skip_feed_link_tags%>
<%= social_share_meta_tags_for(@entry) %>
<%= meta_tags_for_entry(@entry) %>
<%= feed_link_tags_for_entry(@entry) unless @skip_feed_link_tags%>

<%= scrolled_favicons_for_entry(@entry, entry_mode: @widget_scope) %>
<%= scrolled_favicons_for_entry(@entry, entry_mode: @widget_scope) %>

<%= javascript_include_tag 'pageflow_scrolled/legacy' %>
<%= scrolled_frontend_stylesheet_packs_tag(@entry, widget_scope: @widget_scope) %>
<%= javascript_include_tag 'pageflow_scrolled/legacy' %>
<%= scrolled_frontend_stylesheet_packs_tag(@entry, widget_scope: @widget_scope) %>

<%= scrolled_theme_properties_style_tag(@entry.theme) %>
<%= scrolled_theme_stylesheet_pack_tags(@entry.theme) %>
<%= scrolled_theme_properties_style_tag(@entry.theme) %>
<%= scrolled_theme_stylesheet_pack_tags(@entry.theme) %>

<%= render_widget_head_fragments(@entry, scope: @widget_scope) %>
<%= render_widget_head_fragments(@entry, scope: @widget_scope) %>

<% if Rails.env.development? %>
<script>
try {
if (window.top !== window) {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.top.__REACT_DEVTOOLS_GLOBAL_HOOK__;
<% if Rails.env.development? %>
<script>
try {
if (window.top !== window) {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.top.__REACT_DEVTOOLS_GLOBAL_HOOK__;
}
} catch (e) {
console.warn('unable to connect to top frame for connecting dev tools');
}
} catch (e) {
console.warn('unable to connect to top frame for connecting dev tools');
}
</script>
<% end %>
</script>
<% end %>

<% ssr_html = @skip_ssr ? '' : render_scrolled_entry(@entry) %>
<% ssr_html = @skip_ssr ? '' : render_scrolled_entry(@entry) %>

<% if !@skip_ssr && (params[:frontend] == 'v2' || @entry.feature_state('frontend_v2')) %>
<%= generated_media_queries_tags_for(ssr_html) %>
<% end %>
</head>
<body>
<%= structured_data_for_entry(@entry) unless @skip_structured_data %>
<% if !@skip_ssr && (params[:frontend] == 'v2' || @entry.feature_state('frontend_v2')) %>
<%= generated_media_queries_tags_for(ssr_html) %>
<% end %>
</head>
<body>
<%= structured_data_for_entry(@entry) unless @skip_structured_data %>

<%= render 'pageflow_scrolled/entries/global_notices' %>
<%= render 'pageflow_scrolled/entries/global_notices' %>

<div id="fullscreenRoot"></div>
<div id="root"><%= ssr_html %></div>
<div id="fullscreenRoot"></div>
<div id="root"><%= ssr_html %></div>

<div id='template-widget-container'>
<%= render_widgets(@entry, scope: @widget_scope, insert_point: :bottom_of_entry) %>
</div>
<div id='template-widget-container'>
<%= render_widgets(@entry, scope: @widget_scope, insert_point: :bottom_of_entry) %>
</div>

<%= scrolled_webpack_public_path_script_tag %>
<%= scrolled_frontend_javascript_packs_tag(@entry, widget_scope: @widget_scope) %>
<%= scrolled_webpack_public_path_script_tag %>
<%= scrolled_frontend_javascript_packs_tag(@entry, widget_scope: @widget_scope) %>

<%= scrolled_entry_json_seed_script_tag(@entry, @seed_options || {}) %>
</body>
<%= scrolled_entry_json_seed_script_tag(@entry, @seed_options || {}) %>
</body>
<% end %>
<% end %>
4 changes: 4 additions & 0 deletions entry_types/scrolled/config/locales/new/cache.de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
de:
pageflow:
scrolled_entry_fragment_caching:
feature_name: Pageflow-Next-Fragment-Caching
4 changes: 4 additions & 0 deletions entry_types/scrolled/config/locales/new/cache.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
pageflow:
scrolled_entry_fragment_caching:
feature_name: Pageflow Next Fragment Caching
1 change: 1 addition & 0 deletions entry_types/scrolled/lib/pageflow_scrolled/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def configure(config)
c.features.register('iframe_embed_content_element')
c.features.register('image_gallery_content_element')
c.features.register('frontend_v2')
c.features.register('scrolled_entry_fragment_caching')

c.additional_frontend_seed_data.register(
'frontendVersion',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'spec_helper'

module PageflowScrolled
RSpec.describe CacheHelper, type: :helper do
describe '#cache_scrolled_entry', :use_clean_rails_memory_store_fragment_caching do
it 'caches if feature is enabled and entry is published' do
entry = create(:published_entry,
with_feature: 'scrolled_entry_fragment_caching')

result = 'initial value'

helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'old value' }
helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'new value' }

expect(result).to eq('old value')
end

it "doesn't cache if feature is disabled" do
entry = create(:published_entry)

result = 'initial value'

helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'old value' }
helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'new value' }

expect(result).to eq('new value')
end

it "doesn't cache if widget_scope isn't :published" do
# would typically imply widget scope :published
entry = create(:published_entry,
with_feature: 'scrolled_entry_fragment_caching')

result = 'initial value'

helper.cache_scrolled_entry(entry: entry, widget_scope: :editor) { result = 'old value' }
helper.cache_scrolled_entry(entry: entry, widget_scope: :editor) { result = 'new value' }

expect(result).to eq('new value')
end

it 'caches for different values of widget scope' do
entry = create(:published_entry,
with_feature: 'scrolled_entry_fragment_caching')

result = 'initial value'
published_result = 'initial value'

helper.cache_scrolled_entry(entry: entry, widget_scope: :published) do
result = 'oldest value', published_result = 'oldest value'
end
helper.cache_scrolled_entry(entry: entry, widget_scope: :editor) { result = 'old value' }
helper.cache_scrolled_entry(entry: entry, widget_scope: :published) do
result = 'new value', published_result = 'new value'
end
helper.cache_scrolled_entry(entry: entry, widget_scope: :editor) { result = 'newest value' }

expect(result).to eq('newest value')
expect(published_result).to eq('oldest value')
end
end
end
end
14 changes: 14 additions & 0 deletions entry_types/scrolled/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

# Inspired by https://gitlab.com/gitlab-org/gitlab-foss
config.around(:each, :use_clean_rails_memory_store_fragment_caching) do |example|
begin
caching_store = ActionController::Base.cache_store
ActionController::Base.cache_store = ActiveSupport::Cache::MemoryStore.new
ActionController::Base.perform_caching = true

example.run
ensure
ActionController::Base.perform_caching = false
ActionController::Base.cache_store = caching_store
end
end
end

0 comments on commit 7c84e0e

Please sign in to comment.