You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
run / wait can get stuck when using Next.js with App Router #136
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.
@mattt@mirko314
Hi!
Thanks for this very helpful comment.
I ran into this too. I'm fairly new to next.js with the app router and having a hard time trying to make it work.
Maybe would you have a more complete exemple to share, showing how you managed to make it work?
I'm trying to use it within a replicate create/wait structure (which is equivalent to run/wait)
That would be fantastic :-)
I agree this should be addressed. We won't be the only ones trying to use replicate on the new next.js app router.
@mattt Would it be possible to update the documentation with a more detailed explanation on how to use replicate-javascript on next.js recent versions? 😃
Activity
mirko314 commentedon Sep 20, 2023
Hey,
I also just ran into this, and it took me quite some time to debug and come to the conclusion that some "magical" caching must be the culprit.
Would you mind to share your workaround code?
With App Router and Vercel getting even more traction, this definatly should be addressed!
Thanks!
mattt commentedon Sep 20, 2023
@mirko314 Thanks for sharing. That's a helpful data point.
Here's what I remember the code to be. Please give this a try and let me know if that works for you:
mirko314 commentedon Sep 21, 2023
Thanks @mattt, that worked well!
Autometrique commentedon Sep 26, 2023
@mattt @mirko314
Hi!
Thanks for this very helpful comment.
I ran into this too. I'm fairly new to next.js with the app router and having a hard time trying to make it work.
Maybe would you have a more complete exemple to share, showing how you managed to make it work?
I'm trying to use it within a replicate create/wait structure (which is equivalent to run/wait)
That would be fantastic :-)
I agree this should be addressed. We won't be the only ones trying to use replicate on the new next.js app router.
Thanks!
pondorasti commentedon Oct 1, 2023
Just ran into the same issue and this #136 (comment) fixed it for me too. Thanks for the post @mattt, probably saved me hours of troubleshooting 🙌
Autometrique commentedon Oct 7, 2023
@mattt Would it be possible to update the documentation with a more detailed explanation on how to use replicate-javascript on next.js recent versions? 😃
leerob commentedon Dec 8, 2023
Rather than changing the Replicate
fetchbehavior, you can usenoStoreto opt-out of caching for your component.API: https://nextjs.org/docs/app/api-reference/functions/unstable_noStore
zeke commentedon May 23, 2024
I've opened a couple PRs to help prevent further confusion about this:
wuyasong commentedon May 27, 2024
This Great, I was stuck with this issue for a day, and after reading this issue, it was finally fixed