PERF: Ensure suggested topics is only loaded on last page of topic view (#28507)

This commit improves `TopicsController#show` to not load suggested and
related topics unless it is the last page of the topic's view.
Previously, we avoided loading suggested and related topics by the use
of conditionals in the `TopicViewSerializer` to avoid calling
`TopicView#suggested_topics` and `TopicView#related_topics`. However,
this pattern is not reliable as the methods can still be called from
other spots in the code base. Instead, we ensure that
`TopicView#include_suggested` and `TopicView#include_related` is set
correctly on the instance of `TopicView` which ensures that for the
given instance, `TopicView#suggested_topics` and
`TopicView#related_topics` will be a noop.
This commit is contained in:
Alan Guo Xiang Tan
2024-08-23 16:10:50 +08:00
committed by GitHub
parent b83a2a34a4
commit 21bb28df91
5 changed files with 66 additions and 37 deletions

View File

@ -1336,15 +1336,18 @@ class TopicsController < ApplicationController
return
end
if params[:replies_to_post_number] || params[:filter_upwards_post_id] ||
params[:filter_top_level_replies] || @topic_view.next_page.present?
@topic_view.include_suggested = false
@topic_view.include_related = false
end
topic_view_serializer =
TopicViewSerializer.new(
@topic_view,
scope: guardian,
root: false,
include_raw: !!params[:include_raw],
exclude_suggested_and_related:
!!params[:replies_to_post_number] || !!params[:filter_upwards_post_id] ||
!!params[:filter_top_level_replies],
)
respond_to do |format|