|
| 1 | +const requestCache = new Map() |
| 2 | +const cacheTtl = 10 * 1000 |
| 3 | + |
| 4 | +function isPreloadable (linkElement) { |
| 5 | + const href = linkElement.getAttribute('href') |
| 6 | + |
| 7 | + if (!href || href === '#' || linkElement.dataset.turbo === 'false' || linkElement.dataset.prefetch === 'false') { |
| 8 | + return |
| 9 | + } |
| 10 | + |
| 11 | + if (linkElement.origin !== document.location.origin) { |
| 12 | + return |
| 13 | + } |
| 14 | + |
| 15 | + if (!['http:', 'https:'].includes(linkElement.protocol)) { |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + if (linkElement.pathname + linkElement.search === document.location.pathname + document.location.search) { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + if (linkElement.dataset.turboMethod && linkElement.dataset.turboMethod !== 'get') { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + return true |
| 28 | +} |
| 29 | + |
| 30 | +function mouseoverListener (event) { |
| 31 | + let linkElement |
| 32 | + |
| 33 | + if (event.target.tagName === 'A') { |
| 34 | + linkElement = event.target |
| 35 | + } else { |
| 36 | + linkElement = event.target.closest('a') |
| 37 | + } |
| 38 | + |
| 39 | + if (!linkElement) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if (!isPreloadable(linkElement)) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + const url = linkElement.getAttribute('href') |
| 48 | + const loc = new URL(url, location.protocol + '//' + location.host) |
| 49 | + const absoluteUrl = loc.toString() |
| 50 | + |
| 51 | + const cached = requestCache.get(absoluteUrl) |
| 52 | + |
| 53 | + if (cached && cached.ttl > new Date()) { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + const requestOptions = { |
| 58 | + credentials: 'same-origin', |
| 59 | + headers: { Accept: 'text/html, application/xhtml+xml', 'VND.PREFETCH': 'true' }, |
| 60 | + method: 'GET', |
| 61 | + redirect: 'follow' |
| 62 | + } |
| 63 | + |
| 64 | + if (linkElement.dataset.turboFrame && linkElement.dataset.turboFrame !== '_top') { |
| 65 | + requestOptions.headers['Turbo-Frame'] = linkElement.dataset.turboFrame |
| 66 | + } else if (linkElement.dataset.turboFrame !== '_top') { |
| 67 | + const turboFrame = linkElement.closest('turbo-frame') |
| 68 | + |
| 69 | + if (turboFrame) { |
| 70 | + requestOptions.headers['Turbo-Frame'] = turboFrame.id |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + requestCache.set(absoluteUrl, { request: fetch(absoluteUrl, requestOptions), ttl: new Date(new Date().getTime() + cacheTtl) }) |
| 75 | +} |
| 76 | + |
| 77 | +function turboBeforeFetchRequest (event) { |
| 78 | + if (event.target.tagName !== 'FORM' && event.detail.fetchOptions.method === 'GET') { |
| 79 | + const cached = requestCache.get(event.detail.url.toString()) |
| 80 | + |
| 81 | + if (cached && cached.ttl > new Date()) { |
| 82 | + event.detail.response = cached.request |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + requestCache.clear() |
| 87 | +} |
| 88 | + |
| 89 | +function start () { |
| 90 | + if (!window.turboInstantClickEnabled) { |
| 91 | + document.addEventListener('turbo:before-fetch-request', turboBeforeFetchRequest) |
| 92 | + document.addEventListener('mouseover', mouseoverListener, { capture: true, passive: true }) |
| 93 | + } |
| 94 | + |
| 95 | + window.turboInstantClickEnabled = true |
| 96 | +} |
| 97 | + |
| 98 | +export { start } |
0 commit comments