Skip to content

Commit eabd7f3

Browse files
committed
fix(wasmJs): prevent WebView content reload on window resize
The Updater lambda was unconditionally setting iframe.src/srcdoc on every recomposition (triggered by onGloballyPositioned during resize), causing the WebView to lose its state and reload. Track last applied content and skip re-applying when unchanged.
1 parent 4914a73 commit eabd7f3

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

  • webview-compose/src/wasmJsMain/kotlin/io/github/kdroidfilter/webview/web

webview-compose/src/wasmJsMain/kotlin/io/github/kdroidfilter/webview/web/HtmlView.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ fun HtmlView(
9393
state.htmlElement = componentInfo.component
9494
onCreated(componentInfo.component)
9595

96+
var lastAppliedContent: HtmlContent? = null
97+
9698
componentInfo.updater = Updater(componentInfo.component) { iframe: HTMLIFrameElement ->
9799
if (!eventsInitialized.value) {
98100
eventsInitialized.value = true
@@ -149,24 +151,28 @@ fun HtmlView(
149151
}
150152
}
151153

152-
when (val content = state.content) {
153-
is HtmlContent.Url -> {
154-
iframe.src = content.url
155-
state.loadingState = HtmlLoadingState.Loading
156-
}
154+
val content = state.content
155+
if (content != lastAppliedContent) {
156+
lastAppliedContent = content
157+
when (content) {
158+
is HtmlContent.Url -> {
159+
iframe.src = content.url
160+
state.loadingState = HtmlLoadingState.Loading
161+
}
157162

158-
is HtmlContent.Data -> {
159-
iframe.srcdoc = content.data
160-
state.loadingState = HtmlLoadingState.Loading
161-
addContentIdentifierJs(iframe)
162-
}
163+
is HtmlContent.Data -> {
164+
iframe.srcdoc = content.data
165+
state.loadingState = HtmlLoadingState.Loading
166+
addContentIdentifierJs(iframe)
167+
}
163168

164-
is HtmlContent.Post -> {
165-
// POST requests not directly supported in iframe
166-
}
169+
is HtmlContent.Post -> {
170+
// POST requests not directly supported in iframe
171+
}
167172

168-
HtmlContent.NavigatorOnly -> {
169-
// No content update needed
173+
HtmlContent.NavigatorOnly -> {
174+
// No content update needed
175+
}
170176
}
171177
}
172178

0 commit comments

Comments
 (0)