Skip to content

Commit

Permalink
Fix usePrefetch in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroborges committed Oct 15, 2024
1 parent 88a765b commit 611c7d5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/svelte/src/usePrefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export default function usePrefetch(options: VisitOptions = {}) {
const isPrefetching = writable(false)
const lastUpdatedAt = writable<number | null>(null)

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

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

onDestroy(() => {
removePrefetchedListener()
removePrefetchingListener()
if (removePrefetchedListener) {
removePrefetchedListener()
}

if (removePrefetchingListener) {
removePrefetchingListener()
}
})

return {
Expand Down

0 comments on commit 611c7d5

Please sign in to comment.