| 1 |
import type Swup from '../Swup.js'; |
| 2 |
import { Location } from '../helpers.js'; |
| 3 |
import type { Visit } from './Visit.js'; |
| 4 |
|
| 5 |
/** A page object as used by swup and its cache. */ |
| 6 |
export interface PageData { |
| 7 |
/** The URL of the page */ |
| 8 |
url: string; |
| 9 |
/** The complete HTML response received from the server */ |
| 10 |
html: string; |
| 11 |
} |
| 12 |
|
| 13 |
/** Define how a page is fetched. */ |
| 14 |
export interface FetchOptions extends Omit<RequestInit, 'cache'> { |
| 15 |
/** The request method. */ |
| 16 |
method?: 'GET' | 'POST'; |
| 17 |
/** The body of the request: raw string, form data object or URL params. */ |
| 18 |
body?: string | FormData | URLSearchParams; |
| 19 |
/** The request timeout in milliseconds. */ |
| 20 |
timeout?: number; |
| 21 |
/** Optional visit object with additional context. @internal */ |
| 22 |
visit?: Visit; |
| 23 |
} |
| 24 |
|
| 25 |
export class FetchError extends Error { |
| 26 |
url: string; |
| 27 |
status?: number; |
| 28 |
aborted: boolean; |
| 29 |
timedOut: boolean; |
| 30 |
constructor( |
| 31 |
message: string, |
| 32 |
details: { url: string; status?: number; aborted?: boolean; timedOut?: boolean } |
| 33 |
) { |
| 34 |
super(message); |
| 35 |
this.name = 'FetchError'; |
| 36 |
this.url = details.url; |
| 37 |
this.status = details.status; |
| 38 |
this.aborted = details.aborted || false; |
| 39 |
this.timedOut = details.timedOut || false; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Fetch a page from the server, return it and cache it. |
| 45 |
*/ |
| 46 |
export async function fetchPage( |
| 47 |
this: Swup, |
| 48 |
url: URL | string, |
| 49 |
options: FetchOptions = {} |
| 50 |
): Promise<PageData> { |
| 51 |
url = Location.fromUrl(url).url; |
| 52 |
|
| 53 |
const { visit = this.visit } = options; |
| 54 |
const headers = { ...this.options.requestHeaders, ...options.headers }; |
| 55 |
const timeout = options.timeout ?? this.options.timeout; |
| 56 |
const controller = new AbortController(); |
| 57 |
const { signal } = controller; |
| 58 |
options = { ...options, headers, signal }; |
| 59 |
|
| 60 |
let timedOut = false; |
| 61 |
let timeoutId: ReturnType<typeof setTimeout> | null = null; |
| 62 |
if (timeout && timeout > 0) { |
| 63 |
timeoutId = setTimeout(() => { |
| 64 |
timedOut = true; |
| 65 |
controller.abort('timeout'); |
| 66 |
}, timeout); |
| 67 |
} |
| 68 |
|
| 69 |
// Allow hooking before this and returning a custom response-like object (e.g. custom fetch implementation) |
| 70 |
let response: Response; |
| 71 |
try { |
| 72 |
response = await this.hooks.call( |
| 73 |
'fetch:request', |
| 74 |
visit, |
| 75 |
{ url, options }, |
| 76 |
(visit, { url, options }) => fetch(url, options) |
| 77 |
); |
| 78 |
if (timeoutId) { |
| 79 |
clearTimeout(timeoutId); |
| 80 |
} |
| 81 |
} catch (error) { |
| 82 |
if (timedOut) { |
| 83 |
this.hooks.call('fetch:timeout', visit, { url }); |
| 84 |
throw new FetchError(`Request timed out: ${url}`, { url, timedOut }); |
| 85 |
} |
| 86 |
if ((error as Error)?.name === 'AbortError' || signal.aborted) { |
| 87 |
throw new FetchError(`Request aborted: ${url}`, { url, aborted: true }); |
| 88 |
} |
| 89 |
throw error; |
| 90 |
} |
| 91 |
|
| 92 |
const { status, url: responseUrl } = response; |
| 93 |
const html = await response.text(); |
| 94 |
|
| 95 |
if (status === 500) { |
| 96 |
this.hooks.call('fetch:error', visit, { status, response, url: responseUrl }); |
| 97 |
throw new FetchError(`Server error: ${responseUrl}`, { status, url: responseUrl }); |
| 98 |
} |
| 99 |
|
| 100 |
if (!html) { |
| 101 |
throw new FetchError(`Empty response: ${responseUrl}`, { status, url: responseUrl }); |
| 102 |
} |
| 103 |
|
| 104 |
// Resolve real url after potential redirect |
| 105 |
const { url: finalUrl } = Location.fromUrl(responseUrl); |
| 106 |
const page = { url: finalUrl, html }; |
| 107 |
|
| 108 |
// Write to cache for safe methods and non-redirects |
| 109 |
if (visit.cache.write && (!options.method || options.method === 'GET') && url === finalUrl) { |
| 110 |
this.cache.set(page.url, page); |
| 111 |
} |
| 112 |
|
| 113 |
return page; |
| 114 |
} |