diff --git a/Modules/Images/Sources/Images/FaviconDownloader.swift b/Modules/Images/Sources/Images/FaviconDownloader.swift index 86ec18784..74cb15231 100644 --- a/Modules/Images/Sources/Images/FaviconDownloader.swift +++ b/Modules/Images/Sources/Images/FaviconDownloader.swift @@ -146,7 +146,7 @@ extension Notification.Name { // If the site explicitly specifies favicon.ico, it will appear twice. self.currentHomePageHasOnlyFaviconICO = faviconURLs.count == 1 self.remainingFaviconURLs[url] = faviconURLs[...] - downloadNextFavicon(forHomePageURL: url) + downloadNextFavicon(forHomePageURL: url, lastFailureWasTransient: false) } return nil @@ -165,7 +165,7 @@ extension Notification.Name { } guard singleFaviconDownloader.iconImage != nil else { if remainingFaviconURLs[homePageURL] != nil { - downloadNextFavicon(forHomePageURL: homePageURL) + downloadNextFavicon(forHomePageURL: homePageURL, lastFailureWasTransient: singleFaviconDownloader.lastFailureWasTransient) } return } @@ -243,7 +243,7 @@ private extension FaviconDownloader { // A skipped candidate advances to the next one — otherwise the queue // would stall and the remaining candidates would never be tried. // - func downloadNextFavicon(forHomePageURL homePageURL: String) { + func downloadNextFavicon(forHomePageURL homePageURL: String, lastFailureWasTransient: Bool) { while let faviconURL = remainingFaviconURLs[homePageURL]?.first { remainingFaviconURLs[homePageURL] = remainingFaviconURLs[homePageURL]?.dropFirst() if canAttemptDownload(faviconURL) { @@ -253,7 +253,7 @@ private extension FaviconDownloader { } remainingFaviconURLs[homePageURL] = nil - if currentHomePageHasOnlyFaviconICO { + if currentHomePageHasOnlyFaviconICO && !lastFailureWasTransient { ImageDatabase.shared.saveHomePageFavicon(homePageURL: homePageURL, faviconURL: nil) } } diff --git a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift index 17b26194b..c7a35b6e1 100644 --- a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift +++ b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift @@ -22,6 +22,7 @@ extension Notification.Name { public let homePageURL: String? public var iconImage: IconImage? + private(set) var lastFailureWasTransient = false private static let retryInterval: TimeInterval = 30 * 60 // 30 minutes @@ -60,7 +61,9 @@ private extension SingleFaviconDownloader { func findFavicon() async { let kind = ActivityKind.downloadFavicon(faviconURL: faviconURL) - if let storedData = try? await DownloadedImageCache.shared.imageData(for: faviconURL, downloadPriority: nil, checksForUpdates: true, activityOwner: .faviconDownloader, activityKind: kind, activityDetail: homePageURL) { + lastFailureWasTransient = false + do throws(ImageDownloadError) { + let storedData = try await DownloadedImageCache.shared.imageData(for: faviconURL, downloadPriority: nil, checksForUpdates: true, activityOwner: .faviconDownloader, activityKind: kind, activityDetail: homePageURL) // DownloadedImageCache records download failures, but can’t know whether the data decodes as an icon. if let image = await Self.iconImage(storedData, maxPixelSize: RSImage.maxIconPixelSize) { iconImage = IconImage(image) @@ -68,6 +71,9 @@ private extension SingleFaviconDownloader { } else { ImageDatabase.shared.recordFailure(url: faviconURL, statusCode: nil) } + } catch { + // A non-image response usually means the site has no favicon at this URL. + lastFailureWasTransient = error.isTransient && !error.decodingFailed } postDidLoadFaviconNotification() }