Skip to content

Commit 924efbb

Browse files
authored
CMP-9545: Cache Storage API extra check for protocol (#5516) (#5518)
Cherry pick from #5516 Fixes https://youtrack.jetbrains.com/issue/CMP-9545/ComposeResources-on-Web-not-loading-because-Cache-Storage-API-is-not-supported-everywhere ## Testing This should be tested by QA ## Release Notes ### Fixes - Resources Fixes an issue where web resources failed to load when calling the Cache Storage API with unsupported protocols (e.g., vscode-webview: in VS Code webviews).
2 parents ed8a2c1 + d1a4155 commit 924efbb

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal object ResourceWebCache {
3737
// A mutex to avoid multiple cache reset
3838
private val resetMutex = Mutex()
3939

40-
private val supportsCacheApi: Boolean by lazy { supportsCacheApi() }
40+
private val supportsCacheApi: Boolean by lazy { supportsCacheApi() && isCacheableProtocol() }
4141

4242
suspend fun load(path: String, onNoCacheHit: suspend (path: String) -> Response): Response {
4343
if (!supportsCacheApi) return onNoCacheHit(path)
@@ -88,6 +88,10 @@ internal object ResourceWebCache {
8888
// Supported only in secure contexts (HTTPS or localhost)
8989
private fun supportsCacheApi(): Boolean = js("Boolean(window.caches)")
9090

91+
// https://developer.mozilla.org/en-US/docs/Web/API/Cache/put
92+
// Ensure the protocol of the request is http / https - edge cases like "vscode-webview:" are not supported by CacheStorage
93+
private fun isCacheableProtocol(): Boolean = window.location.protocol.startsWith("http")
94+
9195
// Promise.await is not yet available in webMain: https://github.com/Kotlin/kotlinx.coroutines/issues/4544
9296
// TODO(o.karpovich): get rid of this function, when kotlinx-coroutines provide Promise.await in webMain out of a box
9397
@OptIn(ExperimentalWasmJsInterop::class)

0 commit comments

Comments
 (0)