preactjs / preact-router

:earth_americas: URL router for Preact.

Home Page:http://npm.im/preact-router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: property that indicates 'back' navigation

rejhgadellaa opened this issue · comments

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!

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.

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

Happy to work on it!