Skip to content

Commit

Permalink
Fix issue with ballast-navigation where browser back button may resul…
Browse files Browse the repository at this point in the history
…t in `/null` in address bar
  • Loading branch information
cjbrooks12 committed Feb 12, 2024
1 parent d0ca9fd commit c6e03e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Expand Up @@ -16,7 +16,7 @@ public class BrowserHashNavigationInterceptor<T : Route>(
val hashValue = window.location.hash.trim().trimStart('#').trimStart('/')
val hashPieces = hashValue.split('?')

val (initialPath: String?, initialQueryString: String?) = if(hashPieces.size == 2) {
val (initialPath: String?, initialQueryString: String?) = if (hashPieces.size == 2) {
// we have path and query in the hash value
val path = hashPieces[0].takeIf { it.isNotBlank() }
val query = hashPieces[1].takeIf { it.isNotBlank() }
Expand All @@ -41,7 +41,10 @@ public class BrowserHashNavigationInterceptor<T : Route>(
override fun watchForUrlChanges(): Flow<Uri> {
return callbackFlow<Uri> {
window.onhashchange = { event: HashChangeEvent ->
this@callbackFlow.trySend(Uri.parse(event.newURL.split("#").last()))
val partAfterHash = event.newURL?.split("#")?.last()
if (!partAfterHash.isNullOrBlank()) {
this@callbackFlow.trySend(Uri.parse(partAfterHash))
}
Unit
}

Expand Down
Expand Up @@ -37,7 +37,9 @@ public class BrowserHistoryNavigationInterceptor<T : Route>(
override fun watchForUrlChanges(): Flow<Uri> {
return callbackFlow {
window.onpopstate = { event: PopStateEvent ->
this@callbackFlow.trySend(Uri.parse(event.state.toString()))
if(event.state != null) {
this@callbackFlow.trySend(Uri.parse(event.state.toString()))
}
Unit
}

Expand Down

0 comments on commit c6e03e9

Please sign in to comment.