Skip to content
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

Verify with the browser adapter before initiating link prefetching #1354

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core/drive/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ export class Navigator {
}
}

// Link prefetching

linkPrefetchingIsEnabledForLocation(location) {
// Not all adapters implement linkPrefetchingIsEnabledForLocation
if (typeof this.adapter.linkPrefetchingIsEnabledForLocation === "function") {
return this.adapter.linkPrefetchingIsEnabledForLocation(location)
}

return true
}

// Visit delegate

visitStarted(visit) {
Expand Down
6 changes: 6 additions & 0 deletions src/core/native/browser_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export class BrowserAdapter {

visitRendered(_visit) {}

// Link prefetching

linkPrefetchingIsEnabledForLocation(location) {
return true
}

// Form Submission Delegate

formSubmissionStarted(_formSubmission) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ export class Session {
canPrefetchRequestToLocation(link, location) {
return (
this.elementIsNavigatable(link) &&
locationIsVisitable(location, this.snapshot.rootLocation)
locationIsVisitable(location, this.snapshot.rootLocation) &&
this.navigator.linkPrefetchingIsEnabledForLocation(location)
)
}

Expand Down
15 changes: 15 additions & 0 deletions src/tests/unit/native_adapter_support_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NativeAdapterSupportTest {
finishedVisitRequests = []
startedFormSubmissions = []
finishedFormSubmissions = []
linkPrefetchRequests = []

// Adapter interface

Expand Down Expand Up @@ -53,6 +54,10 @@ class NativeAdapterSupportTest {
}

pageInvalidated() {}

linkPrefetchingIsEnabledForLocation(location) {
this.linkPrefetchRequests.push(location)
}
}

let adapter
Expand Down Expand Up @@ -204,3 +209,13 @@ test("visit follows redirect and proposes replace visit to adapter", async () =>
assert.equal(visit.location, redirectedLocation)
assert.equal(visit.options.action, "replace")
})

test ("link prefetch requests verify with adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.linkPrefetchingIsEnabledForLocation(locatable)
assert.equal(adapter.linkPrefetchRequests.length, 1)

const [location] = adapter.linkPrefetchRequests
assert.equal(location, locatable)
})