-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always invalidating cache using fetch in NextJs #1529
Comments
Hey, @maiconsanson! I'd love for you to clarify something: you mention the The fact that your |
You should be able to customize I'll be adding documentation by EOD |
Thank you for your response! The issue arises when a server-rendered page invokes any API endpoint using fetch while setting a cache time. The PostHog configuration is in the frontend, but the This behavior only occurs when I initialize the PostHog client provider inside a I tested the latest version with the new options you introduced, but the problem persists. If helpful, I could create a full example in a new repository to further investigate and clarify the issue. |
Hey! I'd love if you could provide me with a full repro repo. I've tried reproducing it myself, and once I set the new advanced ou're saying you're using the API, so I wouldn't expect you to be using the |
@rafaeelaudibert I’ve identified the root cause of this issue. My application includes the By removing the cookies from the This might help others facing similar issues! const cookieStore = await cookies()
const fetchWithOptions = async ({ body, cacheTime, method, tags, url }) => {
const options = {
method,
credentials: 'include',
headers: {
Cookie: cookieStore?.toString() || '', // root of the problem
[X_INTERNAL_API]: apiSecret || headersList?.get(X_INTERNAL_API) || '',
},
}
if (isPresent(body) && method !== 'GET') {
options.body = JSON.stringify(body)
}
if (isPresent(cacheTime)) {
options.next = { revalidate: cacheTime, tags }
}
const response = await fetch(url, options)
const jsonResponse = await response.json()
return jsonResponse
} |
@maiconsanson Hmm, that's interesting. I wouldn't expect that to be happening. If you are'nt using our cookies in your server, you could also attempt to store the user data on Did you stop sending cookies at all or simply removed our entry? |
The PostHog initialization is currently disabled and I'm figuring out the best way to handle the issue now that I understand what's happening. I tested using the |
I think the issue is now clear. I was sending all cookies (including PostHog's) with each fetch request, which caused the side effect: Cookie: cookieStore?.toString() Restricting the request to only send the necessary cookies resolved the issue. I’ll close this issue now. Thank you for your support! |
Bug Description
After enabling fetch logs in the
next.config
, I noticed that the cached result from API calls using the extended native fetch is always invalidated with the message:Cache skipped reason: (cache-control: no-cache (hard refresh))
when fetching any API endpoint from a page.I'm using
"next": "15.0.3"
with the app folder and followed the PostHog documentation on using the client.The result is always fresh when hitting the same endpoint multiple times:
However, it should be the cached result:
How to reproduce
next.config
.Additional context
posthog-node
package, the problem doesn't occur, but it's not possible to use session replays.layout
is a server component, so I can't import thePostHogPageView
usingdynamic
withssr: false
as stated in the documentation:PostHogPageView
dynamically inside the provider, the behavior remains the same:The text was updated successfully, but these errors were encountered: