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

Feature request: property that indicates 'back' navigation #413

Open
rejhgadellaa opened this issue Jan 6, 2022 · 2 comments
Open

Feature request: property that indicates 'back' navigation #413

rejhgadellaa opened this issue Jan 6, 2022 · 2 comments

Comments

@rejhgadellaa
Copy link

I'm working on a project where it would be really useful to know if the user navigated forward (push state) or backwards (pop state).

I had a look at the source code and it looks like the popstate event listener could call routeTo with an additional parameter that indicates the route is caused by the history popping a state.

Something like:

addEventListener('popstate', () => {
  routeTo(getCurrentUrl(), { isBack: true }); 
});

The parameter could then be stored in state and passed as a prop to the component and the onChangeRoute callback.

I did a little testing and got it working quite easily (see diffs here). I could make it a PR if this is something worth adding to preact-router.

Thanks!

@developit
Copy link
Member

I think this would be a nice addition. The 'back' concept gets a bit murky when the user uses forward history navigation, since that also fires the popstate event. The new App History API provides an explicit indicator for this, but it's only available in Chrome currently.

@TommyDew42
Copy link

Do we wanna use the new App History API to implement the new feature?

Happy to work on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants