Skip to content

Commit 790c9f6

Browse files
pedroborgesreinink
andauthored
[2.x] Fix Svelte usePrefetch in SSR mode (#2034)
* Fix usePrefetch in SSR * Remove checks * Revert "Remove checks" This reverts commit 23bed01. --------- Co-authored-by: Jonathan Reinink <[email protected]>
1 parent 26f2db2 commit 790c9f6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/svelte/src/usePrefetch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export default function usePrefetch(options: VisitOptions = {}) {
77
const isPrefetching = writable(false)
88
const lastUpdatedAt = writable<number | null>(null)
99

10-
const cached = router.getCached(window.location.pathname, options)
11-
const inFlight = router.getPrefetching(window.location.pathname, options)
10+
const cached = typeof window === 'undefined' ? null : router.getCached(window.location.pathname, options)
11+
const inFlight = typeof window === 'undefined' ? null : router.getPrefetching(window.location.pathname, options)
1212

1313
isPrefetched.set(cached !== null)
1414
isPrefetching.set(inFlight !== null)
@@ -33,8 +33,13 @@ export default function usePrefetch(options: VisitOptions = {}) {
3333
})
3434

3535
onDestroy(() => {
36-
removePrefetchedListener()
37-
removePrefetchingListener()
36+
if (removePrefetchedListener) {
37+
removePrefetchedListener()
38+
}
39+
40+
if (removePrefetchingListener) {
41+
removePrefetchingListener()
42+
}
3843
})
3944

4045
return {

0 commit comments

Comments
 (0)