Skip to content

Commit

Permalink
[2.x] Fix Svelte usePrefetch in SSR mode (#2034)
Browse files Browse the repository at this point in the history
* Fix usePrefetch in SSR

* Remove checks

* Revert "Remove checks"

This reverts commit 23bed01.

---------

Co-authored-by: Jonathan Reinink <[email protected]>
  • Loading branch information
pedroborges and reinink authored Oct 18, 2024
1 parent 26f2db2 commit 790c9f6
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 790c9f6

Please sign in to comment.