Skip to content

Commit

Permalink
Image Download: Add cache buster if forceRefresh == true (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinarol authored Oct 1, 2024
1 parent cded085 commit 2cf737b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/Gravatar/Network/Services/ImageDownloadService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ public actor ImageDownloadService: ImageDownloader, Sendable {

extension URLRequest {
fileprivate static func imageRequest(url: URL, forceRefresh: Bool) -> URLRequest {
var request = forceRefresh ? URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData) : URLRequest(url: url)
var request = forceRefresh ? URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData) : URLRequest(url: url)
if forceRefresh, let url = request.url, url.isGravatarURL {
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
// Gravatar doesn't support cache control headers. So we add a random query parameter to
// bypass the backend cache and get the latest image.
// Remove this if Gravatar starts to support cache control headers.
urlComponents?.queryItems?.append(.init(name: "_", value: "\(NSDate().timeIntervalSince1970)"))
request.url = urlComponents?.url
}
request.httpShouldHandleCookies = false
request.addValue("image/*", forHTTPHeaderField: "Accept")
return request
Expand Down

0 comments on commit 2cf737b

Please sign in to comment.