From 0771a5cd2e6d01b5822c4d21c5f17114c42c8a23 Mon Sep 17 00:00:00 2001 From: Ridhwana Date: Wed, 12 Apr 2023 18:15:15 +0200 Subject: [PATCH] Load `post_comments` billboard asynchronously (#19285) * feat: add a route to the async_info for display ads * feat: load the billboard asyncronously * feat: move the methods to the display_ads controller * feat: handle params better * feat: cache control headers * feat: test fastly caching headers on display ads * fix: surrogate key test * feat: use safe navigation operator to handle cases where there is no article id * fix: article id present then find the article * feat: add a response test * Fragment caching * feat: update the article decorator * feat: update cache keys for fragment * feat: remove an empty line * feat: add article id * feat: bust cache * feat: setup dropdown for billboard * chore: add chunk to same line * feat: add to safe params for caching * Update app/controllers/display_ads_controller.rb Co-authored-by: Joshua Wehner * feat: remove the cache deletion * feat: update the routes to follow a new scope * feat: update the cache params --------- Co-authored-by: Joshua Wehner --- .github/PULL_REQUEST_TEMPLATE.md | 1 - app/controllers/display_ads_controller.rb | 30 +++++++++ app/decorators/article_decorator.rb | 2 + app/javascript/packs/billboard.js | 23 +++++++ app/views/articles/_sidebar.html.erb | 4 +- app/views/articles/_sticky_nav.html.erb | 2 +- app/views/articles/show.html.erb | 18 ++--- app/views/display_ads/show.html.erb | 9 +++ app/views/shared/_display_ad.html.erb | 6 +- app/views/shared/_display_ad_header.html.erb | 2 +- app/views/sidebars/_homepage_content.html.erb | 2 +- app/views/stories/_main_stories_feed.html.erb | 6 +- config/fastly/snippets/safe_params_list.vcl | 2 +- config/routes.rb | 5 ++ spec/decorators/article_decorator_spec.rb | 11 +++ spec/requests/display_ads_spec.rb | 67 +++++++++++++++++++ .../user_visits_articles_by_timeframe_spec.rb | 1 - 17 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 app/controllers/display_ads_controller.rb create mode 100644 app/javascript/packs/billboard.js create mode 100644 app/views/display_ads/show.html.erb create mode 100644 spec/requests/display_ads_spec.rb diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 5dd9054d1..110d1d819 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -67,4 +67,3 @@ accessibility is impacted and tested. For more info, check out the ## [optional] What gif best describes this PR or how it makes you feel? ![alt_text](gif_link) - diff --git a/app/controllers/display_ads_controller.rb b/app/controllers/display_ads_controller.rb new file mode 100644 index 000000000..eec3106db --- /dev/null +++ b/app/controllers/display_ads_controller.rb @@ -0,0 +1,30 @@ +class DisplayAdsController < ApplicationController + before_action :set_cache_control_headers, only: %i[for_display], unless: -> { current_user } + CACHE_EXPIRY_FOR_DISPLAY_ADS = 15.minutes.to_i.freeze + + def show + skip_authorization + set_cache_control_headers(CACHE_EXPIRY_FOR_DISPLAY_ADS) unless session_current_user_id + + if params[:placement_area] + if params[:username].present? && params[:slug].present? + @article = Article.find_by(slug: params[:slug]) + end + + @display_ad = DisplayAd.for_display( + area: params[:placement_area], + user_signed_in: user_signed_in?, + organization_id: @article&.organization_id, + permit_adjacent_sponsors: ArticleDecorator.new(@article).permit_adjacent_sponsors?, + article_tags: @article&.decorate&.cached_tag_list_array || [], + article_id: @article&.id, + ) + + if @display_ad && !session_current_user_id + set_surrogate_key_header @display_ad.record_key + end + end + + render layout: false + end +end diff --git a/app/decorators/article_decorator.rb b/app/decorators/article_decorator.rb index 821947f98..fb07d7a48 100644 --- a/app/decorators/article_decorator.rb +++ b/app/decorators/article_decorator.rb @@ -113,6 +113,8 @@ class ArticleDecorator < ApplicationDecorator end def permit_adjacent_sponsors? + return true unless respond_to?(:user_id) && user_id.present? + author_ids = [user_id] + co_author_ids Users::Setting.where(user_id: author_ids).all?(&:permit_adjacent_sponsors) end diff --git a/app/javascript/packs/billboard.js b/app/javascript/packs/billboard.js new file mode 100644 index 000000000..f9b7d7ccb --- /dev/null +++ b/app/javascript/packs/billboard.js @@ -0,0 +1,23 @@ +import { setupDisplayAdDropdown } from '../utilities/displayAdDropdown'; + +// the term billboard can be synonymously interchanged with displayAd +async function getBillboard() { + const placeholderElement = document.getElementsByClassName( + 'js-display-ad-comments-container', + )[0]; + + const { asyncUrl } = placeholderElement.dataset || {}; + + if (placeholderElement.innerHTML.trim() === '') { + const response = await window.fetch(`${asyncUrl}`); + const htmlContent = await response.text(); + + const generatedElement = document.createElement('div'); + generatedElement.innerHTML = htmlContent; + + placeholderElement.appendChild(generatedElement); + setupDisplayAdDropdown(); + } +} + +getBillboard(); diff --git a/app/views/articles/_sidebar.html.erb b/app/views/articles/_sidebar.html.erb index 500da4414..2af17ff10 100644 --- a/app/views/articles/_sidebar.html.erb +++ b/app/views/articles/_sidebar.html.erb @@ -8,14 +8,14 @@ <% cache("display-area-left-#{rand(5)}-#{user_signed_in?}", expires_in: 5.minutes) do %> <% @left_sidebar_ad = DisplayAd.for_display(area: "sidebar_left", user_signed_in: user_signed_in?) %> <% if @left_sidebar_ad %> - <%= render partial: "shared/display_ad", locals: {display_ad: @left_sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_HOME} %> + <%= render partial: "shared/display_ad", locals: { display_ad: @left_sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_HOME } %> <% end %> <% end %> <% cache("display-area-left-2-#{rand(5)}-#{user_signed_in?}", expires_in: 5.minutes) do %> <% @second_left_sidebar_ad = DisplayAd.for_display(area: "sidebar_left_2", user_signed_in: user_signed_in?) %> <% if @second_left_sidebar_ad %>
- <%= render partial: "shared/display_ad", locals: {display_ad: @second_left_sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_HOME} %> + <%= render partial: "shared/display_ad", locals: { display_ad: @second_left_sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_HOME } %>
<% end %> <% end %> diff --git a/app/views/articles/_sticky_nav.html.erb b/app/views/articles/_sticky_nav.html.erb index 78f517bf3..fda46aee7 100644 --- a/app/views/articles/_sticky_nav.html.erb +++ b/app/views/articles/_sticky_nav.html.erb @@ -64,7 +64,7 @@ permit_adjacent_sponsors: @article.decorate.permit_adjacent_sponsors?) %> <% if sidebar_ad %>
- <%= render partial: "shared/display_ad", locals: { display_ad: sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_ARTICLE} %> + <%= render partial: "shared/display_ad", locals: { display_ad: sidebar_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_ARTICLE } %>
<% end %> <%# end %> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index fdbb4fb87..6eb951f0f 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -192,19 +192,9 @@ - <% cache("article-bottom-content-#{rand(5)}-#{@article.id}-#{user_signed_in?}-#{(@organization || @user).latest_article_updated_at}", expires_in: 15.minutes) do %> - <% @display_ad = DisplayAd.for_display(area: "post_comments", - user_signed_in: user_signed_in?, - organization_id: @article.organization_id, - permit_adjacent_sponsors: @article.decorate.permit_adjacent_sponsors?, - article_tags: @article.decorate.cached_tag_list_array, - article_id: @article.id) %> - <% if @display_ad %> -
- <%= render partial: "shared/display_ad", locals: { display_ad: @display_ad, data_context_type: DisplayAdEvent::CONTEXT_TYPE_ARTICLE} %> -
- <% end %> - <% end %> +
+
">
+
<% cache("article-bottom-content-#{@article.id}-#{user_signed_in?}-#{(@organization || @user).latest_article_updated_at}", expires_in: 18.hours) do %> <% suggested_articles = Articles::Suggest.call(@article, max: 4) %> @@ -241,7 +231,7 @@ <%= render "moderations/modals/unflag_user_modal" %>
-<%= javascript_packs_with_chunks_tag "followButtons", defer: true %> +<%= javascript_packs_with_chunks_tag "followButtons", "billboard", defer: true %> <% cache("article-show-scripts", expires_in: 8.hours) do %>