Skip to content

Commit 7981e7c

Browse files
authored
Merge pull request #350 from hotwired/fix-offline-caching
Conditionally cache offline response urls based on their mimeType
2 parents fdf2657 + 4fc1605 commit 7981e7c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

turbo/src/main/kotlin/dev/hotwire/turbo/http/TurboHttpRepository.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dev.hotwire.turbo.http
33
import android.webkit.CookieManager
44
import android.webkit.WebResourceRequest
55
import android.webkit.WebResourceResponse
6-
import dev.hotwire.turbo.util.TurboLog
76
import dev.hotwire.turbo.util.dispatcherProvider
87
import dev.hotwire.turbo.util.logError
98
import kotlinx.coroutines.CoroutineScope
@@ -75,15 +74,22 @@ internal class TurboHttpRepository(private val coroutineScope: CoroutineScope) {
7574

7675
return try {
7776
val response = issueRequest(resourceRequest)
78-
79-
// Cache based on the response's request url, which may have been a redirect
8077
val responseUrl = response?.request?.url.toString()
8178
val isRedirect = url != responseUrl
79+
val resourceResponse = resourceResponse(response)
80+
81+
// If the response is a document, cache based on the final response's request url, so
82+
// that Turbo HTTP location redirects are not masked. Otherwise, cached based on the
83+
// original request url, so redirected images, resources, etc are available offline.
84+
val cacheUrl = if (resourceResponse?.mimeType == "text/html") {
85+
responseUrl
86+
} else {
87+
url
88+
}
8289

8390
// Let the app cache the response
84-
val resourceResponse = resourceResponse(response)
8591
val cachedResponse = resourceResponse?.let {
86-
requestHandler.cacheResponse(responseUrl, it)
92+
requestHandler.cacheResponse(cacheUrl, it)
8793
}
8894

8995
Result(

0 commit comments

Comments
 (0)