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

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Next Next commit
Use history.replaceState for route function
  • Loading branch information
piotr-cz committed Mar 9, 2021
commit 56f83c868fd1b53de10b2db1b35337c4c65e35b8
10 changes: 9 additions & 1 deletion packages/preact-iso/router.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { h, createContext, cloneElement } from 'preact';
import { useContext, useMemo, useReducer, useEffect, useLayoutEffect, useRef } from 'preact/hooks';

const UPDATE = (state, url, push) => {
const UPDATE = (state, url) => {
/** @type {boolean|undefined} - History state update strategy */
let push = undefined

// user click (Mouse event)
if (url && url.type === 'click') {
const link = url.target.closest('a[href]');
if (!link || link.origin != location.origin) return state;

url.preventDefault();
push = true;
url = link.href.replace(location.origin, '');
// navigation (PopStateEvent)
} else if (typeof url !== 'string') {
url = location.pathname + location.search;
// manual invocation (useLocation().route)
} else {
push = false
}

if (push === true) history.pushState(null, '', url);
Expand Down