Skip to content

Commit d1a4155

Browse files
Maxi937eymar
authored andcommitted
CMP-9545: Cache Storage API extra check for protocol (#5516)
Compose Multiplatform v1.10.0 introduced the use of the Cache Storage API to avoid repeat http requests for resources on Web ([#5379](#5379)). In a VsCode Extension Environment, the protocol of a Web View Panel that gets used is "vscode-webview:". This does not work with the Cache Storage API and results in an error when trying to load any compose resources. This PR just ensures the protocol is http/https in order to use Cache Storage API on Web Target. Fixes https://youtrack.jetbrains.com/issue/CMP-9545/ComposeResources-on-Web-not-loading-because-Cache-Storage-API-is-not-supported-everywhere ## Testing **Describe how you tested your changes (provide a snippet or/and steps)** - I ran my vscode extension with the change and without the change **without** <img width="1658" height="753" alt="image" src="https://github.com/user-attachments/assets/367f5a41-4b0b-4262-9d38-3baca1ce0125" /> **with** <img width="1658" height="885" alt="image" src="https://github.com/user-attachments/assets/b06415df-1388-45c2-a439-21e604fc2e54" /> - I ran a regular browserDevelopmentRun to ensure caching still worked as expected <img width="1863" height="942" alt="image" src="https://github.com/user-attachments/assets/54e7dd47-084c-4514-b79b-32f35577dc92" /> ## 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). (cherry picked from commit 89f6aea)
1 parent ed8a2c1 commit d1a4155

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)