33 lines
1.5 KiB
JavaScript
33 lines
1.5 KiB
JavaScript
/**
|
|
* Recover once from an idempotent document GET's transient proxy failure.
|
|
* This never retries hydration, theme assertions, or mutations, and shares one
|
|
* 60-second budget across both attempts rather than extending the deadline.
|
|
* @param {(timeout: number) => Promise<{ok(): boolean, status(): number} | null>} navigate
|
|
* @param {string} url
|
|
* @param {() => number} now
|
|
*/
|
|
export async function navigateBrandDocument(navigate, url, now = Date.now) {
|
|
const deadline = now() + 60000;
|
|
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
const remaining = deadline - now();
|
|
if (remaining <= 0) throw new Error(`Document navigation budget exhausted: ${url}`);
|
|
try {
|
|
const response = await navigate(remaining);
|
|
if (response?.ok()) return response;
|
|
const status = response?.status();
|
|
if (attempt === 1 && [500, 502, 503, 504].includes(status ?? 0)) {
|
|
console.warn('Brand navigation proxy recovery', JSON.stringify({url, status, attempt}));
|
|
continue;
|
|
}
|
|
throw new Error(`Document navigation failed: ${url}, HTTP ${status ?? 'no response'}`);
|
|
} catch (error) {
|
|
if (attempt === 1 && /ECONNRESET|net::ERR_(?:CONNECTION_RESET|CONNECTION_CLOSED|EMPTY_RESPONSE)/.test(String(error))) {
|
|
console.warn('Brand navigation connection recovery', JSON.stringify({url, error: String(error), attempt}));
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
throw new Error(`Document navigation failed: ${url}`);
|
|
}
|