From 6132b4982eac6cacc2c9fc9aae06aa7dc450baf8 Mon Sep 17 00:00:00 2001 From: Fabian Rodriguez Date: Thu, 21 Nov 2019 13:41:18 +0100 Subject: [PATCH] Cache sidenav (#2133) * 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. --- Procfile | 2 +- app/helpers/application_helper.rb | 10 ++++++---- app/javascript/navigation/index.js | 6 ++++++ app/presenters/sidenav_subitem.rb | 16 ---------------- app/views/layouts/documentation.html.erb | 6 ++++-- .../layouts/partials/_sidenav_subitem.html.erb | 2 +- config/environments/development.rb | 6 ++---- lib/tasks/cache.rake | 6 ++++++ spec/presenters/sidenav_subitem_spec.rb | 8 -------- 9 files changed, 26 insertions(+), 36 deletions(-) create mode 100644 lib/tasks/cache.rake diff --git a/Procfile b/Procfile index b31a2ae973..13df39cd13 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,2 @@ -release: bundle exec rake db:migrate +release: bundle exec rake db:migrate && bundle exec rake cache:clear web: rails s diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 44fefd5db1..317928efbf 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/javascript/navigation/index.js b/app/javascript/navigation/index.js index 2b2eb5b868..8ad7b86f02 100644 --- a/app/javascript/navigation/index.js +++ b/app/javascript/navigation/index.js @@ -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(); } diff --git a/app/presenters/sidenav_subitem.rb b/app/presenters/sidenav_subitem.rb index 36a5bd644c..cfc5077b07 100644 --- a/app/presenters/sidenav_subitem.rb +++ b/app/presenters/sidenav_subitem.rb @@ -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 diff --git a/app/views/layouts/documentation.html.erb b/app/views/layouts/documentation.html.erb index 24fd937f98..f0c11ab02b 100644 --- a/app/views/layouts/documentation.html.erb +++ b/app/views/layouts/documentation.html.erb @@ -6,8 +6,10 @@ <%= render partial: 'layouts/partials/header' %>
-
diff --git a/app/views/layouts/partials/_sidenav_subitem.html.erb b/app/views/layouts/partials/_sidenav_subitem.html.erb index 3554788b91..583d756d56 100644 --- a/app/views/layouts/partials/_sidenav_subitem.html.erb +++ b/app/views/layouts/partials/_sidenav_subitem.html.erb @@ -1,6 +1,6 @@