wmr icon indicating copy to clipboard operation
wmr copied to clipboard

(preact-iso): Use pushState/ replaceState for useLocation().route method

Open piotr-cz opened this issue 4 years ago • 10 comments

At this moment when using useLocation().route('/foobar') window location doesn't change: neither history.pushState nor history.replaceState method is used.

This pull request

  1. changes method behavior in that window location will be added to history via pushState.
  2. allows using object as a parameter so it's possible to use replaceState
// Use history.pushState
route('/foobar);
route({ url: '/foobar' });
route({ url: '/foobar', replace: false });

// Use history.replaceState
route({ url: '/foobar', replace: true });

Originally the useReducer hook dispatcher had a third push argument, but dispatchers consume only two arguments. https://github.com/preactjs/wmr/blob/58f1bffd108f45c1ac5759f744f484b5d6a8fcca/packages/preact-iso/router.js#L4

I'm not sure if I should bump patch or minor version with changeset?

piotr-cz avatar Mar 09 '21 11:03 piotr-cz

🦋 Changeset detected

Latest commit: 3d0d9f47d192fce8f2ce954520f0e60f2318a10c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
preact-iso Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Mar 09 '21 11:03 changeset-bot[bot]

@developit Unfortunately passing replace flag as additional argument doesn't work, as route is actually a reducers dispatcher, which accepts only one argument (see docs > useReducer):

https://github.com/preactjs/wmr/blob/58f1bffd108f45c1ac5759f744f484b5d6a8fcca/packages/preact-iso/router.js#L44-L51

Probably we would have to refactor router code to not use reducer in this case (which may be not a bad idea to handle history outside/ without reducer).

piotr-cz avatar Mar 09 '21 18:03 piotr-cz

I prefer route(url, replace) as well, but as long as there is no better solution, let's keep this PR opened for reference

piotr-cz avatar Mar 11 '21 09:03 piotr-cz

I've created new PR in #424 to fix the bug with location change after using useLocation().route(path).

After it's resolved it will be easier to move forward with adding an option such as route(path, replace) either with this PR new one.

piotr-cz avatar Mar 12 '21 09:03 piotr-cz

Nice - thanks for pulling that out, just merged it.

developit avatar Mar 14 '21 19:03 developit

I've updated PR to use code suggested in https://github.com/preactjs/wmr/pull/413#discussion_r593948478

However I'm not happy with the route({ url, replace }) signature (proposed by me) and would prefer route(url, replace) instead. Let's not merge this PR yet, we can do better.

piotr-cz avatar Mar 15 '21 10:03 piotr-cz

@piotr-cz I think we might be able to create a wrapper function in the provider that swaps the argument signature:

-	const [url, route] = useReducer(UPDATE, location.pathname + location.search);
+	const [url, doRoute] = useReducer(UPDATE, location.pathname + location.search);
+	const route = useCallback((url, replace) => doRoute({ url, replace }), []);

It might even be possible to simplify the history event handling this way too.

developit avatar Mar 19 '21 17:03 developit

@developit Updated, thank you for suggestion!

piotr-cz avatar Mar 22 '21 09:03 piotr-cz

Just one question: Should I add changeset with patch or minor version bump? As the route method is not documented, I guess patch is enough.

piotr-cz avatar Mar 24 '21 09:03 piotr-cz

I'd go for patch yea

JoviDeCroock avatar Mar 24 '21 09:03 JoviDeCroock