Skip to content

Commit e3239d5

Browse files
authored
CfW resources: don't use Cache API in non-secure contexts (#5467)
window.caches is available only secure contexts I added a check for `window.caches` availability. And skip all the logic when it's not available. It's a followup for #5379 Fixes: https://youtrack.jetbrains.com/issue/CMP-9174 ## Testing This should be tested by QA ## Release Notes N/A
1 parent c5d9c23 commit e3239d5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

components/resources/library/src/webMain/kotlin/org/jetbrains/compose/resources/ResourceWebCache.web.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlin.js.ExperimentalWasmJsInterop
1212
import kotlin.js.JsAny
1313
import kotlin.js.Promise
1414
import kotlin.js.asJsException
15+
import kotlin.js.js
1516

1617
/**
1718
* We use [Cache] APIs to cache the successful strings.cvr and other responses.
@@ -20,6 +21,9 @@ import kotlin.js.asJsException
2021
*
2122
* Cache limits:
2223
* https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria#other_web_technologies
24+
*
25+
* The Cache API is available only in secure contexts (HTTPS or localhost).
26+
* In non-secure contexts it will behave as if there are no cached values.
2327
*/
2428
@OptIn(ExperimentalWasmJsInterop::class)
2529
internal object ResourceWebCache {
@@ -33,7 +37,11 @@ internal object ResourceWebCache {
3337
// A mutex to avoid multiple cache reset
3438
private val resetMutex = Mutex()
3539

40+
private val supportsCacheApi: Boolean by lazy { supportsCacheApi() }
41+
3642
suspend fun load(path: String, onNoCacheHit: suspend (path: String) -> Response): Response {
43+
if (!supportsCacheApi) return onNoCacheHit(path)
44+
3745
if (isNewSession()) {
3846
// There can be many load requests, and there must be 1 reset max. Therefore, using `resetMutex`.
3947
resetMutex.withLock {
@@ -75,6 +83,11 @@ internal object ResourceWebCache {
7583
}
7684
}
7785

86+
87+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/caches
88+
// Supported only in secure contexts (HTTPS or localhost)
89+
private fun supportsCacheApi(): Boolean = js("Boolean(window.caches)")
90+
7891
// Promise.await is not yet available in webMain: https://github.com/Kotlin/kotlinx.coroutines/issues/4544
7992
// TODO(o.karpovich): get rid of this function, when kotlinx-coroutines provide Promise.await in webMain out of a box
8093
@OptIn(ExperimentalWasmJsInterop::class)

0 commit comments

Comments
 (0)