diff --git a/turbo/src/main/assets/js/turbo_bridge.js b/turbo/src/main/assets/js/turbo_bridge.js index 7bc8bd15..8e23f1e4 100644 --- a/turbo/src/main/assets/js/turbo_bridge.js +++ b/turbo/src/main/assets/js/turbo_bridge.js @@ -57,6 +57,12 @@ } } + cacheSnapshot() { + if (window.Turbo) { + Turbo.session.view.cacheSnapshot() + } + } + // Current visit issueRequestForVisitWithIdentifier(identifier) { diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt index 40279e8e..0d1c15dc 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboWebFragmentDelegate.kt @@ -109,6 +109,14 @@ internal class TurboWebFragmentDelegate( initWebChromeClient() } + /** + * Should be called by the implementing Fragment during + * [dev.hotwire.turbo.nav.TurboNavDestination.onBeforeNavigation] + */ + fun onBeforeNavigation() { + session().cacheSnapshot(location) + } + /** * Provides a hook to Turbo when the dialog has been canceled. Detaches the WebView * before navigation. diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt index 79549a35..b0d2404e 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/fragments/TurboWebFragment.kt @@ -66,6 +66,11 @@ abstract class TurboWebFragment : TurboFragment(), TurboWebFragmentCallback { } } + override fun onBeforeNavigation() { + super.onBeforeNavigation() + webDelegate.onBeforeNavigation() + } + override fun refresh(displayProgress: Boolean) { webDelegate.refresh(displayProgress) } diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/session/TurboSession.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/session/TurboSession.kt index 7687ebf7..d2cae112 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/session/TurboSession.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/session/TurboSession.kt @@ -14,6 +14,7 @@ import androidx.webkit.WebResourceErrorCompat import androidx.webkit.WebViewClientCompat import androidx.webkit.WebViewCompat import androidx.webkit.WebViewFeature.* +import dev.hotwire.turbo.config.Turbo import dev.hotwire.turbo.config.TurboPathConfiguration import dev.hotwire.turbo.config.screenshotsEnabled import dev.hotwire.turbo.delegates.TurboFileChooserDelegate @@ -142,6 +143,13 @@ class TurboSession internal constructor( } } + internal fun cacheSnapshot(location: String) { + if (this.currentVisit?.location == location) { + logEvent("cachingSnapshot", "location" to location) + webView.cacheSnapshot() + } + } + /** * Synthetically restore the WebView's current visit without using a cached snapshot or a * visit request. This is used when restoring a Fragment destination from the backstack, diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt index 61af2d91..59f74082 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt @@ -59,6 +59,10 @@ open class TurboWebView @JvmOverloads constructor(context: Context, attrs: Attri runJavascript("turboNative.visitRenderedForColdBoot('$coldBootVisitIdentifier')") } + internal fun cacheSnapshot() { + runJavascript("turboNative.cacheSnapshot()") + } + internal fun installBridge(onBridgeInstalled: () -> Unit) { val script = "window.turboNative == null" val bridge = context.contentFromAsset("js/turbo_bridge.js")