From 3e598b3baa3f4fcfd29e7da1aea7aa027831884d Mon Sep 17 00:00:00 2001
From: Enzo Innocenzi <enzo@innocenzi.dev>
Date: Sun, 5 Feb 2023 12:29:43 +0100
Subject: Improve `releases-tab` reliability (#6298)

---
 source/features/releases-tab.tsx | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

(limited to 'source/features/releases-tab.tsx')

diff --git a/source/features/releases-tab.tsx b/source/features/releases-tab.tsx
index 5a5ab7c4..e304267b 100644
--- a/source/features/releases-tab.tsx
+++ b/source/features/releases-tab.tsx
@@ -38,7 +38,11 @@ async function fetchFromApi(): Promise<number> {
 	return repository.releases.totalCount;
 }
 
-export const getReleaseCount = cache.function(async () => pageDetect.isRepoRoot() ? parseCountFromDom() : fetchFromApi(), {
+// Release count can be not found in DOM if:
+// - It is disabled by repository owner on the home page (release DOM element won't be there)
+// - It only contains pre-releases (count badge won't be shown)
+// For this reason, if we can't find a count from the DOM, we ask the API instead (see #6298)
+export const getReleaseCount = cache.function(async () => await parseCountFromDom() || fetchFromApi(), {
 	maxAge: {hours: 1},
 	staleWhileRevalidate: {days: 3},
 	cacheKey: getCacheKey,
@@ -80,8 +84,10 @@ async function addReleasesTab(): Promise<false | void> {
 	// Trigger a reflow to push the right-most tab into the overflow dropdown (second attempt #4254)
 	window.dispatchEvent(new Event('resize'));
 
+	const dropdownMenu = await elementReady('.js-responsive-underlinenav .dropdown-menu ul');
+
 	appendBefore(
-		select('.js-responsive-underlinenav .dropdown-menu ul')!,
+		dropdownMenu!,
 		'.dropdown-divider', // Won't exist if `more-dropdown` is disabled
 		createDropdownItem('Releases', buildRepoURL('releases'), {
 			'data-menu-item': 'rgh-releases-item',
-- 
cgit v1.2.3

