diff --git a/src/react/use-outside-click.ts b/src/react/use-outside-click.ts index 706f94b..a0db82d 100644 --- a/src/react/use-outside-click.ts +++ b/src/react/use-outside-click.ts @@ -25,7 +25,7 @@ export function useOutsideClick( for (const { current: element } of refs) { // IMPORTANT: element.contains returns true when receives himself - if (element && element.contains(event.target)) { + if (element?.contains(event.target)) { return; } } diff --git a/src/router/browser-router.ts b/src/router/browser-router.ts index f109f6f..df01521 100644 --- a/src/router/browser-router.ts +++ b/src/router/browser-router.ts @@ -1,5 +1,5 @@ import type { Router, RouterLocation } from './types.ts'; -import { getStubLocation, normalizeLocation } from './utils.ts'; +import { getStubLocation, isEqualLocations, normalizeLocation } from './utils.ts'; export interface BrowserRouterConfig { /** Useful when you use BrowserRouter on server. */ @@ -48,9 +48,11 @@ export class BrowserRouter implements Router { const location = normalizeLocation(nextUrl); - window.history.pushState(null, '', `${location.pathname}${location.search}${location.hash}`); - window.scrollTo(0, 0); - this.setLocation(location); + if (!isEqualLocations(location, this.location)) { + window.history.pushState(null, '', `${location.pathname}${location.search}${location.hash}`); + window.scrollTo(0, 0); + this.setLocation(location); + } } go(delta: number): void { diff --git a/src/router/utils.ts b/src/router/utils.ts index eeaf351..bc69062 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -37,3 +37,14 @@ export function normalizeLocation(location: RouterLocation): RouterLocation { export function normalizePathname(pathname: string): string { return pathname.replace(/\/+$/, '') || '/'; } + +/** + * Checks that location is equal to other. + * @param a Location. + * @param b Location. + * @returns True if equal. + * @internal + */ +export function isEqualLocations(a: RouterLocation, b: RouterLocation): boolean { + return a.pathname === b.pathname && a.search === b.search && a.hash === b.hash; +}