Files
Wyatt JohnsonandGitHub dc6d612529 fix: ensure dynamic fetches are tracked correctly (#66349)
This modifies the patched fetch implementation to better handle when
called within an `unstable_cache` callback. In these callbacks, it
should not throw an error related to dynamic access.

This replaces the verbose `trackDynamicFetch` instead with
`markCurrentScopeAsDynamic` which already has support for checking if
inside an unstable cache context. It also has been adjusted to be a
no-op when `export const dynamic = "force-static"`, further simplifying
the code within the patch fetch implementation.
2024-05-30 16:54:03 +00:00

20 lines
395 B
JavaScript

import { unstable_rethrow } from 'next/navigation'
const MAX_ATTEMPTS = 5
export const fetchRetry = async (url, init) => {
for (let i = 0; i < MAX_ATTEMPTS; i++) {
try {
return await fetch(url, init)
} catch (err) {
unstable_rethrow(err)
if (i === MAX_ATTEMPTS - 1) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}