-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
How to add go back action? #43
Comments
Navaid will still listen/react when you call |
Thanks for the response!
I noticed that :) But if I enter my site and call that function, it may do nothing, because there is no history, or it may take me to the previous site (in a different domain) or a blank page. I would like to know how to avoid leaving my site with this in-app back button. Something like: function goBack (fallback) {
if (canGoBack) { // how do I get this?
// the previous history is in my site
history.back()
} else {
// there is no history or it would leave my site
router.route(fallback, true)
}
} |
You should use some sort of state/boolean flag when your application boots up, so that you know it's on the page. The var startAt;
function onAppInit() {
// ...
startAt = history.length || 0;
}
function goBack(fallback) {
if (history.length - startAt > 0) {
// ...
}
} If you don't want to do this, you have to look for a Navaid side effect. For example, if (and only if) you call function goBack() {
if (!!history.push) { /* ... */ }
// or
if (history.push === 'push') { /* ... */ }
} However, the issue with this is that it's (a) set immediately and (b) if this |
Storing the initial state like this won't work because once the window goes to the previous history entry, either by clicking the native back button or calling I was checking this stackoverflow post and I tried to use the The best answer for me on that post is https://stackoverflow.com/a/62658623, that puts a flag in the history itself. It worked great, but I had to wrap navaid's Is there a way for navaid supporting custom state to be passed in the first parameter here? Or even better handle this state internally and expose a |
It will only stay the same if you use Even then, you can save the initial var startLen, startPath;
function onAppInit() {
// ...
startLen = history.length || 0;
startPath = location.pathname;
}
function goBack(fallback) {
if (history.length - startLen > 0 || location.pathname !== startPath) {
// ...
}
} Sorry but I don't want to add anything, but mostly because I don't think it needs to be added. A library shouldn't need to accomodate every use case, especially when it builds on top of platform primitives. This more-or-less boils down to a simple, application-specific |
It's okay, I understand.
Wow, I didn't think I could do that. At first I thought it would go to the parent path of the current route, but it seems to be going to I am planning to maintain a fork with the changes I need just because I will be using it in multiple projects. |
No problem re: forking :) Ah sorry, the |
Oh, I see. In my case, I had to use If // setup flag before listen() to avoid double on() handle
history.replaceState({ root: true }, '', location.pathname)
router.listen()
function goBack (fallback) {
if (history.state && history.state.root) {
router.route(fallback || '/', true, { root: true })
} else {
history.back()
}
} Maybe this is a use case for #6 :D Of course, this does not cover the SPA setting with redirects for login, because the function navigate (uri, replace) {
if (replace && history.state && history.state.root) {
history.replaceState({ root: true }, '', uri)
} else {
router.route(uri, replace)
}
} It still fails if I call this |
I would like to add an in-app back button that should go back in the history, but if it would leave my site it should call
history.replaceState
to a fallback instead.For example, if I click on a link to
example.com/products/123
, the in-app back button would work asrouter.route('/products', true)
, but if I was already at any page in that site and navigated to that route, the button would work ashistory.back()
.The falback uri would be different depending on the page and should be specified, but it could default to
/
.The text was updated successfully, but these errors were encountered: