preact-router icon indicating copy to clipboard operation
preact-router copied to clipboard

Feature request: property that indicates 'back' navigation

Open rejhgadellaa opened this issue 3 years ago • 2 comments
trafficstars

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!

rejhgadellaa avatar Jan 06 '22 15:01 rejhgadellaa

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.

developit avatar Jan 18 '22 04:01 developit

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

Happy to work on it!

TommyDew42 avatar May 22 '22 07:05 TommyDew42