-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only cache the link you're currently hovering
Instead of maintaining a cache of all the links that have been hovered in the last 10 seconds. This solves issues where the user hovers a link, then performs a non-safe action and then later clicks the link. In this case, we would be showing stale content from before the action was performed.
- Loading branch information
Showing
8 changed files
with
64 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
export const prefetchCache = new Map() | ||
|
||
const PREFETCH_DELAY = 100 | ||
|
||
class PrefetchCache { | ||
#prefetchTimeout = null | ||
#prefetched = null | ||
|
||
get(url) { | ||
if (this.#prefetched && this.#prefetched.url === url && this.#prefetched.expire > Date.now()) { | ||
return this.#prefetched.request | ||
} | ||
} | ||
|
||
setLater(url, request, ttl) { | ||
this.clear() | ||
|
||
this.#prefetchTimeout = setTimeout(() => { | ||
request.perform() | ||
this.set(url, request, ttl) | ||
this.#prefetchTimeout = null | ||
}, PREFETCH_DELAY) | ||
} | ||
|
||
set(url, request, ttl) { | ||
this.#prefetched = { url, request, expire: new Date(new Date().getTime() + ttl) } | ||
} | ||
|
||
clear() { | ||
if (this.#prefetchTimeout) clearTimeout(this.#prefetchTimeout) | ||
this.#prefetched = null | ||
} | ||
} | ||
|
||
export const cacheTtl = 10 * 1000 | ||
export const prefetchCache = new PrefetchCache() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.