Description
At yesterday's hackathon, a participant shared that predictions created and polled for completion using run
would hang indefinitely, despite the Replicate dashboard showing that prediction as finishing minutes earlier.
After debugging by adding a console.log
statement in the progress
callback function, we determined that this behavior was caused by the extensions to fetch
made by Next.js when using App Router 1. From "Data Fetching, Caching, and Revalidating" in the Next.js docs:
Next.js extends the native fetch Web API to allow you to configure the caching and revalidating behavior for each fetch request on the server. React extends fetch to automatically memoize fetch requests while rendering a React component tree.
...
By default, Next.js automatically caches the returned values of fetch in the Data Cache on the server. This means that the data can be fetched at build time or request time, cached, and reused on each data request.
Because the initial GET /v1/predictions/{id}
response was cached, subsequent requests polling for status returned the same response, which had the initial "starting"
status.
Our workaround involved setting replicate.fetch
to a wrapped function that specified a cache: no-store
fetch option. But it's unclear whether there's a better way to get the desired behavior.
We should either document or add a workaround to make this work correctly in Next.js.
Footnotes
-
fetch
when using Pages Router, like in the "Build a website with Next.js" sample project, works as expected. ↩