Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/react/use-outside-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useOutsideClick<T extends Element>(

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;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/router/browser-router.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading