-
Notifications
You must be signed in to change notification settings - Fork 434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add InstantClick behavior #1101
Changes from all commits
273456a
1df1428
2f8c272
de3ffcf
94ff0e1
e662690
15517d4
ab0e538
fca147e
3baf81c
d2ec021
7c7ded9
806aa9d
39e3dda
9cdff6d
368225a
3e75161
b9e82f2
ab01099
c735036
b27bc42
4c86295
4170ddb
5078e0b
3d2665a
3a724ca
efd58fb
10e1311
5326a0f
a551b1f
ebea951
aa6f013
851dabb
ccab6b2
06181be
0704cef
80524d5
c975a7b
e87bf75
1b376c8
c944326
63908d0
1c9b41c
ac310e2
70935c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { FetchRequest, FetchMethod, fetchMethodFromString, fetchEnctypeFromStrin | |
import { expandURL } from "../url" | ||
import { clearBusyState, dispatch, getAttribute, getMetaContent, hasAttribute, markAsBusy } from "../../util" | ||
import { StreamMessage } from "../streams/stream_message" | ||
import { prefetchCache } from "./prefetch_cache" | ||
|
||
export const FormSubmissionState = { | ||
initialized: "initialized", | ||
|
@@ -126,13 +127,20 @@ export class FormSubmission { | |
} | ||
|
||
requestPreventedHandlingResponse(request, response) { | ||
prefetchCache.clear() | ||
|
||
this.result = { success: response.succeeded, fetchResponse: response } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if the event is default prevented after a form submission, I think we should still clear the cache to avoid showing stale content. |
||
|
||
requestSucceededWithResponse(request, response) { | ||
if (response.clientError || response.serverError) { | ||
this.delegate.formSubmissionFailedWithResponse(this, response) | ||
} else if (this.requestMustRedirect(request) && responseSucceededWithoutRedirect(response)) { | ||
return | ||
} | ||
|
||
prefetchCache.clear() | ||
|
||
if (this.requestMustRedirect(request) && responseSucceededWithoutRedirect(response)) { | ||
const error = new Error("Form responses must redirect to another location") | ||
this.delegate.formSubmissionErrored(this, error) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's a client or server error, we don't clear the cache, as we can assume no mutation happened. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to enable touchscreen for existing devices, instead of adding a new device (e.g.
iPhone 13
), because we could still test the screen taps while avoiding extra test runs on another device.