Skip to content

Commit ac2a40f

Browse files
committed
fix: resolve SSR hydration mismatch when ssrPath/ssrSearch not provided
When rendering on the client without ssrPath/ssrSearch props, usePathname and useSearch now use location.pathname and location.search as the SSR snapshot instead of defaulting to empty strings. This ensures the client-side hydration matches what was rendered on the server.
1 parent 2c4ccb9 commit ac2a40f

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎packages/wouter/src/use-browser-location.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ export const useLocationProperty = (fn, ssrFn) =>
3030

3131
const currentSearch = () => location.search;
3232

33-
export const useSearch = ({ ssrSearch = "" } = {}) =>
34-
useLocationProperty(currentSearch, () => ssrSearch);
33+
export const useSearch = ({ ssrSearch } = {}) =>
34+
useLocationProperty(
35+
currentSearch,
36+
ssrSearch != null ? () => ssrSearch : currentSearch
37+
);
3538

3639
const currentPathname = () => location.pathname;
3740

3841
export const usePathname = ({ ssrPath } = {}) =>
3942
useLocationProperty(
4043
currentPathname,
41-
ssrPath ? () => ssrPath : currentPathname
44+
ssrPath != null ? () => ssrPath : currentPathname
4245
);
4346

4447
const currentHistoryState = () => history.state;

0 commit comments

Comments
 (0)