Skip to content

Commit

Permalink
revert previous changes and modify navigate to resolve destinations l…
Browse files Browse the repository at this point in the history
…ike "/abc?def" to "?def#/abc"
  • Loading branch information
GravityTwoG committed Jul 3, 2024
1 parent 35ce210 commit 609cc5e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 73 deletions.
49 changes: 14 additions & 35 deletions packages/wouter/src/use-hash-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,31 @@ const subscribeToHashUpdates = (callback) => {
};
};

export const useLocationProperty = (fn, ssrFn) =>
useSyncExternalStore(subscribeToHashUpdates, fn, ssrFn);

// when useHashLocation is used, location.search is always empty
// so we must retrieve string from the hash
const currentSearch = () => {
const hash = location.hash;
const hashLocation = "/" + hash.replace(/^#?\/?/, "");

const questionMarkIdx = hashLocation.indexOf("?");
if (questionMarkIdx !== -1) {
return hashLocation.slice(questionMarkIdx + 1, hashLocation.length);
}

return "";
};

export const useSearch = ({ ssrSearch = "" } = {}) =>
useLocationProperty(currentSearch, () => ssrSearch);

// leading '#' is ignored, leading '/' is optional
const currentHashLocation = () => {
const hash = location.hash;
const hashLocation = "/" + hash.replace(/^#?\/?/, "");

// remove query string
const questionMarkIdx = hashLocation.indexOf("?");
if (questionMarkIdx !== -1) {
return hashLocation.slice(0, questionMarkIdx);
}

return hashLocation;
};
const currentHashLocation = () => "/" + location.hash.replace(/^#?\/?/, "");

export const navigate = (to, { state = null } = {}) => {
// calling `replaceState` allows us to set the history
// state without creating an extra entry

let hash = to.replace(/^#?\/?/, "");
let search = location.search;

const searchIdx = hash.indexOf("?");
if (searchIdx !== -1) {
search = hash.slice(searchIdx, hash.length);
hash = hash.slice(0, searchIdx);
}

history.replaceState(
state,
"",
// keep the current pathname, current query string, but replace the hash
// keep the current pathname, but replace query string and hash
location.pathname +
location.search +
search +
// update location hash, this will cause `hashchange` event to fire
// normalise the value before updating, so it's always preceeded with "#/"
(location.hash = `#/${to.replace(/^#?\/?/, "")}`)
(location.hash = `#/${hash}`)
);
};

Expand Down
23 changes: 1 addition & 22 deletions packages/wouter/test/use-hash-location.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { renderHook, render } from "@testing-library/react";
import { renderToStaticMarkup } from "react-dom/server";

import { Router, Route, useLocation, Link } from "wouter";
import { useHashLocation, useSearch } from "wouter/use-hash-location";
import { useHashLocation } from "wouter/use-hash-location";

import { waitForHashChangeEvent } from "./test-utils";
import { ReactNode, useSyncExternalStore } from "react";
Expand All @@ -29,14 +29,6 @@ it("isn't sensitive to leading slash", () => {
expect(path).toBe("/app/users");
});

it("isn't sensitive to query string", () => {
location.hash = "/app/users?foo=bar";
const { result } = renderHook(() => useHashLocation());
const [path] = result.current;

expect(path).toBe("/app/users");
});

it("rerenders when hash changes", async () => {
const { result } = renderHook(() => useHashLocation());

Expand Down Expand Up @@ -204,16 +196,3 @@ it("defines a custom way of rendering link hrefs", () => {

expect(getByTestId("link")).toHaveAttribute("href", "#/app");
});

it("useSearch returns correct query string when useHashLocation is used", () => {
location.hash = "/";
const { result } = renderHook(() => useHashLocation());
const [, navigate] = result.current;

navigate("/?hello=world");
const {
result: { current: search },
} = renderHook(() => useSearch());

expect(search).toBe("hello=world");
});
9 changes: 1 addition & 8 deletions packages/wouter/types/use-browser-location.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { Path, SearchString } from "./location-hook.js";

export type Primitive =
| string
| number
| bigint
| boolean
| null
| undefined
| symbol;
type Primitive = string | number | bigint | boolean | null | undefined | symbol;
export const useLocationProperty: <S extends Primitive>(
fn: () => S,
ssrFn?: () => S
Expand Down
8 changes: 0 additions & 8 deletions packages/wouter/types/use-hash-location.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { Path } from "./location-hook.js";
import { BrowserSearchHook, Primitive } from "./use-browser-location.js";

export function navigate<S = any>(to: Path, options?: { state: S }): void;

export const useLocationProperty: <S extends Primitive>(
fn: () => S,
ssrFn?: () => S
) => S;

export const useSearch: BrowserSearchHook;

export function useHashLocation(options?: {
ssrPath?: Path;
}): [Path, typeof navigate];

0 comments on commit 609cc5e

Please sign in to comment.