Skip to content

Commit

Permalink
Ensure that a snapshot is cached before navigating to a new location
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed Feb 23, 2024
1 parent c0d05e7 commit af827c0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions turbo/src/main/assets/js/turbo_bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
}
}

cacheSnapshot() {
if (window.Turbo) {
Turbo.session.view.cacheSnapshot()
}
}

// Current visit

issueRequestForVisitWithIdentifier(identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ abstract class TurboWebFragment : TurboFragment(), TurboWebFragmentCallback {
}
}

override fun onBeforeNavigation() {
super.onBeforeNavigation()
webDelegate.onBeforeNavigation()
}

override fun refresh(displayProgress: Boolean) {
webDelegate.refresh(displayProgress)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions turbo/src/main/kotlin/dev/hotwire/turbo/views/TurboWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit af827c0

Please sign in to comment.