-
Notifications
You must be signed in to change notification settings - Fork 511
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
feat(defineCachedEventHandler): add event.context.cache
#2519
Conversation
Thanks for PR. I thibnk we should make it to |
event.context.cached
flag when using defineCachedEventHandler
event.context.cache
with defineCachedEventHandler
Updated to set Also added a basic test case to improve later on too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Updates:
- Added
cache.options
- Added h3 type augmentations (locally broken on main)
event.context.cache
with defineCachedEventHandler
event.context.cache
Hi @atinux, Is this feature planned for Nuxt, or is it a use case you are developing? I'm curious to see how it can be used. I believe this can be very helpful for caching heavy pages while keeping them dynamic for users. |
@meirroth You can access the same This feature initially has two usecases:
Dynamically enabling or disabling cache can be done using |
And for "nuxt-rendered" parts (so no server api / server routes), nuxt/nuxt#26598 is where we track it. |
@pi0 and @manniL, thank you for your replies! I'm trying to understand how to apply this in practice, so an example would be really helpful if you have one. My use case involves caching a page with SWR for all except requests with an auth header. This way, visitors will see a cached page, while logged-in users will see a personalized version of the page, which differs for each user. I just watched @manniL's video on Dynamic Rendering SSR/SPA. (Great video as always!) Is the approach similar? Would it involve defining server middleware that checks for an auth header and, if it exists, disables the cache with event.cache.option? Additionally, I assume the i18n module adds complexity since you'd need to cache pages for each language. |
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This allow the frontend framework connected to Nitro to be aware that the page is cached, useful for handling a flag in the payload to handle a different logic on client-side (e.g: fetch the auth state after hydration)