|
| 1 | +/** |
| 2 | + * Prefetch on a phone. |
| 3 | + * |
| 4 | + * The router prefetched on mouseover only, and a finger never hovers, so on a |
| 5 | + * phone every first visit waited for its fetch. A touch now starts the fetch |
| 6 | + * at touchdown, a tap's length before the click, and links marked |
| 7 | + * data-stx-prefetch="eager" (a tab bar) are fetched once the page is idle, so |
| 8 | + * their first tap is served from the cache like a native tab switch. |
| 9 | + */ |
| 10 | +import { afterEach, describe, expect, it } from 'bun:test' |
| 11 | +import { Window } from 'very-happy-dom' |
| 12 | +import { getRouterScript } from '../src/client' |
| 13 | + |
| 14 | +const originalGlobals = { |
| 15 | + window: globalThis.window, |
| 16 | + document: globalThis.document, |
| 17 | + location: globalThis.location, |
| 18 | + history: globalThis.history, |
| 19 | + fetch: globalThis.fetch, |
| 20 | + navigator: globalThis.navigator, |
| 21 | + CustomEvent: globalThis.CustomEvent, |
| 22 | + Event: globalThis.Event, |
| 23 | + DOMParser: globalThis.DOMParser, |
| 24 | +} |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + Object.assign(globalThis, originalGlobals) |
| 28 | +}) |
| 29 | + |
| 30 | +function installRouter(body: string, connection?: { saveData?: boolean, effectiveType?: string }) { |
| 31 | + const window = new Window({ url: 'http://localhost/m' }) |
| 32 | + window.document.write(`<html><head><meta name="stx-layout" content="layouts/mobile.stx"></head><body>${body}<main>Today</main></body></html>`) |
| 33 | + ;(window as any).stx = {} |
| 34 | + ;(window as any).__stxRouterConfig = { cache: true, prefetch: true, progress: false, viewTransitions: false } |
| 35 | + if (connection) Object.defineProperty(window.navigator, 'connection', { configurable: true, value: connection }) |
| 36 | + const requested: string[] = [] |
| 37 | + Object.assign(globalThis, { |
| 38 | + window, |
| 39 | + document: window.document, |
| 40 | + location: window.location, |
| 41 | + history: window.history, |
| 42 | + navigator: window.navigator, |
| 43 | + CustomEvent: window.CustomEvent, |
| 44 | + Event: window.Event, |
| 45 | + DOMParser: window.DOMParser, |
| 46 | + fetch: async (url: string) => { |
| 47 | + requested.push(String(url)) |
| 48 | + return new Response('<section>page</section>', { status: 200, headers: { 'Content-Type': 'text/html', 'X-STX-Fragment': 'true', 'X-STX-Layout': 'layouts/mobile.stx' } }) |
| 49 | + }, |
| 50 | + }) |
| 51 | + new Function(getRouterScript())() |
| 52 | + return { window, requested } |
| 53 | +} |
| 54 | + |
| 55 | +const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) |
| 56 | + |
| 57 | +describe('router prefetch on a phone', () => { |
| 58 | + it('starts fetching a link when a finger touches it', async () => { |
| 59 | + const { window, requested } = installRouter('<a href="/m/calendar" data-stx-link id="cal">Calendar</a>') |
| 60 | + const link = window.document.getElementById('cal')! |
| 61 | + link.dispatchEvent(new window.Event('touchstart', { bubbles: true })) |
| 62 | + await wait(10) |
| 63 | + expect(requested).toContain('/m/calendar') |
| 64 | + }) |
| 65 | + |
| 66 | + it('fetches eager links once the page is idle, but not the page it is on', async () => { |
| 67 | + const { requested } = installRouter( |
| 68 | + '<nav><a href="/m" data-stx-link data-stx-prefetch="eager">Today</a>' |
| 69 | + + '<a href="/m/calendar" data-stx-link data-stx-prefetch="eager">Calendar</a>' |
| 70 | + + '<a href="/m/me" data-stx-link data-stx-prefetch="eager">Me</a>' |
| 71 | + + '<a href="/m/health" data-stx-link>Health</a></nav>', |
| 72 | + ) |
| 73 | + await wait(400) |
| 74 | + expect(requested.sort()).toEqual(['/m/calendar', '/m/me']) |
| 75 | + }) |
| 76 | + |
| 77 | + it('leaves eager links alone on a connection that asked to save data', async () => { |
| 78 | + const { requested } = installRouter('<a href="/m/calendar" data-stx-link data-stx-prefetch="eager">Calendar</a>', { saveData: true }) |
| 79 | + await wait(400) |
| 80 | + expect(requested).toEqual([]) |
| 81 | + }) |
| 82 | + |
| 83 | + it('never prefetches a link the router may not claim', async () => { |
| 84 | + const { window, requested } = installRouter('<a href="/logout" data-stx-link data-stx-no-router id="out">Log out</a>') |
| 85 | + window.document.getElementById('out')!.dispatchEvent(new window.Event('touchstart', { bubbles: true })) |
| 86 | + await wait(10) |
| 87 | + expect(requested).toEqual([]) |
| 88 | + }) |
| 89 | +}) |
0 commit comments