A platform for community discussion. Free, open, simple.
FEATURE: Render embedded comments without the 30 second refresh delay (#44101)
jbrw778f26ba626e
diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rbindex e2a19520b6a..4228927c32b 100644--- a/app/controllers/embed_controller.rb+++ b/app/controllers/embed_controller.rb@@ -5,7 +5,7 @@ class EmbedController < ApplicationControllerskip_before_action :check_xhr, :preload_json, :verify_authenticity_token- before_action :prepare_embeddable, except: [:info]+ before_action :prepare_embeddable, except: %i[info status]before_action :ensure_api_request, only: [:info]layout "embed"@@ -120,6 +120,8 @@ class EmbedController < ApplicationController@reply_count = 0 if @reply_count < 0@posts_left = @reply_count - SiteSetting.embed_post_limit if @reply_count >SiteSetting.embed_post_limit++ discourse_expires_in 1.minuteelsif embed_url.present?Jobs.enqueue(:retrieve_topic,@@ -127,10 +129,26 @@ class EmbedController < ApplicationControllerembed_url: embed_url,referer: request.env["HTTP_REFERER"],)+ response.headers["Cache-Control"] = "no-cache"render "loading"end+ end- discourse_expires_in 1.minute+ def status+ embed_url = params.require(:embed_url)++ unless EmbeddableHost.url_allowed?(embed_url)+ raise Discourse::InvalidAccess.new("invalid embed host")+ end++ topic_id = TopicEmbed.topic_id_for_embed(embed_url)+ topic = topic_id.present? ? Topic.find_by(id: topic_id) : nil+ topic_id = nil if topic.nil? || !guardian.can_see?(topic)++ # Short shared cache so a burst of concurrent pollers (thundering herd) is+ # served from the anonymous response cache instead of the database.+ discourse_expires_in 2.seconds+ render json: { topic_id: topic_id }enddef infodiff --git a/config/routes.rb b/config/routes.rbindex 85e11d7c8c4..e5ab357bc9a 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1605,6 +1605,7 @@ Discourse::Application.routes.draw doget "embed/topics" => "embed#topics"get "embed/comments" => "embed#comments"+ get "embed/status" => "embed#status"get "embed/count" => "embed#count"get "embed/info" => "embed#info"diff --git a/frontend/discourse/scripts/js/embed-application.js b/frontend/discourse/scripts/js/embed-application.jsindex e33324b5166..aec5ef05dc2 100644--- a/frontend/discourse/scripts/js/embed-application.js+++ b/frontend/discourse/scripts/js/embed-application.js@@ -22,6 +22,43 @@}}+ // Poll for the topic to be created so we can render comments as soon as+ // possible instead of waiting for the full meta-refresh interval.+ //+ // Use exponential backoff with full jitter so clients landing on the same+ // article don't synchronise their polls (thundering herd on /embed/status).+ // The poll URL is left cacheable so the shared anonymous response cache can+ // serve concurrent polls instead of hitting the database.+ const POLL_MIN_DELAY = 1500;+ const POLL_MAX_DELAY = 10000;++ function pollForTopic() {+ let retryDelay = POLL_MIN_DELAY;++ const scheduleNextPoll = () => {+ const wait = Math.random() * retryDelay;+ retryDelay = Math.min(retryDelay * 2, POLL_MAX_DELAY);+ setTimeout(poll, wait);+ };++ const poll = () => {+ const pollUrl = new URL(window.location.href);+ pollUrl.pathname = pollUrl.pathname.replace(/\/comments$/, "/status");+ fetch(pollUrl, { headers: { Accept: "application/json" } })+ .then((response) => response.json())+ .then((data) => {+ if (data.topic_id) {+ window.location.reload();+ return;+ }+ scheduleNextPoll();+ })+ .catch(scheduleNextPoll);+ };++ scheduleNextPoll();+ }+window.onload = function () {// get state info from data attributelet embedState = document.querySelector("[data-embed-state]");@@ -40,6 +77,10 @@embedId,});+ if (state === "loading") {+ pollForTopic();+ }+let postLinks = document.querySelectorAll("a[data-link-to-post]"),i;diff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex 46c0b981725..267b9370383 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -531,6 +531,19 @@ RSpec.describe EmbedController doexpect(response.body).not_to match(I18n.t("embed.error"))end+ it "does not cache the loading page" do+ global_setting :anon_cache_store_threshold, 1+ Middleware::AnonymousCache.enable_anon_cache+ Middleware::AnonymousCache.clear_all_cache!++ get "/embed/comments", params: { embed_url: embed_url }++ expect(response.status).to eq(200)+ expect(response.body).to include("data-embed-state='loading'")+ expect(response.headers["X-Discourse-Cached"]).to be_nil+ expect(response.headers["Cache-Control"]).to include("no-cache")+ end+it "includes CSS from embedded_scss field" dotheme = Fabricate(:theme)theme.set_default!@@ -767,6 +780,61 @@ RSpec.describe EmbedController doendend+ describe "#status" do+ fab!(:embeddable_host)++ it "raises an error with an unallowed embed url" do+ get "/embed/status", params: { embed_url: "http://nope.com/article" }++ expect(response.status).to eq(400)+ end++ it "returns no topic id before the embed has been imported" do+ get "/embed/status", params: { embed_url: embed_url }++ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to be_nil+ end++ it "serves a short-lived shared cache to concurrent pollers" do+ global_setting :anon_cache_store_threshold, 1+ Middleware::AnonymousCache.enable_anon_cache+ Middleware::AnonymousCache.clear_all_cache!++ get "/embed/status", params: { embed_url: embed_url }++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Cached"]).to eq("store")+ end++ it "returns the topic id once the embed has been imported" do+ topic_embed = Fabricate(:topic_embed, embed_url: embed_url)++ get "/embed/status", params: { embed_url: embed_url }++ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to eq(topic_embed.topic.id)+ end++ it "hides topics the user cannot see" do+ restricted_category = Fabricate(:category)+ restricted_category.set_permissions(staff: :full)+ restricted_category.save!+ restricted_topic = Fabricate(:topic, category: restricted_category)+ Fabricate(+ :topic_embed,+ post: Fabricate(:post, topic: restricted_topic),+ topic: restricted_topic,+ embed_url: embed_url,+ )++ get "/embed/status", params: { embed_url: embed_url }++ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to be_nil+ end+ end+describe "#count" dofab!(:embeddable_host)