Skip to content

Commit cadfedc

Browse files
joetannenbaumWendellAdrielmatthieumota
authored
Fix SSR window reference error in current URL hooks (#313)
Co-authored-by: WendellAdriel <11641518+WendellAdriel@users.noreply.github.com> Co-authored-by: Matthieu Mota <matthieumota@users.noreply.github.com>
1 parent f4d019e commit cadfedc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

resources/js/hooks/use-current-url.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export type UseCurrentUrlReturn = {
2828

2929
export function useCurrentUrl(): UseCurrentUrlReturn {
3030
const page = usePage();
31-
const currentUrlPath = new URL(page.url, window?.location.origin).pathname;
31+
const currentUrlPath = new URL(
32+
page.url,
33+
typeof window !== 'undefined'
34+
? window.location.origin
35+
: 'http://localhost',
36+
).pathname;
3237

3338
const isCurrentUrl: IsCurrentUrlFn = (
3439
urlToCheck: NonNullable<InertiaLinkProps['href']>,
@@ -47,6 +52,7 @@ export function useCurrentUrl(): UseCurrentUrlReturn {
4752

4853
try {
4954
const absoluteUrl = new URL(urlString);
55+
5056
return comparePath(absoluteUrl.pathname);
5157
} catch {
5258
return false;

0 commit comments

Comments
 (0)