A platform for community discussion. Free, open, simple.
FIX: clean up onebox loading decorations on fetch failure (#39277)
Renato Atilio1cd80e702a99
diff --git a/frontend/discourse/app/static/prosemirror/extensions/onebox.js b/frontend/discourse/app/static/prosemirror/extensions/onebox.jsindex fe60fa2f1ce..c7c18644009 100644--- a/frontend/discourse/app/static/prosemirror/extensions/onebox.js+++ b/frontend/discourse/app/static/prosemirror/extensions/onebox.js@@ -319,6 +319,7 @@ const extension = {);} else {failedUrls.full.add(oneboxUrl);+ removeLoadingDecorations(view, oneboxUrl, "full");if (forceRetry) {showPreviewFailedToast();}@@ -327,6 +328,7 @@ const extension = {.catch(() => {pendingUrls.full.delete(oneboxUrl);failedUrls.full.add(oneboxUrl);+ removeLoadingDecorations(view, oneboxUrl, "full");if (forceRetry) {showPreviewFailedToast();}@@ -337,8 +339,8 @@ const extension = {// Inline oneboxes, batchedif (pendingUrls.inline.size) {const inlineUrls = pendingUrls.inline;- loadInlineOneboxes(inlineUrls, getContext()).then(- (inlineOneboxes) => {+ loadInlineOneboxes(inlineUrls, getContext())+ .then((inlineOneboxes) => {for (const url of inlineUrls) {pendingUrls.inline.delete(url);}@@ -348,8 +350,14 @@ const extension = {view.state.tr.setMeta(plugin, { inlineOneboxes }));}- }- );+ })+ .catch(() => {+ for (const url of inlineUrls) {+ pendingUrls.inline.delete(url);+ failedUrls.inline.add(url);+ }+ removeLoadingDecorations(view, inlineUrls, "inline");+ });}},@@ -509,6 +517,21 @@ const extension = {},});+ function removeLoadingDecorations(view, urls, type) {+ const urlSet = typeof urls === "string" ? new Set([urls]) : new Set(urls);+ const decoState = plugin.getState(view.state);+ const toRemove = decoState.find(+ undefined,+ undefined,+ (spec) => spec.oneboxType === type && urlSet.has(spec.oneboxUrl)+ );+ if (toRemove.length) {+ view.dispatch(+ view.state.tr.setMeta(plugin, { removeDecorations: toRemove })+ );+ }+ }+function showPreviewFailedToast() {getContext().toasts.default({duration: "short",diff --git a/frontend/pretty-text/addon/oneboxer.js b/frontend/pretty-text/addon/oneboxer.jsindex e7220a416e4..6ded19d9324 100644--- a/frontend/pretty-text/addon/oneboxer.js+++ b/frontend/pretty-text/addon/oneboxer.js@@ -87,9 +87,17 @@ function loadNext(ajax) {if (result?.jqXHR?.status === 429) {timeoutMs = 2000;removeLoading = false;- loadingQueue.unshift({ url, refresh, elem, categoryId, topicId });+ loadingQueue.unshift({+ url,+ refresh,+ elem,+ categoryId,+ topicId,+ onResolve,+ });} else {setFailedCache(normalize(url), true);+ onResolve?.(null);}})diff --git a/spec/system/composer/prosemirror_onebox_spec.rb b/spec/system/composer/prosemirror_onebox_spec.rbindex a3c31210308..f7787d11492 100644--- a/spec/system/composer/prosemirror_onebox_spec.rb+++ b/spec/system/composer/prosemirror_onebox_spec.rb@@ -181,6 +181,22 @@ describe "Composer - ProseMirror - Oneboxing" doexpect(composer).to have_value("Hey https://example.com/x and https://example.com/x")end+ it "removes loading decoration when onebox fetch fails" do+ # A blank preview makes /onebox respond 404, so the request rejects instead+ # of resolving to an error card.+ Oneboxer.stubs(:preview).returns("")++ cdp.allow_clipboard+ open_composer+ cdp.copy_paste("https://example.com/fail")+ page.send_keys(:enter)++ expect(rich).to have_css(".onebox-loading")+ expect(rich).to have_no_css(".onebox-loading")+ expect(rich).to have_css("a[href='https://example.com/fail']")+ expect(rich).to have_no_css(".onebox-wrapper")+ end+context "with watched word links" dofab!(:topic) { Fabricate(:topic, user: current_user) }fab!(:post) do