Is there currently a way to pass the URL that was ...
# compose-web
s
Is there currently a way to pass the URL that was called to my Compose App? If someone calls
<https://stefan-oltmann.de/oni-seed-browser/#myseed>
I want to get the
#myseed
part to show specific information.
1
r
If you have access to browser API (in
js
or
wasmJs
sourcesets) you can just use
document.location?.hash
s
Thank you. 🙂 It works for the first time the Compose for Web app loads. If I change it in the URL and press Enter it won't have an effect until I reload. Is there something I can do that changing the URL int he browser and pressing enter will update my Composable with the new value?
r
You should use some routing library with support for hash based routing.
Or just use
window.onhashchange = { }
s
Thanks, will look into that 🙂
Great, that's what I needed. 🙏
a
Thank you for the useful question and the answer. What about the case if I would like to handle urls with routing-compose: /page1 - as ordinary url path /page2 - redirection to page2#seed1 /page2#seed1 - as hashed url path /page2#seed2 - as hashed url path This snippet seems to work fine, but probably there are some possible optimisations?
Copy code
BrowserRouter(initPath = "/") {
    route("page1") {
        Text("page1")
    }
    route("page2") {
        HashRouter(initPath = "/") {
            redirect(route = arrayOf("/"), target = "seed1")
            route("seed1") {
                Text("page2#seed1")
            }
            route("seed2") {
                Text("page2#seed2")
            }
            noMatch {
                Text("no match for page2")
            }
        }
    }
    noMatch {
        Text("no match for page1")
    }
}