@@ -195,7 +195,7 @@ public struct CachedAsyncImage<Content>: View where Content: View {
195195 public init ( url: URL ? , urlCache: URLCache = . shared, scale: CGFloat = 1 , transaction: Transaction = Transaction ( ) , @ViewBuilder content: @escaping ( AsyncImagePhase ) -> Content ) {
196196 let configuration = URLSessionConfiguration . default
197197 configuration. urlCache = urlCache
198- configuration. requestCachePolicy = . returnCacheDataElseLoad
198+ configuration. requestCachePolicy = . reloadIgnoringLocalCacheData
199199 self . url = url
200200 self . urlSession = URLSession ( configuration: configuration)
201201 self . scale = scale
@@ -204,14 +204,28 @@ public struct CachedAsyncImage<Content>: View where Content: View {
204204 }
205205
206206 private func load( url: URL ? ) async {
207+ guard let url = url else { return }
208+ let request = URLRequest ( url: url)
209+ let cachedData : Data ?
210+ let animation : Animation ?
211+ if let cachedResponse = urlSession. configuration. urlCache? . cachedResponse ( for: request) {
212+ cachedData = cachedResponse. data
213+ animation = nil
214+ } else {
215+ cachedData = nil
216+ animation = transaction. animation
217+ }
207218 do {
208- guard let url = url else { return }
209- let request = URLRequest ( url: url)
210- let ( data, _) = try await urlSession. data ( for: request)
219+ let data : Data
220+ if let cachedData = cachedData {
221+ data = cachedData
222+ } else {
223+ ( data, _) = try await urlSession. data ( for: request)
224+ }
211225#if os(macOS)
212226 if let nsImage = NSImage ( data: data) {
213227 let image = Image ( nsImage: nsImage)
214- withAnimation ( transaction . animation) {
228+ withAnimation ( animation) {
215229 phase = . success( image)
216230 }
217231 } else {
@@ -220,15 +234,15 @@ public struct CachedAsyncImage<Content>: View where Content: View {
220234#else
221235 if let uiImage = UIImage ( data: data) {
222236 let image = Image ( uiImage: uiImage)
223- withAnimation ( transaction . animation) {
237+ withAnimation ( animation) {
224238 phase = . success( image)
225239 }
226240 } else {
227241 throw AsyncImage< Content> . LoadingError( )
228242 }
229243#endif
230244 } catch {
231- withAnimation ( transaction . animation) {
245+ withAnimation ( animation) {
232246 phase = . failure( error)
233247 }
234248 }
0 commit comments