Skip to content

Commit

Permalink
Cache sidenav (#2133)
Browse files Browse the repository at this point in the history
* Handle active sidenav item on the client side.
* Cache the sidenav for 10 hours.
* Enable cache logging in development and use memory_store.
* Only cache the sidenav if there isn't a namespace present.

This prevents caching the incorrect sidenav for:
* dev-preview
* beta

* Expire cache as part of the release phase.
* Update code after rebase.

Setting the active sidenav item is handled on the client side now.

* Add task to clear Rails' cache and run it during Heroku's release phase.
  • Loading branch information
fabianrbz authored and mheap committed Nov 21, 2019
1 parent 83516d4 commit 6132b49
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
release: bundle exec rake db:migrate
release: bundle exec rake db:migrate && bundle exec rake cache:clear
web: rails s
10 changes: 6 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def title
end
end

def show_canonical_meta?
return true if params[:code_language].present?
return true if Rails.env.production? && request.base_url != 'https://developer.nexmo.com'
false
def active_sidenav_item
if params[:tutorial_name]
"/#{params[:product]}/tutorials/#{params[:tutorial_name]}"
else
request.path.chomp("/#{params[:code_language]}")
end
end

def canonical_path
Expand Down
6 changes: 6 additions & 0 deletions app/javascript/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,13 @@ function toggleMobileNavBtn() {
}
}

function setActiveNavItem() {
const activeItem = $('nav.sidenav').data('active');
$(`.Vlt-sidemenu__link[href="${activeItem}"]`).addClass('Vlt-sidemenu__link_active')
}

export default () => {
setActiveNavItem();
Volta.menu.init();
toggleMobileNavBtn();
}
16 changes: 0 additions & 16 deletions app/presenters/sidenav_subitem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,4 @@ def controller
:use_case
end
end

def active?
if navigation == :tutorials
active_path.starts_with?(url)
else
url == active_path
end
end

def active_path
@active_path ||= request_path.chomp("/#{code_language}")
end

def link_css_class
active? ? 'Vlt-sidemenu__link_active' : ''
end
end
6 changes: 4 additions & 2 deletions app/views/layouts/documentation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<%= render partial: 'layouts/partials/header' %>
<div id="Vlt-sidenav" class="Vlt-sidenav Vlt-sidenav--light">
<div class="Vlt-sidenav__scroll">
<nav class="sidenav" tabindex="0" >
<%= render partial: 'layouts/partials/sidenav' %>
<nav class="sidenav" tabindex="0" data-active="<%= active_sidenav_item %>">
<% cache_unless params[:namespace].present?, 'sidenav', expires_in: 10.hours do %>
<%= render partial: 'layouts/partials/sidenav' %>
<% end %>
</nav>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/partials/_sidenav_subitem.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<li class="navigation-item--<%= sidenav_subitem.title.parameterize %> navigation-item">
<% if sidenav_subitem.show_link? %>
<%= link_to(sidenav_subitem.url, class: "Vlt-sidemenu__link #{sidenav_subitem.link_css_class}") do %>
<%= link_to(sidenav_subitem.url, class: 'Vlt-sidemenu__link') do %>
<% if sidenav_subitem.label? %>
<span class="Vlt-sidemenu__label">
<%= sidenav_subitem.title %>
Expand Down
6 changes: 2 additions & 4 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}",
}
config.cache_store = :memory_store, { size: 32.megabytes }
else
config.action_controller.perform_caching = false

Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/cache.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace :cache do
desc "Clear Rails' redis cache"
task 'clear': :environment do
Rails.cache.clear
end
end
8 changes: 0 additions & 8 deletions spec/presenters/sidenav_subitem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,4 @@
it { expect(subject.url).to eq('/product-lifecycle/beta') }
end
end

describe '#active?' do
it { expect(subject.active?).to eq(true) }
end

describe '#link_css_class' do
it { expect(subject.link_css_class).to eq('Vlt-sidemenu__link_active') }
end
end

0 comments on commit 6132b49

Please sign in to comment.