From 79aa0341837e136013bb9bcd44409f1a943ac221 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 19 Sep 2026 14:32:49 +0300 Subject: [PATCH] native prefetch --- app/javascript/native.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/javascript/native.js b/app/javascript/native.js index d2898f582..ef6c086c1 100644 --- a/app/javascript/native.js +++ b/app/javascript/native.js @@ -113,6 +113,20 @@ if (handlers?.native && window === window.top) { handlers.modal.postMessage({ action: 'open', url: link.href, modal: link.closest('[data-native-modal]')?.dataset.nativeModal || '' }) }, true) + let isTouchPrefetch = false + + document.addEventListener('touchstart', (event) => { + isTouchPrefetch = true + event.target.closest('a[href]')?.dispatchEvent(new MouseEvent('mouseenter')) + isTouchPrefetch = false + }, { passive: true }) + + document.addEventListener('turbo:before-prefetch', (event) => { + if (!isTouchPrefetch || event.target.closest('a[data-turbo-frame="modal"], a[data-turbo-frame="drawer"]')) { + event.preventDefault() + } + }) + document.addEventListener('native:action', (event) => { document.querySelector(`[data-native-id="${event.detail.id}"]`)?.click() }) @@ -132,6 +146,8 @@ if (handlers?.native && window.Turbo) { this.snapshotCache.keys.forEach((key) => staleSnapshots.add(key)) } + window.Turbo.session.navigator.linkPrefetchingIsEnabledForLocation = () => !document.querySelector('native-modal') + document.addEventListener('turbo:submit-end', (e) => { if (e.detail.success && !e.detail.formSubmission.isSafe) { handlers.native.postMessage({ type: 'stale' })