-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Rails fragment cache for scrolled
REDMINE-20281
- Loading branch information
Showing
8 changed files
with
143 additions
and
42 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
entry_types/scrolled/app/helpers/pageflow_scrolled/cache_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: nil, widget_scope: :unknown, &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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 44 additions & 42 deletions
86
entry_types/scrolled/app/views/pageflow_scrolled/entries/show.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
entry_types/scrolled/spec/helpers/pageflow_scrolled/cache_helper_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 | ||
pageflow_configure do |config| | ||
config.features.enable_by_default('scrolled_entry_fragment_caching') | ||
end | ||
|
||
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('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 | ||
pageflow_configure do |config| | ||
config.features.enable_by_default('scrolled_entry_fragment_caching') | ||
end | ||
# would typically imply widget scope :published | ||
entry = create(:published_entry) | ||
|
||
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 | ||
pageflow_configure do |config| | ||
config.features.enable_by_default('scrolled_entry_fragment_caching') | ||
end | ||
entry = create(:published_entry) | ||
|
||
result = 'initial value' | ||
published_result = 'initial value' | ||
|
||
helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'oldest value', published_result = 'oldest value' } | ||
helper.cache_scrolled_entry(entry: entry, widget_scope: :editor) { result = 'old value' } | ||
helper.cache_scrolled_entry(entry: entry, widget_scope: :published) { result = 'new value', published_result = 'new value' } | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters