diff --git a/src/core/drive/navigator.js b/src/core/drive/navigator.js index 8210c7471..26fcd3d16 100644 --- a/src/core/drive/navigator.js +++ b/src/core/drive/navigator.js @@ -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) { diff --git a/src/core/native/browser_adapter.js b/src/core/native/browser_adapter.js index 6f4c73bfe..7eb8a4d8b 100644 --- a/src/core/native/browser_adapter.js +++ b/src/core/native/browser_adapter.js @@ -72,6 +72,12 @@ export class BrowserAdapter { visitRendered(_visit) {} + // Link prefetching + + linkPrefetchingIsEnabledForLocation(location) { + return true + } + // Form Submission Delegate formSubmissionStarted(_formSubmission) { diff --git a/src/core/session.js b/src/core/session.js index 2fb9804d5..634bb5634 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -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) ) } diff --git a/src/tests/unit/native_adapter_support_tests.js b/src/tests/unit/native_adapter_support_tests.js index 6c8c362d0..19dc0d28a 100644 --- a/src/tests/unit/native_adapter_support_tests.js +++ b/src/tests/unit/native_adapter_support_tests.js @@ -11,6 +11,7 @@ class NativeAdapterSupportTest { finishedVisitRequests = [] startedFormSubmissions = [] finishedFormSubmissions = [] + linkPrefetchRequests = [] // Adapter interface @@ -53,6 +54,10 @@ class NativeAdapterSupportTest { } pageInvalidated() {} + + linkPrefetchingIsEnabledForLocation(location) { + this.linkPrefetchRequests.push(location) + } } let adapter @@ -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) +})