For our server-side rendered app, we want to avoid the overhead of fetching the Prismic API ref for every single requests. The ~200-300ms it takes to load the master ref are critical.
For this, we use a stale-while-revalidate mechanism to ensure most users never have to pay the cost of loading the API ref:
- The API ref is cached and considered fresh for 30 seconds. After this period of time, it becomes stale but can be used while we refresh it in background.
- A stale API ref can be used for maximum 5 minutes. After this period of time, we must refresh the ref before requesting any Prismic document.
This logic is based on the assumption that if a document is published, the master ref will change but the previous ref will eventually stay valid for a short period of time (5 minutes based on our tests).
However, in some rare cases, we saw that a ref could expire almost immediately after a document is published, causing some errors because we are using a cached ref which has expired.
Could you share some more info regarding this?
- For how long is it safe to cache an API ref after a document is published?
- Does publishing many documents in row or a big release impacts the duration of a ref?