Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that a snapshot is cached before navigating to a new location #303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading